Mekdela Amba University
Mekdela Amba University
Mekdela Amba University
Laboratory Manual
for
Fundamentals of Database
Contents
Table of Contents
Session 1: introduction to database and Generally laboratory instruction............................3
Session 2 Data Definition language.............................................................................................5
A. CREATING, MODIFYING AND Dropping DATABASES.........................................................5
Session 3 DATA DEFINITION, CONSTRAINTS, AND SCHEMA CHANGES....................................8
CREATING, MODIFYING AND DELETING TABLES......................................................................8
Session 3 DATA MANIPULATION LANGUAGE (DML)..................................................................9
INSERT UPDATE DELETE TABLES...............................................................................................9
ii
Prepared by: Abera Brhanu (BSc.) 2013 E.C
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!
Prepared by: Abera Brhanu (BSc.) 2013 E.C
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
Prepared by: Abera Brhanu (BSc.) 2013 E.C
MODIFYING A DATABASE
Syntax: create database database_name
ALTER DATABASE student MODIFY FILE (NAME = N'test', FILEGROWTH = 2048KB
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:
Prepared by: Abera Brhanu (BSc.) 2013 E.C
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.
3. DELETE-FROM: This is used to delete all the records of a relation but it will
retain the structure of that relation.
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.
Exercise 1
1.Create database called “MAU_university”.
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
Prepared by: Abera Brhanu (BSc.) 2013 E.C
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
instructur varchar(10));
Gred_Report table 4