Dbms Lab Manual PDF
Dbms Lab Manual PDF
Laboratory Manual
for
Fundamentals of Database
Table of Contents
Session 1: introduction to database and Generally laboratory instruction ........................... 1
Installing SQL Server software ................................................................................................. 2
opening and connecting Microsoft SQL server ...................................................................... 7
Session 2 Data Definition language ............................................................................................ 8
CREATING, MODIFYING AND Dropping DATABASES ............................................................. 8
Session 3 DATA DEFINITION, CONSTRAINTS, AND SCHEMA CHANGES ................................. 11
CREATING, MODIFYING AND DELETING TABLES ................................................................... 11
Session 3 DATA MANIPULATION LANGUAGE (DML) ............................................................... 13
INSERT UPDATE DELETE TABLES ............................................................................................ 14
Session 4 DATA RETRIEVAL LANGUAGE (DRL) ............................................................... 16
Retrieve data from one or more tables. ............................................................................... 16
Implementation of different types of operators in SQL. ..................................................... 16
LAB EXPERIMENTS ................................................................................................................... 19
ii
Session 1: introduction to database and Generally
laboratory instruction
A. Introduction to database
This is a laboratory manual for the course Fundamentals of Database.
The content is organized into lessons corresponding to the lecture content
outlined on the curriculum. And the code in this manual is written (edited),
debug and Execute using Microsoft SQL server.
Database: a collection of Logical related data.it is a collection of table
DBMS Software
Microsoft access
Oracle
PostgreSQL
Dbase
SQLite
IBM DB2
Maria DB
Microsoft SQL server
My SQL
In this manual we use software is Microsoft SQL Server
As a final notice, this manual is now ready to be given to the trainees
(students) to help them acquire the necessary skills and understandings,
and then lead them to the level that they would produce software-based
solutions to the miscellaneous societal problems we have today!
1
B.Generally, laboratory instruction
Installing SQL Server software
1. Run Setup.Exe to start the setup of SQL Server Software install,
if prompted, click Yes to allow this app to make changes to your
device.
2. Once the SQL Server Installation Center launches choose
Installation tab (second from the right).
3. In most cases you will want to run a New SQL Server New SQL
Server stand-alone installation, but other options are available,
for example if you have SQL Server 2014 installed, you have an
option to update.
2
Step 2 : License Terms
3
Step 4: Feature Selection
4
Otherwise
5
Step 7: Database Engine Configuration
Step 8: Complete
6
opening and connecting Microsoft SQL server
To open SQL SSMS, use the following steps:
In window 7
1. Start All Programs Microsoft SQL server SQL Server
Management Studio.
Other way in window 10
2. Start All apps Microsoft SQL server SQL Server
Management Studio.
7
Session 2 Data Definition language
Used to define the database structure or Schema
Allows the designers to specify datatype, structures and
constraints on the datatype to be stored in the database
8
Fig 2.1 opening new Query editor window
9
database_name Is the name of the new database. Database names must be
unique within an instance of SQL Server
For example, to create a database with name ‘University, we write the following
statement:
10
Fig 2.4 create database by T-SQL
MODIFYING A DATABASE
Syntax: create database database_name
ALTER DATABASE student MODIFY FILE (NAME = N'test', FILEGROWTH = 2048KB
11
DATA TYPES
Numeric: NUMBER, NUMBER(s,p), INTEGER, INT, FLOAT,
DECIMAL
Character: CHAR(n), VARCHAR(n), VARCHAR2(n), CHAR
VARYING(n)
Boolean: true, false, and null
Date and Time: DATE (YYYY-MM-DD) TIME( HH:MM:SS)
Timestamp: DATE + TIME
USER Defined types
1. CHAR (Size): This data type is used to store character strings values
of fixed length. The size in brackets determines the number of
characters the cell can hold. The maximum number of character is 255
characters.
3. NUMBER (P, S): The NUMBER data type is used to store number (fixed
or floating point).
CREATE SCHEMA
Specifies a new database schema by giving it a name
CREATE TABLE
Specifies a new data base relation by giving it a name, and
specifying each of its attributes and their data types
12
CREATE TABLE <table name> ( <Attribute A1> <Data Type D1>
[< Constraints>], <Attribute A2> <Data Type D2> [< Constraints>],
…….
DROP TABLE
Used to remove a relation (base table) and its definition.
The relation can no longer be used in queries, updates, or any other
commands since its description no longer exists
ALTER TABLE
Used to add an attribute to/from one of the base relations drop
constraint -- The new attribute will have NULLs in all the tuples of the
relation right after the command is executed; hence, the NOT NULL
constraint is not allowed for such an attribute.
The database users must still enter a value for the new attribute JOB
for each EMPLOYEE tuple.
This can be done using the UPDATE command.
13
INSERT UPDATE DELETE TABLES
1. INSERT INTO: This is used to add records into a relation.
These are three type of INSERT INTO queries which are as
a) Inserting a single record
Syntax: INSERT INTO < table name>
(field_1,field_2……field_n)VALUES
(data_1,data_2,........data_n);
Example: insert into school.departement(Did,Dname,Dlocation)
values('d01','Cscince','ethiopia');
14
Example: UPDATE student SET sname = ‘Almaz’ WHERE
sno=1;
15
Session 4 DATA RETRIEVAL LANGUAGE (DRL)
Retrieve data from one or more tables.
1. SELECT FROM: To display all fields for all records.
Syntax : SELECT * FROM relation_name;
ARIHMETIC OPERATORS:
(+): Addition - Adds values on either side of the operator.
(-): Subtraction - Subtracts right hand operand from left hand operand.
(*): Multiplication - Multiplies values on either side of the operator.
(/): Division - Divides left hand operand by right hand operand.
(^): Power- raise to power of.
16
(%): Modulus - Divides left hand operand by right hand operand and returns
remainder.
LOGICAL OPERATORS:
AND: The AND operator allows the existence of multiple conditions in an
SQL statement's WHERE clause.
OR: The OR operator is used to combine multiple conditions in an SQL
statement's WHERE clause.
NOT: The NOT operator reverses the meaning of the logical operator with
which it is used.
Eg: NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.
COMPARISION OPERATORS:
(=): Checks if the values of two operands are equal or not, if yes then
condition becomes true.
(! =): Checks if the values of two operands are equal or not, if values are not
equal then condition becomes true.
(< >): Checks if the values of two operands are equal or not, if values are
not equal then condition becomes true.
(>): Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true
(<): Checks if the value of left operand is less than the value of right
operand, if yes then condition becomes true.
(>=): Checks if the value of left operand is greater than or equal to the value
of right operand, if yes then condition becomes true.
(<=): Checks if the value of left operand is less than or equal to the value of
right operand, if yes then condition becomes true.
SPECIAL OPERATOR:
BETWEEN: The BETWEEN operator is used to search for values that are
within a set of values, given the minimum value and the maximum value.
IS NULL: The NULL operator is used to compare a value with a NULL
attribute value.
ALL: The ALL operator is used to compare a value to all values in another
value set
ANY: The ANY operator is used to compare a value to any applicable value
in the list according to the condition.
17
LIKE: The LIKE operator is used to compare a value to similar values using
wildcard operators. It allows to use percent sign (%) and underscore (_) to
match a given string pattern.
IN: The in operator is used to compare a value to a list of literal values that
have been specified.
EXIST: The EXISTS operator is used to search for the presence of a row in a
specified table that meets certain criteria.
SET OPERATORS:
The Set operator combines the result of 2 queries into a single result. The
following are the operators:
Union
Union all
Intersect
Minus
Union: Returns all distinct rows selected by both the queries
Union all: Returns all rows selected by either query including the
duplicates.
Intersect: Returns rows selected that are common to both queries.
Minus: Returns all distinct rows selected by the first query and are not by
the second
18
LAB EXPERIMENTS
Question
1.Create database called “MAU_university”.
Solution
Department table 1
19
create table school.departement(Did char(8) primary key not
null,
Dname varchar(30) unique not null,Dlocation varchar(30));
Student table 2
Column Data type Size Constraint
Sid Char 10 not null
Fname Varchar 30 not null
Lname Varchar 30 not null
Sex 1 1 Default ‘f’, must be either ‘f’ or ’M’
Year_ofstudy Int not null
Dbirth Date
Age integer Computed or derived from Dbirth and
current date
sem_payment Decimal (6,2)
Paid Char Computed or derived from sem_payment
Did Char 8 FK
20
Course table 3
Column Data type Size Constraint
Cno char 10 Not null
Cname varchar 20 Unique,Not null
Chour int Must be Chour B/n 2
and 4
Section table 4
Column Data type Size Constraint
section_id Int,identity (10,1) PK, not null
Cno FK
acadamic_year int Date Default year current
year,Must be b/n 2015 and
2018
Instructor varchar 10
21
Gred_Report table 4
22
23