Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
214 views

Lab # 07 Implementation of SQL Statements (DDL)

The document discusses various SQL statements used for data definition language (DDL) operations. It describes the DROP TABLE, DROP DATABASE, and TRUNCATE TABLE statements for deleting or clearing table data. It also explains the ALTER TABLE statement for modifying columns by adding, deleting, or changing data types. Additionally, it provides examples of using the ALTER TABLE statement and discusses setting up auto-increment primary keys.

Uploaded by

Mehak Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views

Lab # 07 Implementation of SQL Statements (DDL)

The document discusses various SQL statements used for data definition language (DDL) operations. It describes the DROP TABLE, DROP DATABASE, and TRUNCATE TABLE statements for deleting or clearing table data. It also explains the ALTER TABLE statement for modifying columns by adding, deleting, or changing data types. Additionally, it provides examples of using the ALTER TABLE statement and discusses setting up auto-increment primary keys.

Uploaded by

Mehak Fatima
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

LAB # 07

Implementation of SQL
Statements (DDL)

Lab Instructor: Mehk Fatima Page 1


Lab Objective:
To know the working of some of the commands of SQL.

The DROP TABLE Statement


The DROP TABLE statement is used to delete a table.

DROP TABLE table_name

The DROP DATABASE Statement


The DROP DATABASE statement is used to delete a database.

DROP DATABASE database_name

The TRUNCATE TABLE Statement


What if we only want to delete the data inside the table, and not the table

itself? Then, use the TRUNCATE TABLE statement:

TRUNCATE TABLE table_name

SQL ALTER TABLE Statement


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

SQL ALTER TABLE Syntax

Lab Instructor: Mehk Fatima Page 2


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

ALTER TABLE table_name


ADD column_namedatatype

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

Timoteivn
1 Hansen Ola Sandnes
10

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.

Lab Instructor: Mehk Fatima Page 3


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 like this:

P_Id LastName FirstName Address City DateOfBirth

Timoteivn
1 Hansen Ola Sandnes
10

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

Change Data Type Example

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

Lab Instructor: Mehk Fatima Page 4


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 like this:

P_Id LastName FirstName Address City

Timoteivn
1 Hansen Ola Sandnes
10

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

SQL AUTO INCREMENT Field


Auto-increment allows a unique number to be generated when a new record is inserted into
a table.

AUTO INCREMENT a Field

Very often we would like the value of the primary key field to be created automatically every
time a new record is inserted.

We would like to create an auto-increment field in a table.

Syntax for MySQL

The following SQL statement defines the "P_Id" column to be an auto-increment primary key
field in the "Persons" table:

Lab Instructor: Mehk Fatima Page 5


