Assignment 04
Assignment 04
Assignment 04
Aim: Design any database with at least 3 entities and relationships between them. Draw
suitable ER/EER diagram for the system. Design and implement a database using DDL and
DCL commands.
_______________________________________________________________________
Objectives:
• To learn to design database using E-R model.
Theory:
What is ER/EER?
An entity relationship diagram (ERD) shows the relationships of entity sets stored in a
database. An entity in this context is a component of data. In other words, ER diagrams
illustrate the logical structure of databases. At first glance an entity relationship diagram looks
very much like a flowchart. It is the specialized symbols, and the meanings of those symbols,
that make it unique.
EER Model
EER is a high-level data model that incorporates the extensions to the original ER model. It is
a diagrammatic technique for displaying the following concepts
● Union or Category
● Aggregation
These concepts are used when they come in EER schema and the resulting schema diagrams
called as EER Diagrams.
Entity Example:
Student
Relationship
Example: Plays
Attribute
Examples: Roll_no, Name, Email
Derived Attribute
Composite Attribute
Multivalued Attribute
Example: Contact_no
Key Attribute
Example:Roll_no
ISA Relationship:
Specialization/ Generalization
SQL as we all know is the database language by the use of which we can perform certain
operations on the existing database and also we can use this language to create a database.
SQL uses certain commands like Create, Drop, Insert, etc. to carry out the required tasks.
These SQL commands are mainly categorized into four categories as:
DDL – Data Definition Language
DQl – Data Query Language
DML – Data Manipulation Language DCL
– Data Control Language
Though many resources claim there to be another category of SQL clauses TCL – Transaction
Control Language. So we will see in detail about TCL as well.
DDL or Data Definition Language actually consists of the SQL commands that can be used
to define the database schema. It simply deals with descriptions of the database schema and
is used to create and modify the structure of database objects in the database.
Examples of DDL commands:
CREATE – is used to create the database or its objects (like table, index, function, views,
store procedure and triggers).
CREATE DATABASE:
Syntax: CREATE DATABASE DatabaseName;
Example: SQL> CREATE DATABASE testDB;
CREATE TABLE:
Syntax: CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
Example:
SQL> CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
DROP DATABASE:
Syntax: DROP DATABASE DatabaseName;
Example: SQL> DROP DATABASE testDB;
TRUNCATE–is used to remove all records from a table, including all spaces allocated for the
records are removed.
TRUNCATE TABLE:
Syntax: TRUNCATE TABLE table_name;
Types of key:
1. Primary key
It is the first key which is used to identify one and only one instance of an entity uniquely. An
entity can contain multiple keys as we saw in PERSON table. The key which is most suitable
from those lists become a primary key. In the EMPLOYEE table, ID can be primary key since
it is unique for each employee. In the EMPLOYEE table, we can even select License_Number
and Passport_Number as primary key since they are also unique. For each entity, selection of
the primary key is based on requirement and developers.
2. Candidate key
A candidate key is an attribute or set of an attribute which can uniquely identify a tuple. The
remaining attributes except for primary key are considered as a candidate key. The candidate
keys are as strong as the primary key. For example: In the EMPLOYEE table, id is best suited
for the primary key. Rest of the attributes like SSN, Passport_Number, and License_Number,
etc. are considered as a candidate key.
3. Super Key
Super key is a set of an attribute which can uniquely identify a tuple. Super key is a superset
of a candidate key. For example: In the above EMPLOYEE table, for(EMPLOEE_ID,
EMPLOYEE_NAME) the name of two employees can be the same, but their EMPLYEE_ID
can't be the same. Hence, this combination can also be a key. The super key would be
EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-NAME), etc.
4. Foreign key
Foreign keys are the column of the table which is used to point to the primary key of another
table. In a company, every employee works in a specific department, and employee and
department are two different entities. So we can't store the information of the department in
the employee table. That's why we link these two tables through the primary key of one table.
We add the primary key of the DEPARTMENT table, Department_Id as a new attribute in the
EMPLOYEE table. Now in the EMPLOYEE table, Department_Id is the foreign key, and
both the tables are related.
Database Constraint:
Constraints are the rules enforced on data columns on table. These are used to limit
the type of data that can go into a table. This ensures the accuracy and reliability of
the data in the database.
Constraints could be column level or table level. Column level constraints are
applied only to one column, whereas table level constraints are applied to the whole
table.
Following are commonly used constraints available in SQL.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is
specified.
UNIQUE Constraint: Ensures that all values in a column are different.
PRIMARY Key: Uniquely identified each rows/records in a database table.
FOREIGN Key: Uniquely identified a rows/records in any another database
table.
CHECK Constraint: The CHECK constraint ensures that all values in a
column satisfy certain conditions.
INDEX: Use to create and retrieve data from the database very quickly.
DEFAULT Constraint:
The DEFAULT constraint provides a default value to a column when the INSERT
INTO statement does not provide a specific value.
UNIQUE Constraint:
The UNIQUE Constraint prevents two records from having identical values in a
particular column. In the CUSTOMERS table, for example, you might want to
prevent two or more people from having identical age.
CHECK Constraint
The CHECK Constraint enables a condition to check the value being entered into a
record. If the condition evaluates to false, the record violates the constraint and isn't
entered into the table.
Conclusion:
ER Diagram is a design part, which is expected to complete prior to database creation. It gives
idea about overall application and how many tables you have to create in database. In this
assignment we have also studied relationship between entities, DCL and DDL commands.