TRIBHUVAN UNIVERSITY
MAHENDRA MORANG ADARSHA MULTIPLE CAMPUS
BIRATNAGAR
A Lab Report on
IT 232: Database Management System
(BBA)
Submitted by
Ram Prasad Pathak
Symbol No: 37093/23
Semester: II
BBA
Submitted to:
Department of Management
Mahendra Morang Adarsh Multiple Campus
Biratnagar
2081
1
Lab 1
Title: Implementation of DDL commands of SQL with suitable examples
CREATE database and Table
ALTER table
DROP table
Objectives:
To understand/use of DDL statements to write query and for database
CREATE database statement:
The CREATE DATABASE statement is used to create a new SQL database.
Syntax:
CREATE DATABASE Database_name;
Example:
CREATE DATABASE BIT;
Note: BIT database is created.
Now, to change the database USE statement is used.
Example:
USE BIT;
Creating a Table
The CREATE TABLE statement is used to create a new table in a database.
Syntax:
CREATE TABLE TableName (columnName1 datatype, columnName2
datatype, ..................);
Example:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
DateOfBirth DATE,
Major VARCHAR(50));
ALTER table:
The ALTER TABLE statement is used to add/delete some extra fields in an
existing table.
Syntax:
ALTER TABLE table_name ADD column_name data_type
Example:
ALTER TABLE Student ADD address varchar(20);
Note: Adds an "address" column to the "Student" table.
To delete a column in a table:
ALTER TABLE Student DROP Column email;
Note: Deletes an email column from the Student table
2
To rename the column:
ALTER TABLE Students
RENAME COLUMN FirstName TO GivenName;
This statement renames the FirstName column to GivenName in the Students
table.
DROP table:
It is used to delete the structure of the relation. It permanently deletes the records
in the table.
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE Student;
Lab 2
Title: Implementation of DML commands of SQL with suitable examples
INSERT statement
UPDATE statement
DELETE statement
Objectives:
To understand and use of Data Manipulation Language to query, update and manage a
database.
INSERT Command:
This command is used to insert the data in a table.
Syntax:
INSERT INTO tablename values (value1, value2…….);
Example:
INSERT INTO employee values(101, ‘pop’, ‘Brt’, 5000);
Note: If we want to insert multiple records then we write code like this:
INSERT INTO employee values(102, ’bob’, ’ktm’, 7000), (103, ’harry’, ’brt’,
12300), (104, ’khem’, ’brt’, 45000);
The above code will insert three records at a time
3
DELETE Statement:
The DELETE statement is used to delete rows or records in a table. Users can use
the WHERE clause with DELETE query to delete selected rows; otherwise, all the
records would be deleted.
Syntax:
DELETE FROM tableName WHERE [condition];
Example :
DELETE FROM Students WHERE StudentID = 1;
Note: If we want to remove all rows, then:
DELETE FROM Student;
Note: Delete Rows with Multiple Conditions
DELETE FROM Students
WHERE Major = 'Computer Science' AND DateOfBirth < '2000-01-01';
UPDATE statement:
The UPDATE statement is used to update existing records in a table. You can use
the WHERE clause with UPDATE query to update selected rows; otherwise, all
the rows would be affected.
Syntax:
UPDATE tableName SET columnName = value WHERE condition;
Example 1:
UPDATE Student SET age=16 WHERE sid=12;
Example 2:
UPDATE Student SET s_name='Muna', age=23 WHERE sid=03;
Example 3:
UPDATE emp SET salary=salary+(salary*10/100);
Lab 3
Title: Implementation of DML commands and DDL commands of SQL with suitable
examples
Objectives:
To understand and use DML and DDL commands to manage a database.
a. Write SQL Query to create the following table (Student).
Redgno Name Department
01 Ram BBA
02 Bir BBM
03 Pratik BIT
04
4 shyam BCA
b. Write a SQL command which will show the entire STUDENT table.
c. Write down the SQL command which will show the Regdno of Pratik.
d. Write down the SQL command which will show the Name and Department column.
e. Add another column in the STUDENT table as “address”.
a.
CREATE TABLE STUDENT (
Redgno INT PRIMARY KEY NOT NULL,
Name VARCHAR(20), Branch VARCHAR(20));
To insert records:
INSERT INTO STUDENT Values(01, ’Ram’, ’BBA’), (02, ’Bir’, ’BBM’), (03, ’Pratik’,
’BIT’), (04, ’Shyam’, ’BCA’);
b. Write a SQL command which will show the entire STUDENT table.
SELECT * FROM STUDENT;
c. Write down the SQL command which will show the Regdno of Pradeep.
SELECT Regdno from STUDENT WHERE name=‘Pratik’;
d. Write down the SQL command which will show the Name and Department column.
SELECT Name, Department FROM STUDENT;
e. Add another column in the STUDENT table as “address”.
ALTER table STUDENT ADD Address VARCHAR(20);
Lab 4:
Write DDL statements to create a table for entity customer (cid, name, age, address):
cid: stores integers value and is unique
name: stores maximum of 30 characters
dob: stores date and time value
address: stores at most 30 characters and only Brt, Ithari, and Damak can be
assigned
Solution:
CREATE TABLE Customer (
cid INT PRIMARY KEY,
name VARCHAR(30),
dob DATETIME,
address VARCHAR(30) CHECK (address IN ('Brt', 'Ithari', 'Damak')));
Lab 5:
Write DDL statements for the following table ‘Student’:
Sid: Character, Primary Key
5
Name: Character, Not null
Login: Character, UNIQUE
Age: Integer, Default 18 yrs
GPA: Real, Between 0 and 4
Solution:
CREATE table student (
sid char PRIMARY KEY,
name char not null,
login char UNIQUE,
age int DEFAULT 18,
gpa real CHECK (GPA Between 0 and 4));
Lab 6:
Write the SQL statement to construct the following tables and relationships:
Author (aid, name, age, gender, address)
Writes (aid, bid, publishdate, bookname, ISBN)
Constraints for Author Table:
Name should not be more than 20 characters long
Age should be more than 10
Default gender should be ‘unknown’
Constraints for Writes Table:
ISBN cannot be null
Solution:
CREATE table Author (
aid int primary key,
name varchar(20),
age int,
gender varchar(20) default 'unknown',
address varchar(20) CHECK(age>10));
CREATE table Writes (
bid int PRIMARY KEY,
publishdate datetime,
bookname varchar(20),
ISBN int not null,
aid int,
foreign key(aid)
references Author(aid));
Lab 7:
Write DDL statements for the following:
Table Name: Employees
6
Empid: Varchar, Primary key
Empname: Varchar, Not null
Gender: Varchar, Value must be “M”, “F” or “O”
Age: Number, Age must be >16
Solution:
Create table Employees (
Empid varchar(20) primary key,
Empname varchar(20) not null,
Gender varchar CHECK(gender='M' or gender='F' or gender='O'),
Age int CHECK(age>16));
Or
Create table Employees (
Empid varchar(20) primary key,
Empname varchar(20) not null,
Gender enum('m', 'f', 'o') not null,
Age int CHECK(age>16));