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

Lesson05-Creating and Modifying Database Schema

Uploaded by

mwangibrian1293
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lesson05-Creating and Modifying Database Schema

Uploaded by

mwangibrian1293
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5 Lesson 5: Creating and Modifying database schema

Recap of ER Modelling

The process of creating a Database can be summarized as follows;

i. Requirement analysis
ii. Conceptual Model
iii. Normalization
iv. ERD
v. Database Schema / Physical Model

Logical design is about gathering data requirements and converting them into a logical model. Physical
design is the process of converting the logical model into database tables. Each entity in an E-R model
is converted to a table in relational model. The attributes of each entity are transformed into columns
of the resulting table.

Database schema

A database schema is the skeleton structure that represents the logical view of the entire database. It
defines how the organization of data for different entities and the relationship among them. It
formulates all the constraints that are to be applied on the data. It contains a descriptive detail of the
database, which can be depicted by means of schema diagrams.

Structured Query Language (SQL)

SQL is a programming language for Relational Databases. It is designed over relational algebra and
tuple relational calculus. SQL comes as a package with all major distributions of RDBMS.

SQL comprises of data definition, data manipulation, data retrieval, data control, and transaction
control commands. Using the data definition properties of SQL, one can design and modify database
schema, whereas data manipulation properties allow SQL to store and retrieve data from database.

Data Definition Language (DDL)

SQL uses the following set of commands to define database schema


CREATE Command

This command is used to created new databases, tables, and views in a RDBMS
For example −
CREATE DATABASE xyzcollege;

CREATE TABLE departments


(
dept_id INT PRIMARY KEY NOT NULL,
dept_name VARCHAR (50) NOT NULL
) ENGINE=InnoDB;

CREATE TABLE students

1
(
studid INT PRIMARY KEY NOT NULL,
reg_no VARCHAR (17) UNIQUE NOT NULL,
stud_name VARCHAR (50) NOT NULL,
email VARCHAR (50),
county_code INT (2),
programid INT
) ENGINE=InnoDB;

CREATE TABLE programs


(
programid INT PRIMARY KEY NOT NULL,
program_name VARCHAR (50) NOT NULL,
program_dept_id INT
) ENGINE=InnoDB;

CREATE VIEW [mombasa_students] AS


SELECT student_name, phone_number
FROM students
WHERE countycode = ’1’;

A view is a virtual table based on the result set of an SQL statement. The above statement
creates a view that selects all students from county number “1”:

DROP Command

The command is used to delete views, tables, and databases from a RDBMS.
Syntax:DROP object_type object_name;
 DROP DATABASE xyzcollege;
 DROP TABLE departments;
 DROP VIEW mombasa_students;

ALTER Command

This command is used to modify the database schema.


SYNTAX: − ALTER object_type object_name parameters;
The following command adds an attribute in the relation students, with the name kcseindexno, of
string type, of maximum 16 characters.
ALTER TABLE students ADD kcseindexno VARCHAR (16);
The following command drops the attribute email from the relation students.
ALTER TABLE students DROP COLUMN email;
The following command makes the student_id attribute (that is already existing in the
relation students) the primary key.

2
ALTER TABLE students ADD PRIMARY KEY (student_id);
The following command creates a relationship between the programs and departments relations
by; adding a constraint named FK_program_dept_id, making the attribute program_dept_id a
foreign key that references the primary key attribute dept_id from the departments relation
ALTER TABLE programs
ADD CONSTRAINT FK_program_dept_id
FOREIGN KEY programs(program_dept_id)
REFERENCES departments(dept_id);
Similarly, a foreign key constraint can be created without naming, in which case MySQL uses as
system generated name for the constraint.
ALTER TABLE students
ADD FOREIGN KEY students(stud_program_id)
REFERENCES programs(program_id);

To remove the relationship from the two relations,


ALTER TABLE programs DROP FOREIGN KEY FK_program_dept_id;

Lab 4: Creating a database

 Recall our earlier case study “MyLibSeat”.


 Transform the logical model created in Lab 2, into a database using SQL DML
 NB: All elements (including the constraints) must be created properly
 Populate the database with data using the INSERT command

You might also like