DBMS Unit 1(Notes)
DBMS Unit 1(Notes)
SQL Concepts
Unit-1
SQL:
SQL Data Definition and Data Types; Specifying Constraints in SQL; Schema Change Statements in SQL; Basic Queries in
SQL; More Complex SQL Queries, Insert, Delete and Update Statements in SQL; Specifying Constraints as Assertions and
Triggers; Views in SQL; Additional Features of SQL.
Relational Model was proposed by E.F. Codd to model data in the form of relations or tables. After designing the
conceptual model of Database using ER diagram, we need to convert the conceptual model in the relational model
which can be implemented using any RDMBS languages like Oracle SQL, MySQL etc
Relational Model
Relational Model represents how data is stored in Relational Databases. A relational database
stores data in the form of relations (tables). Consider a relation STUDENT with attributes
ROLL_NO, NAME, ADDRESS, PHONE and AGE shown in Table 1.
STUDENT
ROLL_NO NAME ADDRESS PHONE AGE
1 RAM DELHI 9455123451 18
2 RAMESH GURGAON 9652431543 18
3 SUJIT ROHTAK 9156253131 20
4 SURESH DELHI 18
IMPORTANT TERMINOLOGIES
1. Attribute: Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME
2. Relation Schema: A relation schema represents name of the relation with its attributes.
e.g.; STUDENT (ROLL_NO, NAME, ADDRESS, PHONE and AGE) is relation schema
for STUDENT. If a schema has more than 1 relation, it is called Relational Schema.
Page 1
Database Management System
3. Tuple: Each row in the relation is known as tuple. The above relation contains 4 tuples,
one of which is shown as:
1 RAM DELHI 9455123451 18
4. Relation Instance: The set of tuples of a relation at a particular instance of time is called
as relation instance. Table 1 shows the relation instance of STUDENT at a particular time.
It can change whenever there is insertion, deletion or updation in the database.
Page 2
Database Management System
5. Degree: The number of attributes in the relation is known as degree of the relation.
The STUDENT relation defined above has degree 5.
6. Cardinality: The number of tuples in a relation is known as cardinality.
The STUDENTrelation defined above has cardinality 4.
7. Column: Column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from relation STUDENT.
ROLL_NO
1
2
3
4
8. NULL Values: The value which is not known or unavailable is called NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
1. Domain Constraints
Domain Constraints: These are attribute level constraints. An attribute can only take values
which lie inside the domain range. e.g,; If a constrains AGE>0 is applied on STUDENT relation,
inserting negative value of AGE will result in failure.
Key Integrity: Every relation in the database should have atleast one set of attributes which
Page 3
Database Management System
defines a tuple uniquely. Those set of attributes is called key. e.g.; ROLL_NO in STUDENT is a
key. No two students can have same roll number. So a key has two properties:
It should be unique for all tuples.
It can’t have NULL values. Referential Integrity: When one attribute of a relation can only take
values from other attribute of same relation or any other relation, it is called referential integrity. Let us
suppose we have 2 relations
STUDENT
ROLL_NO NAME ADDRESS PHONE AGE BRANCH_CODE
1 RAM DELHI 9455123451 18 CS
2 RAMESH GURGAON 9652431543 18 CS
3 SUJIT ROHTAK 9156253131 20 ECE
4 SURESH DELHI 18 IT
BRANCH
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
ECE ELECTRONICS AND COMMUNICATION ENGINEERING
CV CIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present in
BRANCH_CODE of BRANCH which is called referential integrity constraint. The relation
which is referencing to other relation is called REFERENCING RELATION (STUDENT in this
case) and the relation to which other relations refer is called REFERENCED RELATION
(BRANCH in this case).
A referential integrity constraint is also known as foreign key constraint. A foreign key is a key whose values are
derived from the Primary key of another table.
The table from which the values are derived is known as Master or Referenced Table and the Table in which values
are inserted accordingly is known as Child or Referencing Table, In other words, we can say that the table
containing the foreign key is called the child table, and the table containing the Primary key/candidate key is
Page 4
Database Management System
called the referenced or parent table. When we talk about the database relational model, the candidate key can be
defined as a set of attribute which can have zero or more attributes.
1. CREATE TABLE Student (Roll int PRIMARY KEY, Name varchar(25) , Course varchar(10) );
Here column Roll is acting as Primary Key, which will help in deriving the value of foreign key in the child table.
27.4M
532
History of Java
CREATE TABLE Subject (Roll int references Student, SubCode int, SubName varchar(10) );
In the above table, column Roll is acting as Foreign Key, whose values are derived using the Roll value of Primary
key from Master table.
Page 5
Database Management System
Insert Constraint: Value cannot be inserted in CHILD Table if the value is not lying in MASTER Table
Delete Constraint: Value cannot be deleted from MASTER Table if the value is lying in CHILD Table
Suppose you wanted to insert Roll = 05 with other values of columns in SUBJECT Table, then you will immediately
see an error "Foreign key Constraint Violated" i.e. on running an insertion command as:
Insert into SUBJECT values(5, 786, OS); will not be entertained by SQL due to
Insertion Constraint ( As you cannot insert value in a child table if the value is not lying in the master table,
since Roll = 5 is not present in the master table, hence it will not be allowed to enter Roll = 5 in child table )
Similarly, if you want to delete Roll = 4 from STUDENT Table, then you will immediately see an error "Foreign
key Constraint Violated" i.e. on running a deletion command as:
Delete from STUDENT where Roll = 4; will not be entertained by SQL due to
Deletion Constraint. ( As you cannot delete the value from the master table if the value is lying in the child
table, since Roll = 5 is present in the child table, hence it will not be allowed to delete Roll = 5 from the master table,
lets if somehow we managed to delete Roll = 5, then Roll = 5 will be available in child table which will ultimately
violate insertion constraint. )
ON DELETE CASCADE.
As per deletion constraint: Value cannot be deleted from the MASTER Table if the value is lying in CHILD Table.
The next question comes can we delete the value from the master table if the value is lying in the child table without
violating the deletion constraint? i.e. The moment we delete the value from the master table the value corresponding
to it should also get deleted from the child table.
The answer to the above question is YES, we can delete the value from the master table if the value is lying in the
child table without violating the deletion constraint, we have to do slight modification while creating the child table,
i.e. by adding on delete cascade.
TABLE SYNTAX
CREATE TABLE Subject (Roll int references Student on delete cascade, SubCode int, SubName varchar(10) );
In the above syntax, just after references keyword( used for creating foreign key), we have added on delete cascade,
by adding such now, we can delete the value from the master table if the value is lying in the child table without
violating deletion constraint. Now if you wanted to delete Roll = 5 from the master table even though Roll = 5 is
lying in the child table, it is possible because the moment you give the command to delete Roll = 5 from the master
table, the row having Roll = 5 from child table will also get deleted.
Page 6
Database Management System
The above two tables STUDENT and SUBJECT having four values each are shown, now suppose you are looking to
delete Roll = 4 from STUDENT( Master ) Table by writing a SQL command: delete from STUDENT
where Roll = 4;
The moment SQL execute the above command the row having Roll = 4 from SUBJECT( Child ) Table will also get
deleted, The resultant STUDENT and SUBJECT table will look like:
Page 7
Database Management System
From the above two tables STUDENT and SUBJECT, you can see that in both the table Roll = 4 gets deleted at one
go without violating deletion constraint.
KEYS
DBMS has following seven types of Keys each have their different functionality:
Super Key
Primary Key
Candidate Key
Alternate Key
Foreign Key
Compound Key
Composite Key
Surrogate Key
Page 8
Database Management System
Super key
A super key is a group of single or multiple keys which identifies rows in a table. A Super key
may have additional attributes that are not needed for unique identification.
Example:
Primary Key
A column or group of columns in a table which helps us to uniquely identifies every row in that
table is called a primary key. This DBMS can't be a duplicate. The same value can't appear more
than once in the table.
Example:
Page 9
Database Management System
Alternate key
All the keys which are not primary key are called an alternate key. It is a candidate key which is
currently not the primary key. However, A table may have single or multiple choices for the
primary key.
StudID, Roll No, Email are qualified to become a primary key. But since StudID is the primary
key, Roll No, Email becomes the alternative key.
Candidate Key
The Primary key should be selected from the candidate keys. Every table must have at least a
single candidate key.
Page 10
Database Management System
Example: In the given table Stud ID, Roll No, and email are candidate keys which help us to
uniquely identify the student record in the table.
Foreign key
A foreign key is a column which is added to create a relationship with another table. Foreign
keys help us to maintain data integrity and also allows navigation between two different
instances of an entity. Every relationship in the model needs to be supported by a foreign key.
Page 11
Database Management System
Example:
In this example, we have two table, teach and department in a school. However, there is no way
to see which search work in which department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can create a
relationship between the two tables.
Compound key
Compound key has many fields which allow you to uniquely recognize a specific record. It is
possible that each column may be not unique by itself within the database. However, when
combined with the other column or columns the combination of composite keys become unique.
Example:
Page 12
Database Management System
In this example, OrderNo and ProductID can't be a primary key as it does not uniquely identify a
record. However, a compound key of Order ID and Product ID could be used as it uniquely
identified each record.
Composite key
A key which has multiple attributes to uniquely identify rows in a table is called a composite
key. The difference between compound and the composite key is that any part of the compound
key can be a foreign key, but the composite key may or maybe not a part of the foreign key.
Page 13
Database Management System
Helps you to uniquely identify a record It is a field in the table that is the primary key
in the table. of another table.
Primary Key never accept null values. A foreign key may accept multiple null values.
Primary key is a clustered index and A foreign key cannot automatically create an
data in the DBMS table are physically index, clustered or non-clustered. However,
organized in the sequence of the you can manually create an index on the
clustered index. foreign key.
You can have the single Primary key in You can have multiple foreign keys in a table.
a table.
Page 14
Database Management System
Page 15
Update Operations, Transactions, and Dealing with Constraint Violations
Database Management System
There are three basic operations that can change the states of relations in the data-base: Insert,
Delete, and Update (or Modify). They insert new data, delete old data, or modify existing
data records. Insert is used to insert one or more new tuples in a relation, Delete is used to
delete tuples, and Update (or Modify) is used to change the values of some attributes in
existing tuples. Whenever these operations are applied, the integrity constraints specified on
the relational database schema should not be violated.
1. The Insert Operation
The Insert operation provides a list of attribute values for a new tuple t that is to be inserted
into a relation R.
Insert can violate any of the four types of constraints
Domain constraints can be violated if an attribute value is given that does not appear
in the corresponding domain or is not of the appropriate data type.
Key constraints can be violated if a key value in the new tuple t already exists in
another tuple in the relation r(R).
Entity integrity can be violated if any part of the primary key of the new
tuple t is NULL.
Referential integrity can be violated if the value of any foreign key in t refers to a
tuple that does not exist in the referenced relation
Operation:
Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, NULL, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F,
28000, NULL, 4> into EMPLOYEE.
Result: This insertion violates the entity integrity constraint (NULL for the primary key Ssn),
so it is rejected.
Operation:
Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F,
28000, ‘987654321’, 4> into EMPLOYEE.
Result: This insertion violates the key constraint because another tuple with the same Ssn
value already exists in the EMPLOYEE relation, and so it is rejected.
Operation:
Delete the EMPLOYEE tuple with Ssn = ‘999887777’.
Result: This deletion is not acceptable, because there are tuples in WORKS_ON that refer to
this tuple. Hence, if the tuple in EMPLOYEE is Deleted, referential integrity violations will
result.
3. The Update Operation
The Update (or Modify) operation is used to change the values of one or more attributes in a
tuple (or tuples) of some relation R. It is necessary to specify a condition on the attributes of
the relation to select the tuple (or tuples) to be modified. Here are some examples.
Operation:
Update the salary of the EMPLOYEE tuple with Ssn = ‘999887777’ to 28000.
Result: Acceptable.
Operation:
Operation:
Operation:
Update the Ssn of the EMPLOYEE tuple with Ssn = ‘999887777’ to ‘987654321’.
Page 17
Database Management System
Result: Unacceptable, because it violates primary key constraint by repeating a value that
already exists as a primary key in another tuple; it violates refer-ential integrity constraints
because there are other relations that refer to the existing value of Ssn.
SQL statements:
Select statements are used to retrieve data from SQL tables. The Select statement illustrated
below retrieves all of the columns and rows from the named table.
Syntax:
Select *
from tablename;
Explanation:
A Select statement is a SQL statement that begins with the word "select."
Select statements are used to retrieve data from SQL tables.
An asterisk after the word "select" means retrieve all fields (columns).
The name of the table from which you are retrieving data is specified in the From
clause.
Use a semicolon to signify the end of a SQL statement.
The SQL SELECT statement is used to fetch the data from a database table
which returns this data in the form of a result table. These result tables are
called result-sets.
Syntax
The basic syntax of the SELECT statement is as follows −
SELECT column1, column2, columnN FROM table_name;
Here, column1, column2... are the fields of a table whose values you want to
fetch. If you want to fetch all the fields available in the field, then you can use
the following syntax.
SELECT * FROM table_name;
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| 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 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Page 18
+----+----------+-----+-----------+----------+
Database Management System
The following code is an example, which would fetch the ID, Name and
Salary fields of the customers available in CUSTOMERS table.
SQL> SELECT ID, NAME, SALARY FROM CUSTOMERS;
Distinct
Pattern Matching
Nested Queries:
Aggregate functions: Page 19
Database Management System
Data types mainly classified into three categories for every database.
o Date and time data types: These are used to store date and time
values. Examples include DATE, TIME, and TIMESTAMP.
o Binary data types: These are used to store binary data, such as
images or audio files. Examples include BLOB and BYTEA.
o Boolean data type: This data type is used to store logical values.
The only possible values are TRUE and FALSE.
Schema Change Statements in SQL: create, alter and drop commands are used for changing the
schema of the SQL
Page 20