DBMS Lab # 2
DBMS Lab # 2
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:
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
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 second form specifies both the column names and the values to be inserted:
5 Tjessem Jakob
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
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!
5 Tjessem Jakob
Now we want to update the person "Tjessem, Jakob" in the "Persons" table.
We use the following SQL statement:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
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!
SELECT column_name(s)
FROM table_name
and
SELECT * FROM table_name
Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select the content of the columns named "LastName" and
"FirstName" from the table above.
We use the following SELECT statement:
Hansen Christ
Svendson Tove
Pettersen Michael
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select only the distinct values from the column named "City" from
the table above.
We use the following SELECT statement:
SELECT DISTINCT City FROM Persons
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select only the persons living in the city "Sandnes" from the table above.
We use the following SELECT statement:
This is correct:
SELECT * FROM Persons WHERE
Year=1965 This is wrong:
SELECT * FROM Persons WHERE Year='1965'
= Equal
IN If you know the exact value you want to return for at least one of the columns
Now we want to select only the persons with the first name equal to "Tove" AND the last
name equal to "Svendson":
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'
OR Operator Example
Now we want to select only the persons with the first name equal to "Tove" OR the
first name equal to "Christ":
We use the following SELECT statement:
You can also combine AND and OR (use parenthesis to form complex expressions).
Now we want to select only the persons with the last name equal to "Svendson" AND the
first name equal to "Tove" OR to "Christ":
We use the following SELECT statement:
Example
The "Persons" table:
P_Id LastName FirstName Address City
Now we want to select all the persons from the table above, however, we want to sort
the persons by their last name.
We use the following SELECT statement:
Additional Commands:
ALTER DATABASE <name>
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
TASK 1:
Create the following table using SQL and using the INSERT INTO command, insert the following values in
the table created.
Name Reg_No Courses Course_Code Offered_By
Ikram 09 DIP
Hassan 10
TASK 2:
Using the UPDATE statement, update the above table for the following values:
Name Reg_No Courses Course_Code Offered_By
TASK 3:
Using the DELETE statement, delete the record for the student having name Akram and
Ahsan in the above table. Also delete the record for the course having course code=1001.
TASK 4:
Select distinct values from the above table for the last three columns.
TASK 5:
Sort the above table in descending order by their name.
TASK 6:
Create and delete any database while multiple users are using it with
ROLLBACK command.
TASK 7:
For the table in task 2, generate a query for updating the table with fully qualified
names and update the following values: