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

Lesson-05 - Creating and Modifying Databases

The document discusses database schemas and the SQL language. It explains that a database schema defines the structure and relationships of a database. SQL is used to define schemas using commands like CREATE, DROP, and ALTER to create, remove, and modify database objects like tables and views. The document also provides examples of using SQL data definition language commands to create tables, views, and relationships between tables in a database schema.

Uploaded by

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

Lesson-05 - Creating and Modifying Databases

The document discusses database schemas and the SQL language. It explains that a database schema defines the structure and relationships of a database. SQL is used to define schemas using commands like CREATE, DROP, and ALTER to create, remove, and modify database objects like tables and views. The document also provides examples of using SQL data definition language commands to create tables, views, and relationships between tables in a database schema.

Uploaded by

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

4 Lesson 5: 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.

Standard 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 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.

Data Definition Language

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 NOT NULL,
dept_name VARCHAR (50) NOT NULL,
CONSTRAINT PK_dept_id PRIMARY KEY (dept_id)
);

CREATE TABLE students (


stud_id INT NOT NULL,
reg_no VARCHAR (17) NOT NULL,
stud_name VARCHAR (50) NOT NULL,
email VARCHAR (50),
county_code INT (2),
stud_program_id INT,
UNIQUE KEY reg_no_kk (reg_no)
);

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);

Now let us 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

Skill progress (Where are we skill wise)


i. Mimic the development of a “real” database

ii. Writing up data requirements

iii. Designing a conceptual model in the form of an ERD

iv. Translating the conceptual model into a relational schema

v. Implementing the relational schema in RDBMS and populating the database

vi. Querying and modifying the database using SQL

vii. Optimizing the access to data by adding indexes to the relational schema

You might also like