SQL Step-by-Step (4) Create a table

Source: Internet
Author: User

Create a table

This CREATE TABLE statement is for creating a new table. The following is a simple format for creating a table statement:

CREATE TABLE "TableName"

("Column1" "Data type",

"Column2" "Data type",

"Column3" "Data type");

If you want to use optional constraints, create a table in the format:

CREATE TABLE "TableName"

("Column1" "Data type" [constraint],

"Column2" "Data type" [constraint],

"Column3" "Data type" [constraint]);

[] = optional

Note here: You can create more than one column of tables at any point, and this condition is optional.

For a better understanding, here's an example:

CREATE TABLE Employee

(varchar (15),

Last varchar (20),

Age Number (3),

Address varchar (30),

City varchar (20),

State varchar (20));

To create a new table, you can follow the name of the table after the keyword CREATE TABLE, followed by a round opening parenthesis "(", then the first column name, the data type of the column, then any optional constraint, and finally the round closing parenthesis "). It is important to make sure that the parentheses are used before the table content is started and that the round closing parenthesis is used after the last column of the table is defined. You also have to make sure that each column definition is separated by commas. Finally, at the end of the SQL statement, add the semicolon ";".

Table and column names must start with a letter, and the second character can begin with letters, numbers, or underscores, but ensure that the total length of the name does not exceed 30 characters. Do not use SQL predefined keywords for table or column names (such as "select", "Create", "insert", and so on) to avoid errors when defining table and column names.

A data type is the type of data that is used in a particular column. If the name of a column is "last_name", it is used to hold the name of the person, so this particular column should be "varchar" (variable-length character, variable length character type) data type.

Here are a few common types of data:

char (size): A fixed-length string. The size is the parameter specified in parentheses, which can be set arbitrarily by the user, but not more than 255 bytes.

varchar (size): variable-length string type. Its maximum length is set by the parameter size in parentheses.

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

Date: Numeric type.

Number (SIZE,D): numeric type. The number of digits of its maximum number is set by the parameter Sieze in parentheses, and the parameter D in parentheses is the number of digits to set the decimal point.

What is a constraint? When a table is created, you can share a constraint in one column or multiple columns. A constraint is a basic guideline for the column, and the data returned must follow this guideline. As an example, a constraint specifies that no two records in a column share a single value. They must be separate. The other two popular constraints are: "NOT NULL", which sets the column not to leave blank, that is, must have data, and "PRIMARY key" (primary key), the primary KEY constraint defines the unique identification of each record (or row) in the table. All of these will be further elaborated in a later tutorial.

Well, maybe you've got the urge to go. As an exercise in this section, we'll design and create the table ourselves below.

You can start by creating a company employee form. You need to create a table that contains FirstName, LastName, title, age, and salary.







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.