DBMS Unit-3
DBMS Unit-3
› Overview,
› SQL data definition,
› SQL data types,
› Integrity constraints,
› Basic structure of SQL Queries,
› Types of SQL Commands: DDL, DML, DCL and TCL
statements,
› Basic SQL clauses [select, from, where, group by, having,
order by etc.].
Overview
› IBM developed the original version of SQL, originally called
Sequel, as part of the System R project in the early 1970s.
› The Sequel language has evolved since then, and its name
has changed to SQL (Structured Query Language).
› Many products now support the SQL language. SQL has
clearly established itself as the standard relational database
language.
› In 1986, the American National Standards Institute (ANSI)
and the International Organization for Standardization (ISO)
published an SQL standard, called SQL-86.
› ANSI published an extended standard for SQL, SQL-89, in
1989. The next version of the standard was SQL-92
standard, followed by SQL:1999, SQL:2003, SQL:2006, and
most recently SQL:2008.
The SQL language has several parts:
• Data-definition language (DDL). The SQL DDL provides
commands for defining relation schemas, deleting relations,
and modifying relation schemas.
• Data-manipulation language (DML). The SQL DML
provides the ability to query information from the database
and to insert tuples into, delete tuples from, and modify
tuples in the database.
• Integrity. The SQL DDL includes commands for specifying
integrity constraints that the data stored in the database must
satisfy. Updates that violate integrity constraints are
disallowed.
• View definition. The SQL DDL includes commands for
defining views.
• Transaction control. SQL includes commands for specifying
the beginning and ending of transactions.
• Embedded SQL and dynamic SQL. Embedded and
dynamic SQL define how SQL statements can be embedded
within general-purpose programming languages, such as C,
C++, and Java.
• Authorization. The SQL DDL includes commands for
specifying access rights to relations and views.
SQL Data Definition
⮚ The SQL data-definition language is used to create relations with
specified schemas. In addition to specifying the names and types of
relation attributes, SQL also allows the specification of integrity
constraints such as primary-key constraints and foreign-key constraints.
⮚ The set of relations in a database must be specified to the system by
means of a data-definition language (DDL). The SQL DDL allows
specification of not only a set of relations, but also information about
each relation, including:
› The schema for each relation.
› The types of values associated with each attribute.
› The integrity constraints.
› The set of indices to be maintained for each relation.
› The security and authorization information for each relation.
› The physical storage structure of each relation on disk.
SQL Data Types
Integrity Constraints
› Integrity constraints are a set of rules. It is used to maintain the quality
of information.
› Integrity constraints ensure that the data insertion, updating, and other
processes have to be performed in such a way that data integrity is
not affected.
› Thus, integrity constraint is used to guard against accidental damage
to the database.
Types of Integrity Constraint
1. Domain constraints
› Domain constraints can be defined as the definition of a
valid set of values for an attribute.
› The data type of domain includes string, character, integer,
time, date, currency, etc. The value of the attribute must be
available in the corresponding domain.
› Example:
2. Entity Integrity Constraints
› The entity integrity constraint states that primary key value
can't be null.
› This is because the primary key value is used to identify
individual rows in relation and if the primary key has a null
value, then we can't identify those rows.
› A table can contain a null value other than the primary key
field.
3. Referential Integrity Constraints
› A referential integrity constraint is specified between two tables.
› In the Referential integrity constraints, if a foreign key in Table 1 refers to
the Primary Key of Table 2, then every value of the Foreign Key in Table 1
must be null or be available in Table 2.
4. Key constraints
› --If you don't want to mention the column names then use the below syntax :
INSERT INTO TableName VALUES (Value1, Value2, Value3, ...);
› Examples:
1. INSERT INTO Employee1(EmployeeID, EmployeeName, Emergency_ContactName,
PhoneNumber, Address, City, Country)
2. VALUES('06', 'Sanjana','Jagannath', '992132114', 'Camel Street House No 12',
'Chennai', 'India');
Syntax
› SELECT Column1, Column2,..., ColumnN FROM TableName WHERE
Condition GROUP BY ColumnName(s) ORDER BY ColumnName(s);
› Example
1. -- To list the number of employees from each city--
2. SELECT COUNT(EmployeeID), City
3. FROM Employee_Info
4. GROUP BY City;
The ‘HAVING’ Clause
› The ‘HAVING’ clause is used in SQL because the WHERE keyword cannot be used
everywhere.
› We use the HAVING clause to filter groups by using single or multiple columns.
› It is noted that the WHERE clause filters the records while the HAVING clause filter the groups.
Syntax
› SELECT ColumnName(s) FROM TableName WHERE Condition GROUP BY ColumnName(s)
HAVING Condition ORDER BY ColumnName(s);
› Example
› /* To list the number of employees in each city. The employees should be sorted high to low
and only those cities must be included who have more than 1 employees:*/
By using the above set of instructions, you can update the wrong Employee
Name by the correct one and save it permanently in the database.
The update transaction gets completed when commit is used.
If commit is not used, then there will be lock on ‘Sanjana’ record till the
rollback or commit is issued.
Rollback
› Using this command, the database can be restored to the last committed
state.
› Additionally, it is also used with savepoint command for jumping to a
savepoint in a transaction.
› Syntax : Rollback to savepoint-name;
› For example
1. UPDATE Employee_Info SET Employee_Name= ‘Maria’
2. WHERE EmployeeName= ‘Sanjana’;
3. ROLLBACK;
› This command is used when the user realizes that he/she has updated the
wrong information after the student name and wants to undo this update.
› The users can issues ROLLBACK command and then undo the update.
Have a look at the below tables to know better about the implementation
of this command.
3. Savepoint:
› The main use of the Savepoint command is to save a transaction
temporarily. This way users can rollback to the point whenever it is needed.
Syntax:
SAVEPOINT SAVEPOINT_NAME; --Syntax for saving the SAVEPOINT
ROLLBACK TO SAVEPOINT_NAME; --Syntax for rolling back to the Savepoint command
Example:
› Use some SQL queries on the above table and then watch the results
1. INSERT into CLASS VALUES (101, ‘Rahul);
2. Commit;
3. UPDATE CLASS SET NAME= ‘Tyler’ where id= 101
4. SAVEPOINT A;
5. INSERT INTO CLASS VALUES (102, ‘Zack’);
6. Savepoint B;
7. INSERT INTO CLASS VALUES (103, ‘Bruno’)
8. Savepoint C;
9. Select * from Class;
11. --Now rollback to savepoint B--
12. Rollback to B;
13. SELECT * from Class;