< Structured Query Language

Data Definition Language is used to modify the schema of the database. It will never impact the user rights for the database. Otherwise, it can erase records in some tables. It describes three statements: CREATE, ALTER and DROP.

CREATE statement

The exhaustive syntax of the CREATE statement for the tables is as follows:

CREATE TABLE <table name>
(<column name> <column type>[ NOT NULL][ PRIMARY KEY| DEFAULT <value>][, <column name> <column type>[ NOT NULL][ PRIMARY KEY| DEFAULT <value>]]*
 [,[ CONSTRAINT <constraint name>]
  {
   PRIMARY KEY (<column name>[, <column name>]*)
  |
   UNIQUE ([VALUE|<column name>[, <column name>]*])
  |
   FOREIGN KEY (<column name>[, <column name>]*) REFERENCES <table name> (<column name>[, <column name>]*)[ ON DELETE CASCADE][ ON UPDATE CASCADE]
  |
   CHECK (<predicate>[{ AND| OR} <predicate>]*)
  }
 ]*
);

The CREATE statement is used to create a new table with no record. Let's create the table office. The records in the office table will contain a technical id, the name of the office, a description, the number of available places, the availability and the date for the next office security control:

  • Query:
CREATE TABLE office
(
   id_office INTEGER PRIMARY KEY NOT NULL,
   name VARCHAR(20) NOT NULL,
   description VARCHAR(255),
   place_number INTEGER NOT NULL,
   available SMALLINT NOT NULL DEFAULT 1,
   next_inspection DATE NOT NULL
);
  • The table after the statement:
office
id_officeINTEGER
nameVARCHAR(20)
descriptionVARCHAR(255)
place_numberINTEGER
availableSMALLINT
next_inspectionDATE

Now the table office can be used and filled as the tables reunion, employee, project and members:

office
id_officenamedescriptionplace_numberavailablenext_inspection
1Show room10012011-03-24
2Big roomThe biggest room.20012010-06-03
3Open spaceThe developer open space.5012011-03-15
4HallThe entrance.2012010-10-28
5Reunion room2012010-05-12
6Actual officeThis office is under construction.502010-06-03
7Temporary officeThe office used while the actual is under construction.512011-03-15
8Coffee machineThe room where you can pause.512011-02-11

The statement starts with CREATE TABLE, to indicate that what we want to create is a table. It's followed by the name of the table (i.e. office). The name of the table is followed by parentheses which describe all the columns of the table. The descriptions of the columns are separated by a comma. Each description contains the column name (for instance, id_office), the column type (INTEGER, VARCHAR, CHAR, DATE, etc...), an optional nullability information (nothing to indicate that the column can be null or NOT NULL to indicate that the column can't be null) and the optional keyword DEFAULT followed by a default value or the optional keyword PRIMARY KEY to indicate that the column is a primary key. If no default value is defined, NULL is the default value. If NOT NULL is defined, the column can't have NULL as default value.

You can see that the column id_office has been defined as a primary key, the column description can be null and the column available has 1 as default value.

ALTER statement

The exhaustive syntax of the ALTER statement for the tables is as follows:

ALTER TABLE <table name>
{
 ADD[ COLUMN] <column name> <column type>[ NOT NULL][ PRIMARY KEY| DEFAULT <value>]
|
 ALTER[ COLUMN] <column name>[ SET DEFAULT <default option>| DROP DEFAULT]
|
 DROP[ COLUMN] <column name>
|
 ADD[ CONSTRAINT <constraint name>]
 {
  PRIMARY KEY (<column name>[, <column name>]*)
 |
  UNIQUE ([VALUE|<column name>[, <column name>]*])
 |
  FOREIGN KEY (<column name>[, <column name>]*) REFERENCES <table name> (<column name>[, <column name>]*)[ ON DELETE CASCADE][ ON UPDATE CASCADE]
 |
  CHECK (<predicate>[{ AND| OR} <predicate>]*)
 }
|
 DROP CONSTRAINT <constraint name>
};

The ALTER statement is used to modify a table. It can be used on a table with records in it.

ADD CONSTRAINT clause

This clause allows to add a constraint on the table as it could be done at the table creation time. Let's add a unicity constraint on both the name and the description of the office:

  • Query:
ALTER TABLE office ADD CONSTRAINT unique_name_and_description UNIQUE (name, description);

Now we can not insert a row with the same name and description of an already existing row and we can not update a row with the same name and description of another row. However, we can insert a row with only the same name or only the same description.

DROP CONSTRAINT clause

This clause allows to remove an existing constraint on the table by its name. Let's remove the preceding unicity constraint on both the name and the description of the office:

  • Query:
ALTER TABLE office DROP CONSTRAINT unique_name_and_description;

Now we can insert a row with the same name and description of an already existing row and we can update a row with the same name and description of another row once again.

ADD COLUMN clause

Let's add a new column has_video_projector to indicate if we can project a slideshow:

  • The table before the statement:
office
id_officenamedescriptionplace_numberavailablenext_inspection
1Show room10012011-03-24
2Big roomThe biggest room.20012010-06-03
3Open spaceThe developer open space.5012011-03-15
4HallThe entrance.2012010-10-28
5Reunion room2012010-05-12
6Actual officeThis office is under construction.502010-06-03
7Temporary officeThe office used while the actual is under construction.512011-03-15
8Coffee machineThe room where you can pause.512011-02-11
  • Query:
ALTER TABLE office ADD has_video_projector SMALLINT DEFAULT 0;
  • The table after the statement:
office
id_officenamedescriptionplace_numberavailablenext_inspectionhas_video_projector
1Show room10012011-03-240
2Big roomThe biggest room.20012010-06-030
3Open spaceThe developer open space.5012011-03-150
4HallThe entrance.2012010-10-280
5Reunion room2012010-05-120
6Actual officeThis office is under construction.502010-06-030
7Temporary officeThe office used while the actual is under construction.512011-03-150
8Coffee machineThe room where you can pause.512011-02-110

The column has_video_projector has been added at the end. The column has been filled with the default value.

DROP COLUMN clause

Now let's remove the column next_inspection:

  • The table before the statement:
office
id_officenamedescriptionplace_numberavailablenext_inspectionhas_video_projector
1Show room10012011-03-240
2Big roomThe biggest room.20012010-06-030
3Open spaceThe developer open space.5012011-03-150
4HallThe entrance.2012010-10-280
5Reunion room2012010-05-120
6Actual officeThis office is under construction.502010-06-030
7Temporary officeThe office used while the actual is under construction.512011-03-150
8Coffee machineThe room where you can pause.512011-02-110
  • Query:
ALTER TABLE office DROP COLUMN next_inspection;
  • The table after the statement:
office
id_officenamedescriptionplace_numberavailablehas_video_projector
1Show room10010
2Big roomThe biggest room.20010
3Open spaceThe developer open space.5010
4HallThe entrance.2010
5Reunion room2010
6Actual officeThis office is under construction.500
7Temporary officeThe office used while the actual is under construction.510
8Coffee machineThe room where you can pause.510

The column next_inspection has been removed. If you want to remove a column, you need to remove any constraint applied on it (for instance, you could not remove the name or the description column if there is still the unique_name_and_description unicity constraint).

DROP statement

The exhaustive syntax of the DROP statement for the tables is as follows:

DROP TABLE <table name>;

The DROP statement is used to remove table.

Now you can use a database for any type of data.

TRUNCATE statement

TRUNCATE statement is a Data Definition Language (DDL) operation that is used to mark the extents of a table for deallocation (empty for reuse). The result of this operation quickly removes all data from a table, typically bypassing a number of integrity enforcing mechanisms. The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to the DELETE FROM mytable statement (without a WHERE clause).[1]

Syntax:

TRUNCATE TABLE table_name;

table_name: Name of the table to be truncated.
DATABASE name - student_data
  1. https://www.geeksforgeeks.org/sql-drop-truncate/
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.