Open In App

How to Create Database Objects in MYSQL

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.

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:

create view

Let’s Create Indexes

CREATE INDEX idx_age
ON employees (age);

Output:

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:

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:

regexp

Explanation:

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:

regexp

Explanation:

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.


Article Tags :