DBMS Unit-3 Notes - Student
DBMS Unit-3 Notes - Student
The relational data model was first introduced by Ted Codd of IBM Research in 1970 in a classic
paper (Codd, 1970), and it attracted immediate attention due to its simplicity and mathematical
foundation. The model uses the concept of a mathematical relation—which looks somewhat like
a table of values—as its basic building block, and has its theoretical basis in set theory and first-
order predicate logic.
The relational model represents the database as a collection of relations. Informally, each relation
resembles a table of values. When a relation is thought of as a table of values, each row in the
table represents a collection of related data values. In the formal relational model terminology, a
row is called a tuple, a column header is called an attribute, and the table is called a relation.
Domain: For each attribute, there is a set of permitted values called the domain of that attribute.
A domain D is a set of atomic values. By atomic we mean that each value in the domain is
indivisible as far as the formal relational model is concerned. A common method of specifying a
domain is to specify a data type from which the data values forming the domain are drawn.
Attributes: Each table column represents an attribute and each column has a distinct name.
Tuples: Each table row represents a single entity occurrence within the entity set.
A relation schema R, denoted by R (A1, A2, …, An), is made up of a relation name R and a list of
attributes, A1, A2, … , An. Each attribute Ai is the name of a role played by some domain D in
the relation schema R. D is called the domain of Ai and is denoted by dom(Ai). A relation of
degree seven, which stores information about university students, would contain seven attributes
describing each student as follows:
Using the data type of each attribute, the definition is sometimes written as:
STUDENT (Name: string, Ssn: string, mobile: string, Address: string, Age: integer)
Relational Model Constraints:
Domain Constraints: Domain constraint enforces restrictions on information that we store in the
database so Domain constraints specify that within each tuple, the value of each attribute A must
be an atomic value from the domain dom(A).
At the simplest level, the database assigned to the column enforces domain integrity. For
example, you won’t be able to enter text in a domain that is defined as a number. The more we
can do to limit the data that can be entered into the field to its domain (length of data). SQL
provides a variety of tools for enforcing domain integrity. These are :
Datatype
Not Null: It specifies which data is absolute necessary for the database to function
properly.
Check: It will check the range from values for the column with check constraints.
Entity Integrity Constraint: The basic idea of Entity Integrity is that we must be able to uniquely
identify each entity that we store in the database. The entity integrity constraint states that no
primary key value can be NULL. This is because the primary key value is used to identify
individual tuples in a relation. Having NULL values for the primary key implies that we cannot
identify some tuples.
Referential Integrity Constraint: Key constraints and entity integrity constraints are specified on
individual relations. The referential integrity constraint is specified between two relations and is
used to maintain the consistency among tuples in the two relations. Informally, the referential
integrity constraint states that a tuple in one relation that refers to another relation must refer to
an existing tuple in that relation. For example, the attribute Dno of EMPLOYEE gives the
department number for which each employee works; hence, its value in every EMPLOYEE tuple
must match the Dnumber value of some tuple in the DEPARTMENT relation.
The CREATE TRIGGER statement is used to implement such actions in SQL. Here we just give
a simple example of how triggers may be used.
Suppose we want to check whenever an employee’s salary is greater than the salary of his or her
direct supervisor in the COMPANY database. Several events can trigger this rule: inserting a
new employee record, changing an employee’s salary, or changing an employee’s supervisor.
Suppose that the action to take would be to call an external stored procedure
SALARY_VIOLATION,5 which will notify the supervisor. The trigger could then be written as
in R5 below. Here we are using the syntax of the Oracle database system.
R5: CREATE TRIGGER SALARY_VIOLATION BEFORE INSERT OR UPDATE OF
SALARY, SUPERVISOR_SSN ON EMPLOYEE FOR EACH ROW WHEN ( NEW.SALARY
> ( SELECT SALARY FROM EMPLOYEE WHERE SSN = NEW.SUPERVISOR_SSN ) )
INFORM_SUPERVISOR(NEW.Supervisor_ssn, NEW.Ssn );
SQL: SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in a relational database. SQL is the standard language for
Relational Database System. All the Relational Database Management Systems (RDMS) like
MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their
standard database language. Also, they are using different dialects, such as −
MS SQL Server using T-SQL,
Oracle using PL/SQL,
MS Access version of SQL is called JET SQL (native format) etc.
Why SQL? SQL is widely popular because it offers the following advantages −
Allows users to access data in the relational database management systems.
Allows users to describe the data.
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.
SQL Commands: The standard SQL commands to interact with relational databases are
CREATE, SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be
classified into the following groups based on their nature.
3 DROP: Deletes an entire table, a view of a table or other objects in the database.
DML - Data Manipulation Language
SQL is followed by a unique set of rules and guidelines called Syntax. This tutorial gives you a
quick start with SQL by listing all the basic SQL Syntax. All the SQL statements start with any
of the keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE,
SHOW and all the statements end with a semicolon (;).
Various Syntax in SQL
SQL SELECT Statement
SELECT column1, column2....columnN FROM table_name;
SQL IN Clause
SELECT column1, column2....columnN FROM table_name WHERE column_name IN (val-1,
val-2,...val-N);
Create Table Command: Creating a basic table involves naming the table and defining its
columns and each column's data type. The SQL CREATE TABLE statement is used to create a
new table. The basic syntax of the CREATE TABLE statement is as follows −
CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,
..... columnN datatype, PRIMARY KEY( one or more columns ) );
CREATE TABLE is the keyword telling the database system what you want to do. In this case,
you want to create a new table. The unique name or identifier for the table follows the CREATE
TABLE statement. The following code block is an example, which creates a CUSTOMERS
table with an ID as a primary key and NOT NULL are the constraints showing that these fields
cannot be NULL while creating records in this table −
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));
You can verify if your table has been created successfully by looking at the message displayed
by the SQL server, otherwise you can use the DESC command as follows –
Insert Command:
The SQL INSERT INTO Statement is used to add new rows of data to a table in the database.
There are two basic syntaxes of the INSERT INTO statement which are shown below.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2,
value3,...valueN)
Here, column1, column2, column3,...columnN are the names of the columns in the table into
which you want to insert the data. You may not need to specify the column(s) name in the SQL
query if you are adding values for all the columns of the table. But make sure the order of the
values is in the same order as the columns in the table.
Update Command: The SQL UPDATE Query is used to modify the existing records in a table.
You can use the WHERE clause with the UPDATE query to update the selected rows,
otherwise all the rows would be affected. The basic syntax of the UPDATE query with a
WHERE clause is as follows:
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
The following query will update the ADDRESS for a customer whose ID number is 6 in the
table.
Delete Command: The SQL DELETE Query is used to delete the existing records from a
table. You can use the WHERE clause with a DELETE query to delete the selected rows,
otherwise all the records would be deleted. The basic syntax of the DELETE query with the
WHERE clause is as follows –
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
The following code has a query, which will DELETE a customer, whose ID is 6.
If you want to DELETE all the records from the CUSTOMERS table, you do not need to use
the WHERE clause and the DELETE query would be as follows −
Question: A Store buys Product from many wholesalers. Wholesaler_ID is used to identify
individual wholesalers. Products are identified by PRODUCT_ID and store details of product.
Wholesaler phone_no are also stored and wholesaler may have multiple phone numbers. The
rates for each product charged by each wholesaler is also saved. Construct an ER Diagram and
design the database schema for the above scenario.