0% found this document useful (0 votes)
27 views

Database Lab Manual1

database lb manual

Uploaded by

waseem04002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Database Lab Manual1

database lb manual

Uploaded by

waseem04002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Lab manual

Database

Name:
Muhammad kamran
Section:
A
Submitted to:
Ms.kanza Hanif
Subject
Database
Roll No:
2023-CS-41

Muhammad Nawaz Sharif University of Engineering


&Technology
Database:
A database is an organized collection of structured information, or data, typically stored electronically
in a computer system which database is usually controlled by a database management system
(DBMS).
Here in database we will discuss about My sql

MySQL:

i. MySQL is a widely used relational database management system (RDBMS).

ii. MySQL is free and open-source.

iii. MySQL is ideal for both small and large applications.

Software used:
The software which is used is MY SQL command line. By using this software the database will be
created in this syntyx. It contain some queries for creating and utilizind database.

Create a database:
For creating database the query is

Create database school;

Show databases:
For show database the query is

Show database;

Use any database:


For show database the query is

Use univerisity

Tables:
A table is a structured collection of data organized into rows and columns. Each row represents a
record, and each column represents a specific attribute or field of that record. Tables are fundamental
components of relational databases and are used to store, manage, and manipulate data.

Table Structure:
i. Each table has a name that uniquely identifies it within the database.
ii. Tables consist of columns (also known as fields) and rows (also known as records or tuples).
iii. Columns have a data type that defines the kind of data that can be stored in them (e.g.,
INTEGER, VARCHAR, DATE).
iv. You can also specify additional properties for columns such as NULL/NOT NULL constraints,
default values, and auto-increment for primary keys.

Primary Key:
i. A primary key uniquely identifies each record in the table.
ii. It ensures that each row in the table can be uniquely identified.
iii. Primary keys are often created on one or more columns that have unique values and are not
null.

Foreign Key:
i. A foreign key establishes a relationship between two tables.
ii. It is a column or a set of columns whose values must correspond to the values of a primary
key in another table.
iii. Foreign keys enforce referential integrity, ensuring that relationships between tables are
maintained.

Data Manipulation:
i. You can perform various operations on tables, such as inserting new records, updating
existing records, deleting records, and querying data using SELECT statements.
ii. SQL (Structured Query Language) is used to interact with tables in MySQL.

Indexes:
i. Indexes can be created on one or more columns of a table to improve the performance of
data retrieval operations (e.g., searching, sorting).
ii. They allow MySQL to quickly locate rows based on the indexed columns.

Constraints:
i. Constraints are rules enforced on the data in a table to maintain data integrity.
ii. Common constraints include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK,
etc.
ER Model of Univerisity Database

Program Name
id Duration
Semester Title id
Degree Program id Student

Credit
Course Grade Points
Degree
id Earned
type
Department Letter Cgpa
Name id
Grade
Name

Location Student

Chairperson Due Date Email


id
id Univerisity
student
Proffessor
Assignment

Name
Department
Description
Staff id

Date Assigned
Staff Name

Building
Edition
Salary

Type Class room Year


Textbook Position

Hire Date
Publisher
Capacity
Author
Room number
ISBN Title
Create table
For creating table the query used is

Such as student info

Roll no Name F.Name Class Phone Address subject Section

Create tablel student_info(Roll No int primary key not null,

Name varchar(20) not null,

F.Name varchar(20) not null,

Class varchar(20) not null,

Phone varchar(20) not null,

Address varchar(20) not null,

Section varchar(20) not null,

Section varchar(20));

Describe Table:
For describing table such as the data type of column,primary key or varchar length.This query is used

Describe Student_info;

SQL DELETE Statement

DELETE FROM table_name

WHERE some_condition;

Example Query

DELETE FROM GFG_Employees WHERE NAME = 'waseem';

ALTER (RENAME) in SQL

ALTER TABLE table_name

