You are on page 1of 21

MEKDELA AMBA UNIVERSITY

Collage of Natural resource and computational science


DEPARTEMENT OF COMPUTER SCINCE

Laboratory Manual
for
Fundamentals of Database

Prepared by: Abera Brhanu (BSc.)


Contents

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.

Database: a collection of Logical related data.it is a collection of table


Database Management Systems (DBMS): a computerized system that
system enables user to create and maintain a database.
The DBMS is a general-purpose software system that facilitate of defining,
constructing and sharing database users and applications.

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.

Step 1: New SQL stand-alone installation

2
Step 2 : License Terms

Step 3: Install Rules

3
Step 4: Feature Selection

Step 5: Instance Configuration

4
Otherwise

Step 6: Server Configuration

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.

Click on Connect to your default instance as shown in the


following figure

Fig 1 connecting SQL server

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

CREATING, MODIFYING AND Dropping DATABASES


CREATING A DATABASE IN GRAPHICAL USER INTERFACE

1. Start SQL Server Management Studio by selecting Start


Programs  Microsoft SQL server 2012 Management
Studio.
2. Click on Connect to your default instance of SQL Server as
in Figure 1.1
3. Open Query editor window
4. Expand your Databases folder.
5. Right-click either the Databases folder in the console tree or
the white space in the right pane, and choose New
Database from the context menu.
6. You should now see the General tab of the Database
properties sheet. Enter the database name, and
leave the owner as <default>.

8
Fig 2.1 opening new Query editor window

Fig 2.2 create New database


CREATING A DATABASE IN TRANSACT SQL STATEMENT(T-SQL)

1. Start SQL Server Management Studio by selecting Start


Programs  Microsoft SQL server 2012 Management
Studio.
2. Click on Connect to your default instance of SQL Server as
in Figure 1.1
3. Open Query editor window as in Figure 2.1
4. Write the database creation statement
5. Execute the statement

Syntax: CREATE DATABASE database_name

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:
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

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

Alter database database_name modify file (name=’new name’


filegrowth=2048 kb
Dropping A DATABASE

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

Session 3 DATA DEFINITION, CONSTRAINTS,AND SCHEMA CHANGES

CREATING, MODIFYING AND DELETING TABLES


table…………………………………….……relation
row……………………………………..…….tuple
column………………………………….……attribute
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

CREATE SCHEMA
Specifies a new database schema by giving it a name

Syntax: create schema Schema_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;

DROP A COLUMN AN ATTRIBUTE


Syntax: DROP TABLE tabl_name DROP COLUMN COLUMN_NAME

Example: alter table grade_report drop column result

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.

Session 3 DATA MANIPULATION LANGUAGE (DML)


The Data Manipulation Language (DML) is used to retrieve, insert and
modify database information. These commands will be used by all
database users during the routine operation of the database. Let's take
a brief look at the basic DML commands:

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');

b) Inserting a single record


Syntax: INSERT INTO < table name>VALUES
(data_1,data_2,........data_n); Example:
insert into school.departement values('d01','Cscince','ethiopia');

c) Inserting all records from another relation


Syntax: INSERT INTO relation_name_1 SELECT
Field_1,field_2,field_n FROM relation_name_2 WHERE
field_x=data;
Example: insert into school.departement SELECT Dname,Dlocation
FROM department WHERE NAME=’Almaz’;

2.UPDATE-SET-WHERE: This is used to update the content


of a record in a relation.
Syntax: UPDATE table_name SET
column_name1=data,column_name2=data, WHERE
column_name=data;
Example: UPDATE student SET sname = ‘Almaz’ WHERE
sno=1;

13
3. DELETE-FROM: This is used to delete all the records of a
relation but it will retain the structure of that relation.

a) DELETE-FROM: This is used to delete all the records of relation.


Syntax: SQL>DELETE FROM table_name;

Example: delete from student


b) DELETE -FROM-WHERE: This is used to delete a selecte record from
a relation.

Syntax: DELETE FROM relation_name WHERE condition;


Example: delete from student where sno=14
4.TRUNCATE: This command will remove the data permanently. But
structure will not be removed.
Difference between Truncate & Delete:-
 By using truncate command data will be removed permanently & will
not get back where as by using delete command data will be removed
temporally & get back by using roll back command.
 By using delete command data will be removed based on the
condition where as by using truncate command there is no condition.
 Truncate is a DDL command & delete is a DML command.

Syntax: TRUNCATE TABLE <Table name>

Example : TRUNCATE TABLE STUDENT

Session 4 Retrieve data from one or more tables.


1. SELECT FROM: To display all fields for all records.
Syntax : SELECT * FROM relation_name;

Example : select *from student

2. SELECT FROM: To display a set of fields for all records of relation.


Syntax: SELECT a set of fields FROM relation_name;

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;

Example: select * from student WHERE SNO=15

Exercise 1
1.Create database called “MAU_university”.

Syntax: create database Database_Name


create database MAU_university

2.create schema called “School” under this database.

Syntax: create schema Schema_Name

create schema school


3.create “department,student,course” table inside “school” schema
under “university”
Department table 1
Column Data type Size Constraint
Did Char 8 Pk,Not null
Dname Varchar 30 Unique,not null
Dlocation Varchar 30

Syntax: create table schema.table_Name(column_name


Datatype(size) constraint, column_Name1 Datatype(size)
constraint ... column Name N Datatype(size) constraint)

create table school.departement(Did char(8) primary key not


null,
Dname varchar(30) unique not null,Dlocation varchar(30));

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

create table student(Sid char(10) primary key not


null,Fname varchar(30) not null,
Lname varchar(30)not null,
sex char(1) default 'f' check(sex='f' or sex='m'),
--check(sex in('f' or 'm'))
Year_ofstudy int not null,Dbirth date,age as
datediff(year,Dbirth,getdate()),
--as year(getdat())-year(Dbirth)
sem_payment decimal(6,2),
paid as case when(sem_payment is not null)then 'yes'
when(sem_payment is null)then 'no'end,
Did char(8) foreign key references school.departement(Did));

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

create table section(section_id int identity(10,1) primary


key not null,
Cno char(10) not null foreign key references
course(Cno),semester char(4) not null ,
acadamic_year int check(acadamic_year >=2015 and
acadamic_year <=2018)default year(getdate()),
instructur varchar(10));

Gred_Report table 4

Column Data type Size Constraint


Sid Char 8 Not null
section_id Int Not null
grade char

create table gred_report(Sid char (8) not null,section_id


int not null,
constraint gred_pk primary key(Sid,section_id),
constraint gred_fk1 foreign key(Sid)references student(Sid),
constraint gred_fk2 foreign key(section_id)references
section(section_id));

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

create table prerequast(Cno char(10) not null,


prno char(10) not null,
constraint pr_pk primary key(Cno,Prno),
constraint pr_fk foreign key(Cno) references course(Cno));

18
19

You might also like