Lab # 07 Implementation of SQL Statements (DDL)
Lab # 07 Implementation of SQL Statements (DDL)
Implementation of SQL
Statements (DDL)
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:
Timoteivn
1 Hansen Ola Sandnes
10
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.
Timoteivn
1 Hansen Ola Sandnes
10
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.
Timoteivn
1 Hansen Ola Sandnes
10
Very often we would like the value of the primary key field to be created automatically every
time a new record is inserted.
The following SQL statement defines the "P_Id" column to be an auto-increment primary key
field in the "Persons" table:
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:
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):
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".
The following SQL statement defines the "P_Id" column to be an auto-increment primary key
field in the "Persons" table:
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):
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".
The following SQL statement defines the "P_Id" column to be an auto-increment primary key
field in the "Persons" table:
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):
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".
You will have to create an auto-increment field with the sequence object (this object generates
a number sequence).
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):
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".
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.
Note: The syntax for creating indexes varies amongst different databases. Therefore: Check the
syntax for creating indexes in your database.
The SQL statement below creates an index named "PIndex" on the "LastName" column in
the "Persons" table:
If you want to create an index on a combination of columns, you can list the column names
within the parentheses, separated by commas:
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”.
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