Professional Documents
Culture Documents
Laboratory Manual
for
Fundamentals of Database
Table of Contents
Session 1: introduction to database and Generally laboratory instruction ......................................... 1
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) ............................................................................. 12
INSERT UPDATE DELETE TABLES .......................................................................................................... 13
Session 4 Retrieve data from one or more tables. ................................................................................ 14
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.
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
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 Microsoft SQL server Software
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
For example, to create a database with name ‘University, we write the following
statement:
CREATE DATABASE University
1. Expand your database Right Click on Tables and specify columns with their data types
9
2.Enter database name Fig 2.3 create database by GUI
Syntax: create database database_name
CREATE DATABASE MAU_University
MODIFYING A DATABASE
Syntax: create database database_name
ALTER DATABASE student MODIFY FILE (NAME = N'test', FILEGROWTH = 2048KB
10
We can drop a database either by right clicking the database and
pressing Delete on the context menu or using the following Drop
Syntax: DROP DATABASE <databaseName>
Example: DROP DATABASE Mau_university
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
Syntax of CREATE Command:
11
CREATE TABLE <table name> ( <Attribute A1> <Data Type D1>
[< Constraints>], <Attribute A2> <Data Type D2> [< Constraints>],
…….
<Attribute An> <Data Type Dn> [< 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
Syntax: DROP TABLE tabl_name;
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.
Syntax: ALTER TABLE tabl_name ADD COLUMN_NAME DATATYPE (SAIZE);
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.
12
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');
13
3. DELETE-FROM: This is used to delete all the records of a
relation but it will retain the structure of that relation.
14
Example: select FNAME,LNAME from student
3. SELECT - FROM -WHERE: This query is used to display a selected set
of fields for a selected set of records of a relation.
Syntax: SELECT a set of fields FROM relation_name WHERE condition;
Exercise 1
1.Create database called “MAU_university”.
15
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
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
16
create table course(Cno char (10) primary key not null,
Cname varchar (20) unique not null,Chour int check (Chour>=2
and Chour <=4),
Did char(8) foreign key references school.departement(Did));
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
Gred_Report table 4
17
grade char check(grade in('a','b','c','d','f')),remark as
case when(grade='a') then'Execelent'
when(grade='b') then'Very good'when(grade='c')
then'good'when(grade='d') then'poor'
when(grade='f') then'f'else 'invalid'end);
prerequast table 5
Column Data type Size Constraint
Cno char 10 Not null
Prno char 10 Not null
18
19