RENAME TO new_table_name;
Example Query:
CREATE TABLE Assignment (
Assignment_id INT PRIMARY KEY,
Due_date VARCHAR(50),
student VARCHAR (10)
Description VARCHAR(100)
);

CREATE TABLE Classroom (


Room_no INT PRIMARY KEY,
Building type VARCHAR(10),
Type VARCHAR (10)
Capacity VARCHAR(100)
);

CREATE TABLE course (


Course_id INT PRIMARY KEY,
Title VARCHAR(50),
Credit VARCHAR (10)
Semester VARCHAR(100)
);
CREATE TABLE degree_program (
program_id INT PRIMARY KEY,
duration VARCHAR(10),
Department VARCHAR (10)
Degree_type VARCHAR(100)
);

CREATE TABLE department (


department_id INT PRIMARY KEY,
name VARCHAR(100),
Chairperson VARCHAR (100)
Location VARCHAR(100)
);

CREATE TABLE grade (


Student_id INT PRIMARY KEY,
points earned VARCHAR(50),
points_earned VARCHAR (10)
);
CREATE TABLE proffessor (
proffessor_id_id INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR (10)
);

CREATE TABLE staff (


Staff_id INT PRIMARY KEY,
Name VARCHAR(50),
Hire_date VARCHAR (10)
Position VARCHAR(100)

salary VARCHAR(100)
);

CREATE TABLE student (


student_id INT PRIMARY KEY,
Name VARCHAR(50),
CGPA VARCHAR (10)
Email VARCHAR(100)
);
CREATE TABLE textboo; (
ISBN INT PRIMARY KEY,
Title VARCHAR(50),
Edition VARCHAR (10)
Author VARCHAR(100)

Publisher VARCHAR(100)
);

Sql Select statement:


The sql select statement is used to show the specific data of table

Select* From assignmrnt;


This is also used to select the specific record such as

Select*from assignment where Assignment_id=6

Insert Record in table:


Here I am using my created database data in it by using this syntyx.

insert into assignment (Assignment_id,Due_Date,Stuent,Description)values(1,'01/10/2024','ali','Web'),


