Lesson05-Creating and Modifying Database Schema
Lesson05-Creating and Modifying Database Schema
Recap of ER Modelling
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.
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.
This command is used to created new databases, tables, and views in a RDBMS
For example −
CREATE DATABASE xyzcollege;
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;
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
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);