CREATE TABLE Persons
(
P_Idint NOT NULL AUTO_INCREMENT,
LastNamevarchar(255) NOT NULL,
FirstNamevarchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

MySQL uses the AUTO_INCREMENT keyword to perform an auto-increment feature.

By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each
new record.

To let the AUTO_INCREMENT sequence start with another value, use the following
SQL statement:

ALTER TABLE Persons AUTO_INCREMENT=100

To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id"
column (a unique value will be addedautomatically):

INSERT INTO Persons (FirstName,LastName)


VALUES ('Lars','Monsen')

The SQL statement above would insert a new record into the "Persons" table. The "P_Id"
column would be assigned a unique value. The "FirstName" column would be set to "Lars" and
the "LastName" column would be set to "Monsen".

Syntax for SQL Server

The following SQL statement defines the "P_Id" column to be an auto-increment primary key
field in the "Persons" table:

Lab Instructor: Mehk Fatima Page 6


CREATE TABLE Persons
(
P_Idint PRIMARY KEY IDENTITY,
LastNamevarchar(255) NOT NULL,
FirstNamevarchar(255),
Address varchar(255),
City varchar(255)
)

The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.

By default, the starting value for IDENTITY is 1, and it will increment by 1 for each new record.

To specify that the "P_Id" column should start at value 10 and increment by 5, change
the identity to IDENTITY(10,5).

To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id"
column (a unique value will be added automatically):

INSERT INTO Persons (FirstName,LastName)


VALUES ('Lars','Monsen')

The SQL statement above would insert a new record into the "Persons" table. The "P_Id"
column would be assigned a unique value. The "FirstName" column would be set to "Lars" and
the "LastName" column would be set to "Monsen".

Syntax for Access

The following SQL statement defines the "P_Id" column to be an auto-increment primary key
field in the "Persons" table:

CREATE TABLE Persons


(
P_Id PRIMARY KEY
AUTOINCREMENT,
LastNamevarchar(255) NOT NULL,

Lab Instructor: Mehk Fatima Page 7


FirstNamevarchar(255),
Address varchar(255),
City varchar(255)
)

The MS Access uses the AUTOINCREMENT keyword to perform an auto-increment feature.

By default, the starting value for AUTOINCREMENT is 1, and it will increment by 1 for each
new record.

To specify that the "P_Id" column should start at value 10 and increment by 5, change
the autoincrement to AUTOINCREMENT(10,5).

To insert a new record into the "Persons" table, we will not have to specify a value for the "P_Id"
column (a unique value will be added automatically):

INSERT INTO Persons (FirstName,LastName)


VALUES ('Lars','Monsen')

The SQL statement above would insert a new record into the "Persons" table. The "P_Id"
column would be assigned a unique value. The "FirstName" column would be set to "Lars" and
the "LastName" column would be set to "Monsen".

Syntax for Oracle

In Oracle the code is a little bit more tricky.

You will have to create an auto-increment field with the sequence object (this object generates
a number sequence).

Use the following CREATE SEQUENCE syntax:

CREATE SEQUENCE seq_person


MINVALUE 1

Lab Instructor: Mehk Fatima Page 8


START WITH 1
INCREMENT BY1
CACHE 10

The code above creates a sequence object called seq_person, that starts with 1 and will
increment by 1. It will also cache up to 10 values for performance. The cache option specifies
how many sequence values will be stored in memory for faster access.

To insert a new record into the "Persons" table, we will have to use the nextval function
(this function retrieves the next value from seq_person sequence):

INSERT INTO Persons (P_Id,FirstName,LastName)


VALUES (seq_person.nextval,'Lars','Monsen')

The SQL statement above would insert a new record into the "Persons" table. The "P_Id"
column would be assigned the next number from the seq_person sequence. The "FirstName"
column would be set to "Lars" and the "LastName" column would be set to "Monsen".

SQL CREATE INDEX Statement


The CREATE INDEX statement is used to create indexes in tables.

Indexes allow the database application to find data fast; without reading the whole table.

Indexes

An index can be created in a table to find data more quickly and efficiently.

The users cannot see the indexes, they are just used to speed up searches/queries.

Note: Updating a table with indexes takes more time than updating a table without (because
the indexes also need an update). So you should only create indexes on columns (and tables)
that will be frequently searched against.

Lab Instructor: Mehk Fatima Page 9


SQL CREATE INDEX Syntax

Creates an index on a table. Duplicate values are allowed:

CREATE INDEX index_name


ON table_name (column_name)

SQL CREATE UNIQUE INDEX Syntax

Creates a unique index on a table. Duplicate values are not allowed:

CREATE UNIQUE INDEX index_name


ON table_name (column_name)

Note: The syntax for creating indexes varies amongst different databases. Therefore: Check the
syntax for creating indexes in your database.

CREATE INDEX Example

The SQL statement below creates an index named "PIndex" on the "LastName" column in
the "Persons" table:

CREATE INDEX PIndex


ON Persons (LastName)

If you want to create an index on a combination of columns, you can list the column names
within the parentheses, separated by commas:

CREATE INDEX PIndex


ON Persons (LastName, FirstName)

Lab Instructor: Mehk Fatima Page 10


Lab Tasks:

1): Consider the following table:

 Write an SQL statement to convert the above table into following table.

 Write SQL statement(s) to change “Birth_Date” to “Age” with data type Integer.
 Create an Index on the “Customer” table using “First_Name” and “Age”.

Lab Instructor: Mehk Fatima Page 11


2): Consider the following table “Product”:

 Decompose the above table into new tables using foreign key relationship between
the parent (Product) and child (Supplier) table.
 Add anew attribute Supplier_Name into child table (Supplier).
 Write an SQL statement that selects all supplier names whose price is not less than 20.
 Write an SQL statement that selects all products whosename is starting with “C”
and ending with “s” or “g”.
 Write an SQL statement to delete the “ProductName” entries from the table.
3): Consider the following tables:

Student Faculty

Student_ID Student_Name Faculty_ID Faculty_Name


38214 Ali 38214 Ali
54907 Ahsan 54907 Ahsan
66324 Bilal 66324 Bilal
70542 Naeem 70542 Naeem
Create the above two tables by keeping their first columns as primary key

Now using above:

 Write a query to add an attribute, Class to the Student table


 Write a query to change the field for Student_Name from 25 characters to 40 characters
 Write a query to remove the Student table
 Write a query to add another column in the Faculty table with an auto increment field
 Write a query to add another column Department in the Faculty table. The column must
not contain any value other than the values COMPUTER or CompEngineering.
 Write a query to change the auto increment field to start from 50

Lab Instructor: Mehk Fatima Page 12

You might also like