SQL 基础
SQL 简介
SQL 入门
SQL 语法
SQL 创建数据库
SQL 创建表
SQL Constraints
SQL Insert
SQL Select
SQL Where
SQL AND & OR
SQL IN & Between
SQL Order By
SQL Top/Limit
SQL Distinct
SQL Update
SQL Delete
SQL Truncate Table
SQL Drop
SQL 连接
SQL Joining Tables
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Cross Join
高级 SQL
SQL Union
SQL Like
SQL Alter Table
SQL Aliases
SQL Group By
SQL Having
SQL Create View
SQL Create Index
SQL Dates and Times
SQL Cloning Tables
SQL 临时表
SQL 子查询
SQL 注入
SQL 参考
SQL 数据类型
MySQL 数据类型
SQL Server 数据类型
SQL 方法
SQL 创建表 - SQL基础教程 - 笔下光年
网站首页
SQL 创建表
In this tutorial you will learn how to create a table inside the database using SQL. ## Creating a Table In the previous chapter we have learned how to create a database on the database server. Now it's time to create some tables inside our database that will actually hold the data. A database table simply organizes the information into rows and columns. The SQL `CREATE TABLE` statement is used to create a table. ## Syntax The basic syntax for creating a table can be given with: ```sql CREATE TABLE table_name ( column1_name data_type constraints, column2_name data_type constraints, .... ); ``` To understand this syntax easily, let's create a table in our demo database. Type the following statement on MySQL command-line tool and press enter: ```sql -- Syntax for MySQL Database CREATE TABLE persons ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, birth_date DATE, phone VARCHAR(15) NOT NULL UNIQUE ); -- Syntax for SQL Server Database CREATE TABLE persons ( id INT NOT NULL PRIMARY KEY IDENTITY(1,1), name VARCHAR(50) NOT NULL, birth_date DATE, phone VARCHAR(15) NOT NULL UNIQUE ); ``` The above statement creates a table named persons with four columns id, name, birth_date and phone. Notice that each column name is followed by a data type declaration; this declaration specifies that what type of data the column will store, whether integer, string, date, etc. Some [data types](http://www.bixiaguangnian.com/manual/sql/3423.html "data types") can be declared with a length parameter that indicates how many characters can be stored in the column. For example, `VARCHAR(50)` can hold up to 50 characters. <div class="callout callout-info mb-3">Note: The data type of the columns may vary depending on the database system. For example, MySQL and SQL Server supports INT data type for integer values, whereas the Oracle database supports NUMBER data type.</div> The following table summarizes the most commonly used data types supported by MySQL. | Data Type | Description | |-----------------|--------------------------| | INT | Stores numeric values in the range of -2147483648 to 2147483647 | | DECIMAL | Stores decimal values with exact precision. | | CHAR | Stores fixed-length strings with a maximum size of 255 characters. | | VARCHAR | Stores variable-length strings with a maximum size of 65,535 characters. | | TEXT | Stores strings with a maximum size of 65,535 characters. | | DATE | Stores date values in the YYYY-MM-DD format. | | DATETIME | Stores combined date/time values in the YYYY-MM-DD HH:MM:SS format. | | TIMESTAMP | Stores timestamp values. `TIMESTAMP` values are stored as the number of seconds since the Unix epoch ('1970-01-01 00:00:01' UTC). | Please check out the reference section [SQL DB data types](http://www.bixiaguangnian.com/manual/sql/3424.html "SQL DB data types") for the detailed information on all the data types available in popular RDBMS like MySQL, SQL Server, etc. There are a few additional constraints (also called modifiers) that are set for the table columns in the preceding statement. Constraints define rules regarding the values allowed in columns. - The `NOT NULL` constraint ensures that the field cannot accept a `NULL` value. - The `PRIMARY KEY` constraint marks the corresponding field as the table's primary key. - The `AUTO_INCREMENT` attribute is a MySQL extension to standard SQL, which tells MySQL to automatically assign a value to this field if it is left unspecified, by incrementing the previous value by 1. Only available for numeric fields. - The `UNIQUE` constraint ensures that each row for a column must have a unique value. We will learn more about the [SQL constraints](http://www.bixiaguangnian.com/manual/sql/3391.html "SQL constraints") in next chapter. <div class="callout callout-info mb-3">Note: The Microsoft SQL Server uses the IDENTITY property to perform an auto-increment feature. The default value is IDENTITY(1,1) which means the seed or starting value is 1, and the incremental value is also 1.</div> <div class="callout callout-success mb-3">Tip: You can execute the command DESC table_name; to see the column information or structure of any table in MySQL and Oracle database, whereas EXEC sp_columns table_name; in SQL Server (replace the table_name with actual table name).</div> ## Create Table If Not Exists If you try to create a table that is already exists inside the database you'll get an error message. To avoid this in MySQL you can use an optional clause `IF NOT EXISTS` as follow: ```sql CREATE TABLE IF NOT EXISTS persons ( id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, birth_date DATE, phone VARCHAR(15) NOT NULL UNIQUE ); ``` <div class="callout callout-success mb-3">Tip: If you want to see the list of tables inside the currently selected database, you can execute SHOW TABLES; statement on the MySQL command line.</div>
上一篇:
SQL 创建数据库
下一篇:
SQL Constraints