(2,'01/10/2024','Abrahim','physics'),(3,'01/10/2024','hamza','chemsitry'),(4,'01/10/2024','waseem','Biolo
gy'),(5,'01/10/2024','waqas','C++'),(6,'01/10/2024','Rahim','OPP'),(7,'01/10/2024','Ahmad','java'),(8,'01/
10/2024','imran','javascript'),(9,'01/10/2024','Mudassir','physics'),(10,'01/10/2024','Muzammil','physics')
;

Delete a database or table

We use the drop query to delete that database or table such as

Drop database school;

Update Query
The update query is used to update the database or table or the data wiwthin these.

Such as

My Assignment:
Univerisity Database

Assignment table:
insert into assignment (Assignment_id,Due_Date,Stuent,Description)values(1,'01/10/2024','ali','Web'),
(2,'01/10/2024','Abrahim','physics'),(3,'01/10/2024','hamza','chemsitry'),(4,'01/10/2024','waseem','Biolo
gy'),(5,'01/10/2024','waqas','C++'),(6,'01/10/2024','Rahim','OPP'),(7,'01/10/2024','Ahmad','java'),(8,'01/
10/2024','imran','javascript'),(9,'01/10/2024','Mudassir','physics'),(10,'01/10/2024','Muzammil','physics')
;
Classroom Table:
insert into classroom
(Room_No,Building_type,Type,Capacity)values(1,'good','smooth','upto_50'),(2,'good','beautiful','upto_
60'),(3,'good','smooth','upto_50'),(4,'good','smooth','upto_70'),(5,'high','smooth','upto_80'),(6,'good','nor
mal','upto_40'),(7,'good','smooth','upto_80'),(8,'high','smooth','upto_50'),(9,'good','smooth','upto_90'),(1
0,'good','smooth','upto_80');

Course Table:
insert into course(course_id,Title,credits,semester)values(1,'wrenmartin','3 hours','1st'),(2,'c++','4
hours','1st'),(3,'database','3 hours','2nd'),(4,'','3 hours','1st'),(5,'DLD','3 hours','2nd'),(6,'islamstudy','4
hours','3rd'),(7,'pakstudy','3 hours','1st'),(8,'calculus','3 hours','1st'),(9,'physics','4
hours','3rd'),(10,'OOP','3 hours','1st');
Degree Program Table:
insert into degree_program(program_id,Duration,Department,degree_type)values(1, '4', 'CS',
'Bachelor'),(2, '4', 'EEE', 'Bachelor'),(3, '4', 'MECH', 'Bachelor'),(4, '2', 'BUS', 'Master'),(5, '5', 'PSY',
'Ph.D.'),(6, '4', 'ENG', 'Bachelor'),(7, '4', 'NUR', 'Bachelor'),(8, '2', 'CEE', 'Master'),(9, '2', 'ENG',
'Master'),(10, '4', 'MECH', 'Bachelor');

Department Table:
insert into Department (department_id, Name, Chairperson, Location)values (1, 'Computer Science
and Engineering', 'Dr. John Smith', 'Building A, Room 101'), (2, 'Electrical and Electronics
Engineering', 'Dr. Emily Johnson', 'Building B, Room 202'),(3, 'Mechanical Engineering', 'Dr. Robert
Williams', 'Building C, Room 303'),(4, 'Business Administration', 'Dr. Sarah Davis', 'Building D, Room
404'), (5, 'Psychology', 'Dr. Michael Brown', 'Building E, Room 505'),(6, 'English', 'Dr. Jessica Wilson',
'Building F, Room 606'),(7, 'Nursing', 'Dr. Jennifer Martinez', 'Building G, Room 707'),(8, 'Civil and
Environmental Engineering', 'Dr. David Jones', 'Building H, Room 808'), (9, 'Mathematics', 'Dr.
Christopher Lee', 'Building I, Room 909'),(10, 'Chemistry', 'Dr. Amanda Thompson', 'Building J, Room
1010');
Grade Table:
insert into grade (student_id, points_earned, letter_grade) values (1, '95.5', 'A'),(2, '87.2', 'B'), (3,
'78.9', 'C'), (4, '64.3', 'D'), (5, '91.8', 'A'),(6, '76.5', 'C'),(7, '82.1', 'B'),(8, '69.4', 'D'),(9, '90.0', 'A'), (10,
'85.7', 'B');

Professor Table:
INSERT INTO Professor (professor_id, name, department)VALUES (1, 'Dr. John Smith', 'Computer
Science and Engineering'),(2, 'Dr. Emily Johnson', 'Electrical and Electronics Engineering'),(3, 'Dr.
Robert Williams', 'Mechanical Engineering'),(4, 'Dr. Sarah Davis', 'Business Administration'), (5, 'Dr.
Michael Brown', 'Psychology'),(6, 'Dr. Jessica Wilson', 'English'), (7, 'Dr. Jennifer Martinez', 'Nursing'),
(8, 'Dr. David Jones', 'Civil and Environmental Engineering'), (9, 'Dr. Christopher Lee',
'Mathematics'),(10, 'Dr. Amanda Thompson', 'Chemistry');

Staff Table:
insert into staff (staff_id, name, hire_date, salary, position) values(1, 'john doe', '2020-05-15',
50000.00, 'manager'),(2, 'jane smith', '2021-02-10', 45000.00, 'assistant manager'),(3, 'michael
johnson', '2019-09-20', 60000.00, 'senior developer'),(4, 'emily brown', '2022-01-05', 55000.00,
'marketing coordinator'),(5, 'david wilson', '2018-11-30', 52000.00, 'sales representative'),(6, 'sarah
lee', '2020-08-25', 48000.00, 'hr specialist'),(7, 'robert garcia', '2021-03-12', 58000.00, 'project
manager'),(8, 'laura martinez', '2019-06-18', 51000.00, 'financial analyst'),(9, 'daniel thompson', '2022-
02-28', 53000.00, 'customer service representative'),(10, 'jennifer taylor', '2020-04-10', 49000.00,
'administrative assistant');

Student Table:
insert into student (studentid, name, cgpa, email)values (1, 'john doe', '3.75',
'[email protected]'),(2, 'jane smith', '3.82', '[email protected]'),(3, 'alice johnson', '3.95',
'[email protected]'),(4, 'michael brown', '3.60', '[email protected]'),(5, 'emily
davis', '3.90', '[email protected]'), (6, 'william wilson', '3.88',
'[email protected]'),(7, 'sarah martinez', '3.77', '[email protected]'),(8, 'david
anderson', '3.70', '[email protected]'), (9, 'jessica taylor', '3.65',
'[email protected]'), (10, 'daniel thomas', '3.80', '[email protected]');

Textbook Table:
insert into textbook (isbn, title, edition, author, publisher) values('9780134854196', 'introduction to
algorithms', '4th edition', 'cormen, thomas h.', 'mit press'),('9780262533058', 'the art of computer
programming', 'volume 1: fundamental algorithms', 'knuth, donald e.', 'addison-
wesley'),('9780321856715', 'clean code: a handbook of agile software craftsmanship', '1st edition',
'martin, robert c.', 'prentice hall'),('9780132350884', 'the c programming language', '2nd edition',
'kernighan, brian w.', 'prentice hall'),('9780262033848', 'structure and interpretation of computer
programs', '2nd edition', 'abelson, harold', 'mit press'),('1234567890123', 'advanced mathematics',
'advanced edition', 'fermat, pierre', 'mathematics pub'),('1234567890124', 'introduction to physics',
'physics primer', 'einstein, albert', 'physics assoc'),('1234567890125', 'history of literature', 'historical
edition', 'shakespeare, william', 'literature pre'),('1234567890126', 'principles of chemistry', 'chemistry
essentials', 'bohr, niels', 'chemistry boo'),('1234567890127', 'introduction to psychology', 'psychology
basics', 'freud, sigmund', 'psychology pub');
Join in my sql:
select*from staff inner join student on student.student_id = staff.Staff_id;

Left Join:
It return all the record from left table amd matched the record from the right table.

Query:
select student.Student_id,student.Name,student.CGPA from student left join staff on
student.Name=staff.Name;
Right join:
The right join returns all the record from right table and match the record from the left table.

Query:
select assignment.Assignment_id,assignment.Due_date,assignment.student ,assignment.Description
from assignment right join classroom on assignment.Assignment_id=classroom.Room_no;

Cross join:
A CROSS JOIN produces the Cartesian product of two tables. This means every row from the first
table is combined with every row from the second table. The result set includes all possible
combinations of rows from the two tables.

Query:
select assignment.Assignment_id,assignment.Due_date,assignment.student ,assignment.Description
from assignment cross join classroom on assignment.Assignment_id=classroom.Room_no;
Join Multiple Tables:
We can join multiple table with each other such as

Query:
select assignment.Assignment_id,assignment.Due_date,assignment.student ,assignment.Description
from assignment cross join classroom on assignment.Assignment_id=classroom.Room_no inner join
degree_program on assignment.Assignment_id=program_id;

ALTER TABLE Classroom

ADD Assignment_id INT;

ALTER TABLE Classroom

ADD CONSTRAINT fk_assignment

FOREIGN KEY (Assignment_id)

REFERENCES Assignment (Assignment_id);


CREATE TABLE emp (

eno INT PRIMARY KEY,

ename VARCHAR(255),

edate DATE,

title VARCHAR(255),

salary DECIMAL(10, 2),

dno INT,

FOREIGN KEY (dno) REFERENCES Department(Dno)

);

CREATE TABLE proj (

pno INT PRIMARY KEY,pname VARCHAR(255),budget DECIMAL(10, 2),dno


INT,FOREIGN KEY (dno) REFERENCES Department(dno));

You might also like