Lesson-05 - Creating and Modifying Databases
Lesson-05 - Creating and Modifying Databases
Recap of ER Modelling
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 fields 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 data is organized and how the relations among them are associated. It formulates all the
constraints that are to be applied on the data. A database schema defines its entities and the relationship
among them. 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 both data definition and data manipulation languages. 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.
1
CREATE TABLE programs (
program_id INT NOT NULL,
program_name VARCHAR (50) NOT NULL,
program_dept_id INT,
PRIMARY KEY (prog_id),
CONSTRAINT FK_program_dept_id FOREIGN KEY (program_dept_id)
REFERENCES departments (dept_id)
);
// ) ENGINE=InnoDB;
// change the last line to change from default storage engine from
// MyISAM to INNODB
CREATE VIEW [sci_students] AS
SELECT student_name, phone_number
FROM students
WHERE school_code = ’SCI’;
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 in “SCI”:
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 sci_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.
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 department’s relation
2
ALTER TABLE programs
ADD CONSTRAINT FK_program_dept_id
FOREIGN KEY (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 (stud_program_id) REFERENCES programs(program_id);
vii. Optimizing the access to data by adding indexes to the relational schema