Unit 2
Unit 2
Unit 2
Advantages of SQL
SQL is an efficient language for database communication. It has various
advantages, some of them are listed below:
One of the things that make SQL a popular choice is that it doesn't require any
substantial knowledge about coding and writing programs. SQL has some basic
keywords such as, SELECT, INSERT INTO, UPDATE, etc that can carry out tasks.
Syntactical rules are also simple and easy to follow.
SQL works with an efficient speed. Huge data can be processed in seconds!
Querying, manipulating, and calculations on data with analytical queries in a
relational database can be done in no time.
3. Standard Language
4. Portable
5. Highly Interactive
As it has simple commands for all purposes, it becomes an interactive language for
its users. It easy to understand and the commands are also understandable to the
non-programmers.
Disadvantages of SQL
1. Poor Interface
SQL has a poor interface as it makes look everything very complex even when it's
not! Due to its difficult interfacing, users find it difficult to deal with the databases.
2. Cost Inefficient
SQL Server Standard costs around $1,418/year. The high cost makes it difficult for
some programmers to use it.
3. Partial Control
SQL doesn't grant complete control over databases to its users. This is due to some
hidden business rules.
4. Security
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of
information.
o 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.
o Thus, integrity constraint is used to guard against accidental damage to the database.
Types of Integrity Constraint
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an
attribute.
o 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:
o The entity integrity constraint states that primary key value can't be null.
o 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.
o A table can contain a null value other than the primary key field.
Example:
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key.
A primary key can contain a unique and null value in the relational table.
Example:
In the tables, one attribute, you can see, is common, that is Stud_Id, but it has
different key constraints for both tables. In the Student table, the field Stud_Id is
a primary key because it is uniquely identifying all other fields of the Student table.
On the other hand, Stud_Id is a foreign key attribute for the Department table
because it is acting as a primary key attribute for the Student table. It means that
both the Student and Department table are linked with one another because of the
Stud_Id attribute.
In the below-shown figure, you can view the following structure of the relationship
between the two tables.
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to
obtain the result of the query. It uses operators to perform queries.
1. Select Operation:
1. Notation: σ p(r)
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR
and NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
2. 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 ∏.
1. Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Input:
1. ∏ NAME, CITY (CUSTOMER)
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples
that are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
1. Notation: R ∪ S
Example:
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
DEPOSITOR RELATION
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Input:
1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Input:
1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
Output:
1 Smith A A
1 Smith A B
1 Smith A C
2 Harry C A
2 Harry C B
2 Harry C C
3 John B A
3 John B B
3 John B C
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).