The CREATE TABLEstatementin SQL used to create a new table.Creating a new table primarily involves naming the table, defining its columns, and the data type of each column.

create table statement

create table statement

Syntax CREATE TABLE statement

The CREATE TABLE SQL statement has the following syntax:

CREATE TABLE ten_bang(  cot1 kieu_du_kieu,  cot2 kieu_du_kieu,  cot3 kieu_du_kieu,  .....  cotN kieu_du_kieu,  PRIMARY KEY( mot hoac nhieu cot ) );

CREATE TABLE is the keyword that tells the database system what you want to do.In this case. You want to create a new table.Unique names or table identifiers are immediately followed by the CREATE TABLE statement.

The parentheses define each column in the table and its data type.The syntax is more precise when you look at the example below.

It is possible to copy an existing table to create a new table using the CREATE TABLE statement and the SELECT statement.

Example of a CREATE TABLE statement

The following code is an example of create. An NHANVIEN table with IDs as primary and NOT NULL. As constraints to ensure that fields cannot be NULL when creating records in this table.

SQL> CREATE TABLE NHANVIEN(
   ID   INT              NOT NULL,
   TEN VARCHAR (20)      NOT NULL,
   TUOI  INT             NOT NULL,
   DIACHI  CHAR (25) ,
   LUONG   DECIMAL (18, 2),       
   PRIMARY KEY (ID)
);

You can verify that the table is successfully created by looking at the message. Displayed by the SQL server, or using the DESC command like this:

SQL> DESC NHANVIEN; +-----------+---------------+------+-----+---------+-------+ | Field  | Type     | Null | Key | Default | Extra | +-----------+---------------+------+-----+---------+-------+ | ID    | int(11)    | NO   | PRI |         |       | | TEN   | varchar(20)   | NO   |     |         |       | | TUOI   | int(11)   | NO   |     |         |       | |DIACHI   | char(25)   | YES  |     | NULL    |       | | LUONG   | decimal(18,2) | YES  |     | NULL    |       | +---------+---------------+------+-----+---------+---------+ 5 rows in set (0.00 sec)

Now, you have the NHANVIEN table ready in the database to be able to store the necessary information related to the staff.

Leave a Comment

Close