Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

SQL 教程

SQL 主页 SQL 简介 SQL 语法 SQL 选择 SQL 选择 DISTINCT SQL WHERE SQL ORDER BY SQL AND SQL OR SQL NOT SQL 插入数据 SQL 空值 SQL 更新 SQL 删除 SQL 选择 TOP SQL 聚合函数 SQL MIN 和 MAX SQL COUNT SQL SUM SQL AVG SQL LIKE SQL 通配符 SQL IN SQL BETWEEN SQL 别名 SQL 连接 SQL 内连接 SQL 左连接 SQL 右连接 SQL 全连接 SQL 自连接 SQL UNION SQL GROUP BY SQL HAVING SQL EXISTS SQL ANY, ALL SQL SELECT INTO SQL INSERT INTO SELECT SQL CASE SQL 空值函数 SQL 存储过程 SQL 注释 SQL 运算符

SQL 数据库

SQL 创建数据库 SQL 删除数据库 SQL 数据库备份 SQL 创建表 SQL 删除表 SQL 修改表 SQL 约束 SQL 非空 SQL 唯一 SQL 主键 SQL 外键 SQL 检查 SQL 默认值 SQL 索引 SQL 自动增长 SQL 日期 SQL 视图 SQL 注入 SQL 托管 SQL 数据类型

SQL 参考

SQL 关键字 MySQL 函数 SQL Server 函数 MS Access 函数 SQL 快速参考

SQL 例子

SQL 例子 SQL 编辑器 SQL 测验 SQL 练习 SQL Server SQL 集训营 SQL 证书

SQL CREATE 关键字


CREATE DATABASE

The CREATE DATABASE command is used is to create a new SQL database.

The following SQL creates a database called "testDB"

Example

CREATE DATABASE testDB;

提示: 确保您在创建任何数据库之前拥有管理员权限。创建数据库后,您可以使用以下 SQL 命令在数据库列表中检查它:SHOW DATABASES;


CREATE TABLE

The CREATE TABLE command creates a new table in the database.

The following SQL creates a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City

Example

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
);

CREATE TABLE Using Another Table

The following SQL creates a new table called "TestTables" (which is a copy of two columns of the "Customers" table): 

Example

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

CREATE INDEX

The CREATE INDEX command is used to create indexes in tables (allows duplicate values).

Indexes are used to retrieve data from the database very fast. The users cannot see the indexes, they are just used to speed up searches/queries.

The following SQL creates an index named "idx_lastname" on the "LastName" column in the "Persons" table

CREATE INDEX idx_lastname
ON Persons (LastName);

If you want to create an index on a combination of columns, you can list the column names within the parentheses, separated by commas

CREATE INDEX idx_pname
ON Persons (LastName, FirstName);

注意: 创建索引的语法在不同的数据库中有所不同。因此:检查您的数据库中创建索引的语法。

注意: 更新带索引的表比更新不带索引的表需要更多时间(因为索引也需要更新)。因此,只对经常搜索的列创建索引。


CREATE UNIQUE INDEX

The CREATE UNIQUE INDEX command creates a unique index on a table (no duplicate values allowed)

The following SQL creates an index named "uidx_pid" on the "PersonID" column in the "Persons" table

CREATE UNIQUE INDEX uidx_pid
ON Persons (PersonID);

CREATE VIEW

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 that selects all customers from Brazil

Example

CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";

CREATE OR REPLACE VIEW

The CREATE OR REPLACE VIEW command updates a view.

The following SQL adds the "City" column to the "Brazil Customers" view

Example

CREATE OR REPLACE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = "Brazil";

Query The View

We can query the view above as follows

Example

SELECT * FROM [Brazil Customers];

CREATE PROCEDURE

The CREATE PROCEDURE command is used to create a stored procedure.

A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again.

The following SQL creates a stored procedure named "SelectAllCustomers" that selects all records from the "Customers" table

Example

CREATE PROCEDURE SelectAllCustomers
AS
SELECT * FROM Customers
GO;

Execute the stored procedure above as follows

Example

EXEC SelectAllCustomers;

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.