Creation and deletion of tables in SQL

Creation and deletion of tables in SQL

Création des tables - CREATE TABLE

In SQL to create a table you need to provide its name and its columns and the data type of each column.

L'instruction CREATE TABLE permet de créer une nouvelle table.

Syntaxe :

                                CREATE TABLE table_name(
                                    column1 data_type [constraints],
                                    column2 data_type [constraints],
                                    column3 data_type [constraints],
                                    .....
                                    columnN data_type [constraints],
                                    PRIMARY KEY( one or more columns )
                                );
                            

In any query you need first to specify what you want to do, in this case we want to create a new table, so we need to specify the keyword CREATE TABLE followed by unique name or identifier of the table.

Then, in brackets, the list defining each column of the table and its data type.

Example 1 :

The following code is an example, which creates an Employees table with an ID as the primary key and NOT NULL are the constraints indicating that these fields cannot be NULL when creating records in this table.

                                CREATE TABLE Employees(
                                    Id INT              NOT NULL,
                                    Name VARCHAR (20)    NOT NULL,
                                    Age  INT            NOT NULL,
                                    Salary   DECIMAL (18, 2),       
                                    PRIMARY KEY (Id)
                                );
                            

Drop tables - DROP TABLE

In SQL to delete a table definition as well as all the data, indexes, triggers, constraints and permission specifications for this table, we use the keyword DROP TABLE

Warning ! You must be very careful when using this command because once the table is deleted, all the information available in this table will also be permanently lost.

Syntax :

                                DROP TABLE table_name;
                            
Example 1 :

To delete the Employees table, you must run the following query:

                                DROP TABLE Employees;
                            

Share this course with your friends :

This course is written by M. ESSADDOUKI Mostafa

Many people realize their hearts desires late in life. Continue learning, never stop striving and keep your curiosity sharp, and you will never become too old to appreciate life.

0 Comment(s)

To leave a comment you must have an account Sign up, or Sign in