SQL Lab Manual Guide Materials
SQL Lab Manual Guide Materials
1|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
SQL DATA TYPES : Data Types are a classification of a particular type of information. As like
other programming languages, the user should specify the data types for each field in the
database.
SQL is very flexible language that enables you to accomplish all kinds of tasks. SQL
Statements (commands) – these commands are available in different relational DBMS
software such as MySQL, MSSQL, SQLlite, PostGreSQL, etc. In this lab, we select
MySQL as it is open source and most popular to develop web applications.
Installation see My SQL Community Server download page
(https://dev.mysql.com/downloads/installer/)
Connecting and disconnecting mySQL: basically, there two options two write SQL
commands. (1) Command line: run “mysql -u root -p” from command line and (1)
Using GUI tools like Workbench MySQL. We will use workbench MySQL in this
session.
To see the available database: use the SHOW database command. e.g SHOW
DATABASES;
To Select a database from the list, use the USE command. e.g. USE db_name;
2|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
3|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
To Add/Drop primary key constraint or Foreign Key constraint, we use ALTER statement
ALTER TABLE base_table_name
o ADD/DROP CONSTRAINT constraint_name
o PRIMARY KEY(column_name)
Example:
ALTER TABLE base_table_name
o ADD/DROP CONSTRAINT constraint_name
o FORIENGN KEY(column_name) REFERENCES Table_name (Column Name).
Example:
Deleting a table: used to delete an existing table.
General Form:
o DROP TABLE base_table_name|database_name|view_name
Example:
4|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
5|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
The SELECT-lists the items-list of column names, list of computed values and aggregate
functions (min, max, count, avg, etc) to be retrieved.
The FROM clause specifies the table(s) from which information is retrieved.
The WHERE clause tells to SQL to include only certain rows of data. It specifies search
criteria. The criteria may be combined by logical operators: OR, AND, IN, LIKE, NOT etc
The GROUP BY clause specifies a summary query. It usually used with aggregate function.
E.g. GROUP BY PublisherName….will group result set based on publisher’s Name
The HAVING clause tells SQL to include only certain groups produced by the GROUP BY
clause in the query result set. Having clause is the equivalent of where clause.
The ORDER BY clause sorts or orders results based on the data in one or more columns in
ASC or DESC order.
Note: Select * …….selects data in all columns.
Note: Use DISTINCT to avoid duplicates of rows in the results.
EXAMPLE:
--Total number of students who in each course
select count(takes.sid) as [Number of Students],course.ctitle from student inner join takes on
Student.Sid=takes.sid
inner join course on course.cid=takes.cid
group by course.cid,ctitle--,course.ctitle
6|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
7|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
8|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
9|Page
SQL Lab Manual Database System-SWEG2108 AASTU2022
Types of JOIN:
[Natural] Join: returns all matching rows from both rows by making Cartesian product. Same as
inner join or join.
e.g SELECT Student.Name, Takes.grade, Course.CTitle
FROM Course ,Student, takes
where Course.CID = Takes.CID and Takes.SID = Student.SID
Self Join: Join a table with itself
E.g. Find out the titles that have the same price.
Select c1.title,c1.price from catalog c1, catalog c2 where c1.price=c2.price
E.g. Students having same cgpa.
select distinct s1.name+' And '+s2.name+' have same cgpa of ', s1.cgpa from student s1, student s2
where s1.cgpa=s2.cgpa and s1.name<>s2.name
[Inner]Join: returns matching rows from both tables.
Outer Join: returns both matching or/and non-matching rows from both tables.
Outer Join: Types: Left[outer],Right[outer] and Full[outer]
General Form for Inner/Outer Join:
Table_reference[natural] outer_join_type
JOIN table_reference
[ON join_condition]
[USING column_commalist]
If Natural is used, ON or Using are not necessary.
Left outer join-retains non matching rows from the left table in the result.
10 | P a g e
SQL Lab Manual Database System-SWEG2108 AASTU2022
b)Using IN
select * from student
where SID IN
(select SID from Takes
where CID IN(select cid from course
where ctitle='Database System' ))
11 | P a g e
SQL Lab Manual Database System-SWEG2108 AASTU2022
12 | P a g e
SQL Lab Manual Database System-SWEG2108 AASTU2022
AS
UPDATE [Student] SET [Name] = @Name,[cgpa] = @cgpa, [Dobith] = @birthDate WHERE
[SID] = @SID
GO
EXEC UpdateStudent 'ZEMEN',4.0,'8/30/1990',5
13 | P a g e
SQL Lab Manual Database System-SWEG2108 AASTU2022
Lab 8: Triggers
Triggers on Student database
Triggers is a kind of stored procedure that is executed automatically when an action is
performed on database objects
a)To inform the user when user inserts new rows
CREATE trigger [student_trigger] on [dbo].[Student]
after insert as
select 'one new row is inserted'Example:
Insert into student values(33,'Haimanot','Female',3.8,'12/3/1988 12:00:00 AM',27)
b)To prevent user from deleting data
Lab 1: Exercise
create mini-student database for a data requirement specification:
“Student takes course.” Identify entities, attributes, relationships, PK, FK,
check constraints, defaults, null values,. Create tables using MS Access.
Populate sample data and formulate queries to retrieve data. Create it
using MYSQL.
14 | P a g e
SQL Lab Manual Database System-SWEG2108 AASTU2022
15 | P a g e