Database 3,4
Database 3,4
SQL: SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS). It is a standard
language for Relational Database System. It enables a user to create, read, update
and delete relational databases and tables. All the RDBMS like MySQL, Oracle,
SQL Server etc. use SQL as their standard database language.
SQL Data Types: Data types are used to represent the nature of the data that can
be stored in the database table. For example, in a particular column of a table, if we
want to store a string type of data then we will have to declare a string data type of
that column. Data types is mainly classified into three categories for every database.
MySQL Data Types : A list of data types used in MySQL database. This is based
on MySQL 8.0.
SQL Constraints: SQL constraints are used to specify rules for the data in a
table. Constraints are used to prevent invalid data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any violation
between the constraint and the data action, the action is aborted. Constraints can be
column level or table level. Column level constraints apply to a column, and table
level constraints apply to the whole table. The following are the various types of
integrity constraints used in SQL:
1. Domain Integrity constraints – maintains value according to specifications
a. NOT NULL - Ensures that a column cannot have a NULL value
b. CHECK - Ensures that all values in a column satisfies a specific
condition
c. DEFAULT - Sets a default value for a column when no value is specified
SQL Commands
o SQL commands are instructions used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the
table, modify the table, set permission for users etc.
Types of SQL Commands: There are five types of SQL commands: DDL, DML,
DCL, TCL, and DQL.
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax: DROP TABLE <table_name>;
Example: DROP TABLE STUD;
c. ALTER: It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to add a new
attribute.
Syntax To add a new column in the table:
ALTER TABLE table_name ADD (column_name column_definition);
Syntax To modify existing column in the table:
ALTER TABLE table_name MODIFY (column_name column_definition);
EXAMPLE
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.
Syntax: TRUNCATE TABLE table_name;
Example: TRUNCATE TABLE EMPLOYEE;
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
Syntax: INSERT INTO TABLE_NAME (col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
Or
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Example:
INSERT INTO BOOK (Author, Subject) VALUES ("Korth", "DBMS");
b. UPDATE: This command is used to update or modify the value of a column in the
table.
Syntax:
UPDATE table_name SET [column_name1=value1,...column_nameN=valueN]
[WHERE CONDITION]
Example: UPDATE students SET Name = 'Sonoo' WHERE Student_Id = '3'
3. Data Control Language: DCL commands are used to grant and take back
authority from any database user. The commands that come under DCL are Grant
and Revoke.
4. Transaction Control Language: TCL commands can only use with DML
commands like INSERT, DELETE and UPDATE only. They cannot be used with
DDL commands as DDL commands are auto-committed. The commands that come
under TCL are COMMIT, ROLLBACK and SAVEPOINT.
a. Commit: Commit command is used to save all the transactions to the database.
Syntax: COMMIT;
Example: DELETE FROM CUSTOMERS WHERE AGE = 25; COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already
been saved to the database.
Syntax: ROLLBACK;
Example: DELETE FROM CUSTOMERS WHERE AGE = 25; ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without
rolling back the entire transaction.
Syntax: SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language: DQL is used to fetch the data from the database. It uses
only one command SELECT.
a. SELECT: This is the same as the projection operation of relational algebra. It is
used to select the attribute based on the condition described by WHERE clause.
Syntax: SELECT expressions FROM TABLES
WHERE conditions;
For example: SELECT emp_name FROM employee
WHERE age > 20;
SELECT Statement Clauses: The SELECT statement has the following clauses:
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
SQL Arithmetic Operators: Let's assume 'variable a' and 'variable b'. Here, 'a'
contains 20 and 'b' contains 10.
SQL Comparison Operators: Let's assume 'variable a' and 'variable b'. Here,
'a' contains 20 and 'b' contains 10.
Notation: σ p(r)
Where, p is propositional logic formula which may use connectors like: AND OR and
NOT. It can use relational operators like =, ≠, ≥, <, >, ≤. r is the relation on which
select operation is performed.
For example − σsubject = "database" and price = "450"(Books)
Output − Selects tuples from books where subject is 'database' and 'price' is 450.
2. The Project Operation:
o This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
o It is denoted by ∏.
Notation: ∏ A1, A2, An (r)
Where: A1, A2, A3 is used as attribute names of relation r.
For example − ∏subject, author (Books)
Selects and projects columns named as subject and author from the relation Books.
Inner Outer
Join Join
Equijoin: When Theta join uses only equality comparison operator, it is said to be
equijoin. The above example corresponds to equijoin.
Right Outer Join: ( R S ): All the tuples from the Right relation, S, are included
in the resulting relation. If there are tuples in S without any matching tuple in R, then
the R-attributes of resulting relation are made NULL.
Example:
Division Operator (÷): Division operator A÷B can be applied if and only if:
Attributes of B is proper subset of Attributes of A.
The relation returned by division operator will have attributes = (All attributes of A
– All Attributes of B)
The relation returned by division operator will return those tuples from relation A
which are associated to every tuple of B.
Example:
STUDENT_SPORTS ALL_SPORTS STUDENT_SPORTS ÷
ALL_SPORTS
ROLL_NO SPORTS SPORTS ROLL_NO
1 Badminton Badminton 2
(ROLL_NO 2 is
2 Cricket Cricket
associated to
2 Badminton all tuples of B)
4 Badminton
The operation is valid as attributes in ALL_SPORTS is a proper subset of
attributes in STUDENT_SPORTS.
The attributes in resulting relation will have attributes
{ROLL_NO,SPORTS} - {SPORTS} = ROLL_NO
The tuples in resulting relation will have those ROLL_NO which are associated
with all B’s tuple {Badminton, Cricket}. ROLL_NO 1 and 4 are associated to
Badminton only. ROLL_NO 2 is associated to all tuples of B. So the resulting
relation will be:
ROLL_NO
2
Intersection Operation: The intersection operator gives the common data values
between the two data sets that are intersected. The two data sets that are
intersected should be similar for the intersection operator to work. Intersection also
removes all duplicates before displaying the result.
Example:
Select Student_Name from Art_Students
INTERSECT
Select Student_Name from Dance_Students
This will display the names of the students in the table Art_Students and in the table
Dance_Students i.e all the students that have taken both art and dance classes
2. Non-serial Schedule
Example:
Schedule S1: Precedence graph of Schedule S1
Transaction Transaction
Time
T1 T2
T1 T2
t1 Read(A)
t2 Read(A) In above precedence graph of schedule
S1, contains two vertices T1 and T2, and
t3 Write(A) edges T1→ T2 and T2→ T1. In this
t4 A=A+50 Schedule S1, operations of T1 and T2
transaction are present in an interleaved
t5 Write(A) manner.
The precedence graph contains a cycle, that’s why schedule S1 is non-serializable.
Types of Serializability
1. Conflict Serializability
2. View Serializability
Conflict Serializable Schedule: A schedule is called conflict serializability if after
swapping of non-conflicting operations, it can transform into a serial schedule. The
schedule will be a conflict serializable if it is conflict equivalent to a serial schedule.
Conflicting Operations: The two operations become conflicting if all the following
conditions are satisfied.
1. Both belong to separate transactions.
2. They have the same data item.
3. They contain at least one write operation.
View Equivalent: Two schedules S1 and S2 are said to be view equivalent if they
satisfy the following conditions:
1. Initial Read: An initial read of both schedules must be the same. In schedule S1,
if a transaction T1 is reading the data item A, then in S2, transaction T1 should also
read A.
T1 T2 T1 T2
Read(A) Write(A)
Write(A) Read(A)
Schedule S1 Schedule S2
In the above example initial read operation is done by T1 in both the schedule S1
and S2.
T1 T2 T3 T1 T2 T3
Write(A) Write(A)
Write(A) Write(A)
Read(A) Read(A)
Schedule S1 Schedule S2
Above two schedules are not view equal because in S1, T3 is reading A which is
updated by T2 and in S2, T3 is reading A which is updated by T1.
3. Final Write: A final write must be the same between both the schedules. In
schedule S1, if a transaction T1 updates A at last then in S2, final writes operations
should also be done by T1.
T1 T2 T3 T1 T2 T3
Write(A) Read(A)
Read(A) Write(A)
Write(A) Write(A)
Schedule S1 Schedule S2
In the above example final write operation is done by T3 both in schedule S1 and
schedule S2.
Example:
T1 T2 T3With 3 transactions, the total number of possible
Read(A) schedule = 3! = 6
Write(A) S1 = <T1 T2 T3>
Write(A) S2 = <T1 T3 T2>
Write(A) S3 = <T2 T3 T1>
Schedule S S4 = <T2 T1 T3>
S5 = <T3 T1 T2>
S6 = <T3 T2 T1>
Taking first schedule S1:
T1 T2 T3 Step 1: Initial Read: The initial read operation
Read(A) in S is done by T1 and in S1, it is also done
Write(A) by T1.
Write(A) Step 2: Updated Read: In both schedules S
Write(A) and S1, there is no read except the initial
Schedule S1 read that's why we don't need to check that
condition.
Step 3: Final Write: The final write operation is done by T3 in both the schedule S
and S1. So, S and S1 are view Equivalent.
The first schedule S1 satisfies all three conditions, so we don't need to check other
schedules.
Hence, view equivalent serial schedule is: T1 → T2 → T3