Open In App

How to Create Database Objects in MYSQL

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL, the widely used relational database management system, offers a robust set of tools for creating and managing database objects. To organize and manipulate data effectively, users can utilize SQL commands such as CREATE TABLE, CREATE VIEW, CREATE INDEX, and CREATE PROCEDURE.

In this article, We will explore the creation of these database objects in MySQL by understanding various methods along with the examples in detail.

How to Create Database Objects in MYSQL

The process of creating database objects in MySQL involves several key components. First, tables are created using the CREATE TABLE command, defining the structure of the data. Then Views can be created with the CREATE VIEW command and offering customized perspectives on the data. Indexes can enhance performance by creating efficient data retrieval paths using the CREATE INDEX command. Lastly, stored procedures can be implemented with the CREATE PROCEDURE command and enable the execution of predefined logic on the database. Below are the approaches which we will discuss in the article.

  1. Using Basic Regular Expression Patterns
  2. Using Advanced Regular Expression Features

Let’s set up an Environment:

To understand How to Create Database Objects in MYSQL we need a table on which we will perform various operations and queries. Here we will consider a table called employees which contains id, name, salary, and age as Columns.

MySQL-create-table

create table

Let’s Creating Views

Syntax:

CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example:

CREATE VIEW high_salary_employees AS
SELECT name, salary
FROM employees
WHERE salary > 50000;

In this query its creating a view named high_salary_employees, which returns those data as name and salaries from the employees table for those employees who has salaries more than 50000.

Output:

MySQL-create-views

create view

Let’s Create Indexes

CREATE INDEX idx_age
ON employees (age);

Output:

MySQL-create-index

create index

In the SQL query its creating a index for the age column of the employees table, and because of this created index the data retrievel operations will much be fast. Because Indexes improve the efficiency of data retrieval operations by allowing faster lookup of rows based on the indexed column.

Let’s Creating Stored Procedures

DELIMITER $$
CREATE PROCEDURE increase_salary(IN empid INT, IN amount DECIMAL(10,2))
BEGIN
UPDATE employees
SET salary = salary + amount
WHERE id = empid;
END $$
DELIMITER ;

Output:

MySQL-create-procedure

create procedure

In this SQL query its created a stored procedure with name of increase_salary, it takes arguments empid and amount. It will update the salary value of specified empid by the given amount.

1. Using Basic Regular Expression Patterns

By the regular expression gives a basic way to match text in the string. Because its have defined Literals, Characters and metacharacters that define the pattern to be matched.

Let’s say we want to match email addresses in a given text.

SELECT * FROM employees 
WHERE email REGEXP '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$';

Output:

MySQL-REGXP1

regexp

Explanation:

  • ^[a-zA-Z0-9._%+-]+ : This will match the username part of the email address, which begins with one or more letters, numbers, dots, underscores, percent signs, plus signs or hyphens.
  • @ : It matches the symbol “@”.
  • [a-zA-Z0-9.-]+ : This matches the domain part of the email address that consists of at least one letter or number followed by a period and/or hyphen.
  • \. : The dot is escaped to get dot from domain
  • [a-zA-Z]{2,}$ : It matches top-level domain (TLD) part of the email address and it consists of at least two alphabets.

2. Using Advanced Regular Expression Features

This includes advanced regex features that go beyond simple patterns to incorporate more complex matching options such as quantifiers, anchors and lookaheads/lookbehinds.

Suppose we would like to find phone numbers of a particular format.

SELECT mobile
FROM employees
WHERE mobile REGEXP '^(\\+\\d{1,2}\\s?)?(\\d{3}[-\\s]?)?\\d{3}[-\\s]?\\d{4}$';

Output:

MySQL-REGXP2

regexp

Explanation:

  • ^ : It signifies the start of a given string.
  • (\\+\\d{1,2}\\s?)? : This is an optional international dialing code like +1 or +91 and could be followed by an optional space.
  • (\\d{3}[-\\s]?)? : Three digits that make up the area code might be followed by an optional dash or space.
  • \d{3}\s?-? : The phone number’s first three digits are then followed by a dash or space if necessary.
  • \d{4}$ : This is the last four digits of the phone number, which come before the end of any string.

Conclusion

Overall, By mastering the creation of database objects in MySQL, users can effectively organize and manage their data. Whether it’s creating tables to structure data, views to provide customized data perspectives, indexes to improve data retrieval performance, or stored procedures to automate common tasks.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads