CREATE TABLE command in SQL to create a database table

What does the CREATE TABLE command in SQL do? In this article, let's learn everything you need to know about the CREATE TABLE statement in SQL!

What does the CREATE TABLE command in SQL do? In this article, let's learn everything you need to know about the CREATE TABLE statement in SQL !

CREATE TABLE command in SQL to create a database table Picture 1CREATE TABLE command in SQL to create a database table Picture 1

If you are learning programming, you must know how to use SQL. In fact, learning SQL is not as difficult as many people think. It also has basic functions and commands. Just by knowing them, using SQL in programming projects will become simpler than ever.

Database tables in any relational database management system are used to store data in structured form (fields and records). Here, field is a column that defines the type of data stored in a table and record is a row containing the actual data. SQL provides various queries to interact with data in the most convenient way. You can use SQL commands to create and delete tables, and insert, update, and delete data in these tables. The instructions below will show you how to use SQL to create tables and other information you need to know .

CREATE TABLE command syntax

The SQL CREATE TABLE 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. The unique name or identifier for the table immediately follows the CREATE TABLE statement.

The parentheses will identify each column in the table and its data type. The above syntax will be clearer when you see the example below.

Example of CREATE TABLE command

The code below is an example of creating a NHANVIEN table with ID as the primary key and NOT NULL as a constraint to ensure fields cannot be NULL when creating records in this table.

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

With the above table creation information, you will create a table with ID and TUOI columns of type INT that will contain an integer. Columns TEN, DIACHI are of type varchar containing characters with a maximum field length of 255 characters. The LUONG column is decimal data with 2 digits after the decimal point. And this table uses the ID column as the primary key (no duplicates).

You can verify if the table was created successfully 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(255) | NO | | | | | TUOI | int(11) | NO | | | | | DIACHI | char(255) | YES | | NULL | | | LUONG | decimal(18,2) | YES | | NULL | | +---------+---------------+------+-----+---------+---------+ 5 rows in set (0.00 sec)

Create the table another way

You can also create a table from copying an existing table, using a combination of the CREATE TABLE command and the SELECT command. The copied table will have the same column data type as the original table's data type. The syntax is as follows:

CREATE TABLE ten_bang_moi AS SELECT cot_1, cot_2,. FROM ten_bang_da_co WHERE .;

For example : Create a data table KHACHHANG with columns ID, TEN, DIACHI similar to the NHANVIEN table above. You will use the following command:

CREATE TABLE KHACHHANG AS SELECT ID, TEN, DIACHI FROM NHANVIEN

Now, after you have the NHANVIEN and KHACHHANG tables ready in the database, you can start using the INSERT INTO function to insert data into the table.

3.5 ★ | 2 Vote