Create a data table

Source: Internet
Author: User
Tags table definition

The SQL statement CREATE table is used for creating a data table with the following basic syntax:

CREATE TABLE 表名(    字段名1 字段类型,    字段名2 字段类型, 字段名3 字段类型, ……………… 约束定义 1, 约束定义 2, ………………)

The CREATE TABLE statement here tells the database system that we are going to create a data table with the table name immediately following the CREATE TABLE statement, which cannot be duplicated with the table name already in the database. The parentheses are one or more table definitions, and the table definition includes both the field definition and the constraint definition, with at least one field definition in a table, and the constraint definition optional. A constraint definition includes a primary key definition, a foreign key definition, and a unique constraint definition. The following example illustrates the use of this statement.

The following SQL statement creates a data table to hold the people information:

CREATE TABLE T_Person(    FName VARCHAR(20), FAge INT)

Note: The above SQL works correctly in MySQL, MSSQLServer, and DB2, but may need to be rewritten in other databases due to differences in data types in various major database systems.

Here's how this SQL is written under Oracle:

CREATE TABLE T_Person(    FName VARCHAR2(20),    FAge NUMBER (10))

You can see here the name of the People data table is T_person, and there are two fields, one field for the record name fname, and the other for the age Fage. The name is a string type with an indeterminate length, so here we define the fname field using the plug-in development of the variable-length string vs studio with a maximum length of 20 and an integer, so use int to define the Fage field. The definition of each field needs to be separated by a comma, and we define each field in a separate row here, which is not mandatory, and we can define all the fields in a

Line, as follows:

CREATE TABLE T_Person(FName VARCHAR(20),FAge INT);

This kind of SQL statement is also legal, but when the number of fields is more than the time to write this is too messy, so it is recommended to use each row of a field definition, so easy to read, and error when it is easy to debug, because many database systems are based on the line number to prompt the error message.

The field definition can only limit the data types that can be populated in one field, and the requirement that the field value must be unique, the age of entry must be between 18-26 years, and that the name cannot be empty is not sufficient, which is the work of the constraint definition.

Create a data table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.