Database
Database
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 INSERT INTO statement is used to insert new record or row in a table.
The second form specifies both the column names and the values to be
inserted:
Stavange
3 Pettersen Michael Storgt 20 r
Stavange
4 Nilsen Johan Bakken 2 r
5 Tjessem Jakob
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Stavange
3 Pettersen Michael Storgt 20 r
Stavange
4 Nilsen Johan Bakken 2 r
5 Tjessem Jakob
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
The "Persons" table will now look like this:
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
Note: Be very careful when deleting records. You cannot undo this
statement!
and
SELECT * FROM table_name
Example
FirstNam
P_Id LastName e Address City
Timoteivn
1 Hansen Christ 10 Sandnes
Stavang
3 Pettersen Michael Storgt 20 er
LastName FirstName
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:
LastNam FirstNam
P_Id e e Address City
Timoteivn
1 Hansen Christ 10 Sandnes
Svendso
2 n Tove Borgvn 23 Sandnes
Petterse Stavang
3 n Michael Storgt 20 er
Timoteivn
1 Hansen Christ 10 Sandnes
Svendso
2 n Tove Borgvn 23 Sandnes
Petterse Stavang
3 n Michael Storgt 20 er
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
Timoteivn Sandne
1 Hansen Christ 10 s
Sandne
2 Svendson Tove Borgvn 23 s
SQL uses single quotes around text values (most database systems will
also accept double quotes).
Although, numeric values should not be enclosed in quotes.
For text values:
This is correct:
SELECT * FROM Persons WHERE
FirstName='Tove' This is wrong:
SELECT * FROM Persons WHERE FirstName=Tove
This is correct:
SELECT * FROM Persons WHERE
Year=1965 This is wrong:
SELECT * FROM Persons WHERE Year='1965'
Stavang
3 Pettersen Michael Storgt 20 er
Borgvn Sandne
2 Svendson Tove 23 s
OR Operator Example
LastNam
P_Id e FirstName Address City
Svendso
2 n Tove Borgvn 23 Sandnes
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:
Borgvn Sandne
2 Svendson Tove 23 s
Example
Stavange
3 Pettersen Michael Storgt 20 r
Stavange
4 Nilsen Tom Vingvn 23 r
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:
Stavange
3 Pettersen Michael Storgt 20 r
Stavange
4 Nilsen Tom Vingvn 23 r
Timoteivn
1 Hansen Christ 10 Sandnes
Additional Commands:
TASK 1:
Create the following table using SQL and using the INSERT INTO
command, insert the following values in the table created.
Ikram 09 DIP
Hassan 10
TASK 2:
Using the UPDATE statement, update the above table for the following
values:
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.
Answer:
DELETE FROM school
WHERE Name IN ('Akram', 'Ahsan');
TASK 4:
Select distinct values from the above table for the last three columns.
Answer:
TASK 5:
Sort the above table in descending order by their name.
Answer:
SELECT *
FROM school
ORDER BY Name DESC;
TASK 6:
Create and delete any database while multiple users are using
it with ROLLBACK command.
Answer:
BEGIN TRANSACTION;
BEGIN TRANSACTION;
ROLLBACK;
TASK 7:
For the table in task 2, generate a query for updating the table with
fully qualified names and update the following values:
Answer:
UPDATE school
SET Courses = 'SE',
Course_Code = 1001,
Offered_By = 'Mr. Z'
WHERE Name = 'Ali' AND Reg_No = 01;
UPDATE school
SET Courses = 'CG',
Course_Code = 1002,
Offered_By = 'Mr. X'
WHERE Name = 'Basit' AND Reg_No = 02;
SELECT *
FROM Student;