SQL Create Table: SQL DML and DDL
SQL Create Table: SQL DML and DDL
SQL can be divided into two parts: The Data Manipulation Language (DML) and the
Data Definition Language (DDL).
The query and update commands form the DML part of SQL:
The DDL part of SQL permits database tables to be created or deleted. It also
define indexes (keys), specify links between tables, and impose constraints
between tables. The most important DDL statements in SQL are:
SQL CREATE TABLE Statement
« Previous Next Chapter »
The data type specifies what type of data the column can hold. For a complete
reference of all the data types available in MS Access, MySQL, and SQL Server, go
to our complete Data Types reference.
CREATE TABLE Example
Now we want to create a table called "Persons" that contains five columns: P_Id,
LastName, FirstName, Address, and City.
The P_Id column is of type int and will hold a number. The LastName, FirstName,
Address, and City columns are of type varchar with a maximum length of 255
characters.
The empty table can be filled with data with the INSERT INTO statement.
SQL ALTER TABLE Statement
« Previous Next Chapter »
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
To change the data type of a column in a table, use the following syntax:
Notice that the "DateOfBirth" column is now of type year and is going to hold a year
in a two-digit or four-digit format.
SQL INSERT INTO Statement
« Previous Next Chapter »
The first form doesn't specify the column names where the data will be inserted, only their
values:
The second form specifies both the column names and the values to be inserted:
The following SQL statement will add a new row, but only add data in the "P_Id", "LastName"
and the "FirstName" columns:
SQL UPDATE Statement
« Previous Next Chapter »
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies
which record or records that should be updated. If you omit the WHERE clause, all
records will be updated!
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
SQL DELETE Statement
« Previous Next Chapter »
Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.
Note: Be very careful when deleting records. You cannot undo this statement!