English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The CREATE DATABASE command is used to create a new SQL database.
The following SQL creates a database named "testDB":
CREATE DATABASE testDB;
Tip:Before creating any database, make sure you have administrative privileges. After creating a database, you can use the following SQL command to check it in the list of databases: SHOW DATABASES;
The CREATE TABLE command is used to create a new table in the database.
The following SQL creates a table named "Persons", which contains five columns: PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
The following SQL creates a new table named "TestTables" (this table is a copy of two columns from the "Customers" table):
CREATE TABLE TestTable AS SELECT customername, contactname FROM customers;
The CREATE INDEX command is used to create an index in the table (allowing duplicate values).
Indexes are used to retrieve data from the database very quickly. Users do not see indexes; they are only used to accelerate searches/Query.
The following SQL creates an index named "idx_lastname" on the "LastName" column of the "Persons" table:
CREATE INDEX idx_lastname ON Persons (LastName);
If you want to create an index on a column combination, you can list the column names within parentheses, separated by commas:
CREATE INDEX idx_pname ON Persons (LastName, FirstName);
Note:The syntax for creating indexes varies between databases. Therefore: please check the syntax for creating indexes in the database first.
Note:Updating a table using an index takes more time than updating a table without an index (because the index also needs to be updated). Therefore, it is strongly recommended to create indexes only on columns that are frequently searched.
The CREATE UNIQUE INDEX command creates a unique index on the table (disallowing duplicate values).
The following SQL creates an index named "uidx_pid" on the "PersonID" column of the "Persons" table:
CREATE UNIQUE INDEX uidx_pid ON Persons (PersonID);
The CREATE VIEW command creates a view.
A view is a virtual table based on the result set of an SQL statement.
The following SQL creates a view selecting all customers from Brazil:
CREATE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName FROM Customers WHERE Country = "Brazil";
The CREATE OR REPLACE VIEW command updates the view.
The following SQL adds the "City" column to the "Brazil Customers" view:
CREATE OR REPLACE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName, City FROM Customers WHERE Country = "Brazil";
We can query the following views:
SELECT * FROM [Brazil Customers];
The CREATE PROCEDURE command is used to create stored procedures.
Stored procedures are saved prepared SQL code that can be reused.
The following SQL creates a stored procedure named 'SelectAllCustomers', which selects all records from the 'Customers' table:
CREATE PROCEDURE SelectAllCustomers AS SELECT * FROM Customers GO;
Execute the following stored procedure as shown below:
EXEC SelectAllCustomers;