0% found this document useful (0 votes)
219 views

Data Definition Language (DDL) Commands: Sql-Lab 1

This document discusses SQL data definition language (DDL) commands like CREATE, ALTER, and DROP. It provides examples of using CREATE DATABASE to create a database, CREATE TABLE to define a table with columns, ALTER TABLE to add, modify, and drop columns, and DROP COLUMN to remove a column from a table. The objective is for students to practice these DDL commands to create a database and table called "Persons" with various columns using SQL queries in a spool file.

Uploaded by

Adnan Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
219 views

Data Definition Language (DDL) Commands: Sql-Lab 1

This document discusses SQL data definition language (DDL) commands like CREATE, ALTER, and DROP. It provides examples of using CREATE DATABASE to create a database, CREATE TABLE to define a table with columns, ALTER TABLE to add, modify, and drop columns, and DROP COLUMN to remove a column from a table. The objective is for students to practice these DDL commands to create a database and table called "Persons" with various columns using SQL queries in a spool file.

Uploaded by

Adnan Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL-LAB 1 FALL 2014

DATA DEFINITION LANGUAGE (DDL) COMMANDS


OBJECTIVE (AIM) OF THE EXPERIMENT
To practice and implement data definition language commands like Create, Alter, Drop etc.
EQUIPMENT USED
Sl. Facilities Required Quantity
No.
1 System 1
2 Operating System Windows 7
3 DBMS Sql Server Management Studio
2012

TASKS
The SQL CREATE DATABASE Statement

The CREATE DATABASE statement is used to create a database.

SQL CREATE DATABASE Syntax

CREATE DATABASE dbname;

SQL CREATE DATABASE Example

The following SQL statement creates a database called "my_db":

CREATE DATABASE my_db;

Database tables can be added with the CREATE TABLE statement.

The SQL CREATE TABLE Statement

The CREATE TABLE statement is used to create a table in a database.

Tables are organized into rows and columns; and each table must have a name.

SQL CREATE TABLE Syntax

CREATE TABLE table_name


(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);

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.

SQL CREATE TABLE Example

Now we want to create a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City.

We use the following CREATE TABLE statement:

Example
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

The PersonID column is of type int and will hold an integer.

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.

The empty "Persons" table will now look like this:

PersonID LastName FirstName Address City

Tip: The empty table can be filled with data with the INSERT INTO statement.

The ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

SQL ALTER TABLE Syntax

To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype

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):

ALTER TABLE table_name


DROP COLUMN column_name

To change the data type of a column in a table, use the following syntax:

ALTER TABLE table_name


ALTER COLUMN column_name datatype

SQL ALTER TABLE Example

Look at the "Persons" table:

P_Id LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

Now we want to add a column named "DateOfBirth" in the "Persons" table.

We use the following SQL statement:

ALTER TABLE Persons


ADD DateOfBirth date

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.

The "Persons" table will now look like this:

P_Id LastName FirstName Address City DateOfBirth


1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

Change Data Type Example

3|Page
SQL-LAB 1 FALL 2014
Now we want to change the data type of the column named "DateOfBirth" in the "Persons"
table.

We use the following SQL statement:

ALTER TABLE Persons


ALTER COLUMN DateOfBirth year

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.

DROP COLUMN Example

Next, we want to delete the column named "DateOfBirth" in the "Persons" table.

We use the following SQL statement:

ALTER TABLE Persons


DROP COLUMN DateOfBirth

The "Persons" table will now look like this:

P_Id LastName FirstName Address City


1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

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

You might also like