T Sql 简明教程

T-SQL - Create Tables

创建基本表涉及命名表并定义其列和每个列的数据类型。

Creating a basic table involves naming the table and defining its columns and each column’s data type.

SQL Server CREATE TABLE 语句用于创建新表。

The SQL Server CREATE TABLE statement is used to create a new table.

Syntax

以下是 CREATE TABLE 语句的基本语法:

Following is the basic syntax of CREATE TABLE statement −

CREATE TABLE table_name(
   column1 datatype,
   column2 datatype,
   column3 datatype,
   .....
   columnN datatype,
   PRIMARY KEY( one or more columns ));

CREATE TABLE 是告诉数据库系统你想要做什么的关键字。在本例中,你要创建一个新表。表中唯一名称或标识符应位于 CREATE TABLE 语句后面。然后括号中应该是定义表中每一列的内容及其是什么数据类型的列表。通过以下示例可以更清楚地了解语法。

CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax becomes clearer to understand with the following example.

可以使用 CREATE TABLE 语句与 SELECT 语句的组合来创建现有表的副本。你可以在使用其他表创建表中查看所有详细信息。

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. You can check complete details at Create Table Using another Table.

Example

在本示例中,让我们创建一个以 ID 为主键且 NOT NULL 的 CUSTOMERS 表,约束表明在该表中创建记录时,这些字段不能为 NULL:

In this example, let’s create a CUSTOMERS table with ID as primary key and NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table −

CREATE TABLE CUSTOMERS(
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   AGE  INT              NOT NULL,
   ADDRESS  CHAR (25) ,
   SALARY   DECIMAL (18, 2),
   PRIMARY KEY (ID));

你可以通过查看 SQL Server 显示的消息来验证你的表是否已成功创建,此外还可以使用以下命令:

You can verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the following command −

exec sp_columns CUSTOMERS

以上命令会产生以下输出。

The above command produces the following output.

TABLE_QUALIFIER   TABLE_OWNER   TABLE_NAME   COLUMN_NAME   DATA_TYPE   TYPE_NAME
   PRECISION   LENGTH SCALE   RADIX   NULLABLE   REMARKS   COLUMN_DEF   SQL_DATA_TYPE
   SQL_DATETIME_SUB   CHAR_OCTET_LENGTH   ORDINAL_POSITION   IS_NULLABLE   SS_DATA_TYPE

TestDB    dbo    CUSTOMERS   ID        4    int      10   4    0      10     0
   NULL   NULL   4   NULL    NULL      1    NO       56

TestDB    dbo    CUSTOMERS   NAME      12   varchar  20   20   NULL   NULL   0
   NULL   NULL   12   NULL   20        2    NO       39

TestDB    dbo    CUSTOMERS   AGE       4    int      10   4    0      10     0
   NULL   NULL   4   NULL    NULL      3    NO       56

TestDB    dbo    CUSTOMERS   ADDRESS   1    char     25   25   NULL   NULL   1
   NULL   NULL   1   NULL    25   4    YES  39

TestDB    dbo    CUSTOMERS   SALARY    3    decimal  18   20   2      10     1
   NULL   NULL   3   NULL    NULL      5    YES      106

你现在可以看见 CUSTOMERS 表已在你的数据库中可用,你可以使用它来存储与客户相关的所需信息。

You can now see that CUSTOMERS table is available in your database which you can use to store required information related to customers.