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

Intro to DB_6 lab 06 umer

Uploaded by

Ibad Hussain
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)
5 views

Intro to DB_6 lab 06 umer

Uploaded by

Ibad Hussain
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/ 9

Lab No 6

CSCL 2203 Database Systems

Lab#6

Objective:

-Creating and managing tables.

Name of Student: _____________________________________________

Roll No: ______________________________Sec. ___________

Date of Experiment: ___________________________________________

Marks Obtained/Remarks: _____________________________

Signature: _____________________________

1
Lab No 6

EXERCISES
Consider the following schema, in the form of normalized relations, to represent information
about employees, grades, training and projects in an organization.

EMPLOYEE GRADE
Empno (eg 6712) Designation
Name Grade (1-20)
Designation (e.g. Database Developer) TotalPosts
Qualification PostsAvailable (<= TotalPosts)
Joindate
PROJECT TRAINING
PID (eg P812) Tcode (eg T902)
Title Title
Client StartDate
Duration (in weeks) EndDate
Status (New, In Progress, Complete)
EMP_PROJECT EMP_TRAINING
Empno Empno
PID Tcode
Performance (Excellent, Good, Fair, Bad, Poor) Attendance (%)

1. Develop a script file EMPLOYEE.SQL to create tables for the above schema. Implement
all necessary integrity constraints including primary and foreign keys. (NOTE: All check
constraints should be at table level)

EMPLOYEE TABLE

2
Lab No 6

GRADE TABLE

PROJECT TABLE

3
Lab No 6

TRAINING TABLE

EMP_PROJECT TABLE

4
Lab No 6

EMP_TRAINING TABLE

2. Write SQL statements to add


o Gender column to EMP table. The only possible values are Male and Female.
o Instructor_Name column to TRAINING table.
o Salary column to GRADE table.

5
Lab No 6

3. What is database schema? What are the different objects included in it?

A database schema is the logical structure or blueprint of a database that defines how data is
organized and how the relationships between data are managed. It includes a set of rules and
constraints that specify the organization of tables, columns, keys, indexes, and other elements
within the database. The schema provides a detailed framework for how the database operates and
serves as a guide for developers and database administrators.

It does not include the actual data but focuses on the metadata and structure.

Objects Included in a Database Schema


A database schema can include several objects, such as:

1. Tables

● Definition: The primary structure in a schema where data is stored in rows and columns.

Example:
CREATE TABLE Employee (
Emp_ID INT PRIMARY KEY,
FullName VARCHAR(150),
JobTitle VARCHAR(100)
);

2. Views

● Definition: A virtual table that is based on the result set of a query. It does not store data
itself but provides a way to access or aggregate data from one or more tables.

Example:

CREATE VIEW ActiveEmployees AS


SELECT FullName, JobTitle FROM Employee WHERE Status = 'Active';

3. Indexes

● Definition: Used to improve the speed of data retrieval by creating quick access paths to
data in tables.

Example:
CREATE INDEX idx_emp_fullname ON Employee (FullName);

6
Lab No 6

4. Keys

● Types:
○ Primary Key: Ensures that each row in a table is unique.
○ Foreign Key: Establishes a relationship between two tables.
○ Unique Key: Ensures that values in a specific column are unique.

Example:

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
Emp_ID INT,
FOREIGN KEY (Emp_ID) REFERENCES Employee(Emp_ID)
);

5. Constraints

● Definition: Rules applied to table columns to ensure data integrity.


● Types:
○ NOT NULL: Prevents null values.
○ CHECK: Ensures values meet specific conditions.
○ DEFAULT: Assigns a default value.

Example:

CREATE TABLE Product (


ProductID INT PRIMARY KEY,
Price DECIMAL(10, 2) CHECK (Price > 0)
);

6. Stored Procedures

● Definition: Precompiled SQL code that can be executed as a single unit to perform
operations like insert, update, or calculations.

Example:

CREATE PROCEDURE GetEmployeeDetails()


BEGIN
SELECT * FROM Employee;
END;

7
Lab No 6

7. Functions

● Definition: Similar to stored procedures but returns a single value and is used for
calculations.

Example:

CREATE FUNCTION GetTotalEmployees() RETURNS INT


BEGIN
RETURN (SELECT COUNT(*) FROM Employee);
END;

8. Triggers

● Definition: Code that automatically executes in response to certain events (e.g., insert,
update, delete) in a table.

Example:

CREATE TRIGGER AfterInsert


AFTER INSERT ON Employee
FOR EACH ROW
BEGIN
INSERT INTO AuditLog (Action, TimeStamp) VALUES ('Insert',
NOW());
END;

9. Sequences

● Definition: Generates unique numeric values, often used for auto-incrementing primary
keys.

Example:

CREATE SEQUENCE EmployeeID_Seq START WITH 1 INCREMENT BY 1;

10. Synonyms

● Definition: Alias names for database objects like tables, views, or procedures.

Example:

CREATE SYNONYM EmpSyn FOR Employee;

8
Lab No 6

Types of Database Schemas

1. Physical Schema: Defines how data is stored at the physical level, including storage
details like file organization.
2. Logical Schema: Represents the logical structure of the database (e.g., tables,
relationships).
3. View Schema: Defines how data is presented to users, such as via views.

The database schema is critical in ensuring that the database is consistent, maintains integrity, and
aligns with business rules.

You might also like