Quick Start for SQL language (ii)

Source: Internet
Author: User

Create a table

The CREATE TABLE statement in the SQL language is used to create a new database table. The use format for the Create table statement is as follows:

CREATE TABLE TableName

(Column1 data type,

Column2 data type,

Column3 data type);

You can use the optional conditional option if the user wants to specify the column restrictions when creating a new table:

CREATE TABLE TableName

(Column1 data type [constraint],

Column2 data type [constraint],

COLUMN3 data type [constraint]);

Examples are as follows:

CREATE TABLE Employee

(FirstName varchar (15),

LastName varchar (20),

Age Number (3),

Address varchar (30),

City varchar (20));

In short, when creating a new table, add the name of the table you want to create after the keyword CREATE TABLE, and then set the column names, data types, and optional restrictions in parentheses. Note that all SQL statements use the ";" Symbol at the end.

The names of the columns in the database tables and tables that you create using SQL statements must begin with a letter, followed by letters, numbers, or underscores, and the name cannot exceed 30 characters in length. Note that users do not use reserved keywords in the SQL language when selecting table names, such as SELECT, Create, insert, and so on, as table or column names.

A data type is used to set the type of data in a specific column. For example, you can only use the data type of varchar or char in the Name column, not the data type of number.

The more commonly used data types in SQL languages are:

char (size): A fixed-length string in which the size in parentheses is used to set the maximum length of the string. The maximum length of a char type is 255 bytes.

varchar (size): A variable-length string with a maximum length set by size.

Number (size): The numeric type in which the maximum number of digits is set by size.

Date: Day type.

Number (SIZE,D): The numeric type, size determines the maximum number of digits for the digit, and D is used to set the number of digits after the decimal point.

Finally, the one thing you need to be aware of when creating a new table is the restrictions on the columns in the table. The limiting condition is the rule that must be followed when entering data into a particular column. For example, a unique constraint requires that two records of the same value cannot exist in a column, and that all records must be unique. In addition to the unique, the more commonly used column restrictions include NOT NULL and primary key. Not NULL is used to specify that the value of a column in a table cannot be empty. Primary Key sets a unique identifier for all records in the 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.