Data Definition Language (DDL) Commands: Sql-Lab 1
Data Definition Language (DDL) Commands: Sql-Lab 1
TASKS
The SQL CREATE DATABASE Statement
Tables are organized into rows and columns; and each table must have a name.
The column_name parameters specify the names of the columns of the table.
1|Page
SQL-LAB 1 FALL 2014
The data_type parameter specifies what type of data the column can hold (e.g. varchar,
integer, decimal, date, etc.).
The size parameter specifies the maximum length of the column of the table.
Now we want to create a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City.
Example
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
The LastName, FirstName, Address, and City columns are of type varchar and will hold
characters, and the maximum length for these fields is 255 characters.
Tip: The empty table can be filled with data with the INSERT INTO statement.
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
2|Page
SQL-LAB 1 FALL 2014
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 new column, "DateOfBirth", is of type date and is going to hold a date. 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.
3|Page
SQL-LAB 1 FALL 2014
Now we want to change the data type of the column named "DateOfBirth" in the "Persons"
table.
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.
Next, we want to delete the column named "DateOfBirth" in the "Persons" table.
4|Page
SQL-LAB 1 FALL 2014
EXPECTED DELIVERABLE
Create the database and the table mentioned in the above diagram using SQL queries.
Create a spool file having all the commands and their executions.
5|Page