ET Unit 03
ET Unit 03
1
UNIT 2
and use user-defined things (like table name, column name, etc.) in
small letters.
Using SQL in Your Web Site
To build a web site that shows data from a database, you will need:
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems
such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft
Access.
Example
Every table is broken up into smaller entities called fields. The fields in
the Customers table consist of CustomerID, CustomerName,
ContactName, Address, City, PostalCode and Country. A field is a
column in a table that is designed to maintain specific information about
every record in the table.
2
UNIT 2
Creating a Database :
Using Database :
USE geeks;
3
UNIT 2
You can use the below statement to query the description of the created table:
EXEC SP_COLUMNS department;
4
UNIT 2
The date data type uses the format ‘YYYY-MM-DD‘. Use the
below statement to add data to the department table:
INSERT INTO department VALUES (1,'Neha','F','1994-06-03');
INSERT INTO department VALUES (2,'Harsh','M','1996-03-12');
INSERT INTO department VALUES (3,'Harsh','M','1995-05-01');
INSERT INTO department VALUES (4,'Rupali','F',1996-11-11');
INSERT INTO department VALUES (5,'Rohan','M','1992-03-08');
To verify the contents of the table use the below statement:
SELECT * FROM department;
5
UNIT 2
SELECT * FROM employee;
Method 1:
The most common way to query multiple tables is with a simple
SELECT expression. To integrate results from different tables, use the
FROM clause to name more than one table. Here’s how it works in
practice:
Syntax:
SELECT table1name.column1name, table2name.column2name FROM
table1name, table2name
WHERE table1name.column1name = table2name.column1name;
Example:
SELECT department.ID, department.NAME, employee.Email,
employee.City FROM department, employee
WHERE department.ID = employee.ID;
6
UNIT 2
Output:
7
UNIT 2
More specifically, you have a parent table and a child table. The parent
contains the primary key, and the child table contains a foreign key that
references the primary key of the parent table.
When you use SQL to create a relationship, you can create the
relationship at the time you create the table, or you can create it later (by
altering the table). This article covers both scenarios.
8
UNIT 2
I created the relationship within the table definition for child. The
relationship is created with the CONSTRAINT argument. Note that this is still
inside the CREATE TABLE statement.
The REFERENCES part specifies the column that the foreign key will
reference. In this case it references the ParentId column of the Parent table.
This is done using REFERENCES Parent (ParentId).
Note that the examples on this page were done using SQL Server.
Depending on your DBMS, you may need to change some details of the
column definitions.
Let’s pretend that we didn’t create the relationship when creating the two
tables from the previous example. So let’s pretend that we’d done this
instead:
9
UNIT 2
Now, after creating the tables, we suddenly remember “oh dang, I forgot
to create a relationship!”.
Note that SQLite doesn’t support adding foreign keys with the ALTER
TABLE statement. See how to add a foreign key to an existing table in
SQLite for more about that.
On Update/Delete
By default, SQL Server relationships are created using ON DELETE NO
ACTION and ON UPDATE NO ACTION. Therefore, the previous examples were
created using this setting.
Either way, you can explicitly specify this in your code. So we can
modify the previous example to look like this:
However, you do have some options with how you want SQL Server to
deal with these situations.
10
UNIT 2
NO ACTION: An error is raised and the delete/update action on the
row in the parent table is rolled back.
CASCADE: Corresponding rows are deleted from/updated in the
referencing table if that row is deleted from/updated in the parent
table.
SET NULL: All the values that make up the foreign key are set
to NULL if the corresponding row in the parent table is deleted or
updated. This requires that the foreign key columns are nullable.
SET DEFAULT: All the values that make up the foreign key are set to
their default values if the corresponding row in the parent table is
deleted or updated. For this constraint to execute, all foreign key
columns must have default definitions. If a column is nullable, and
there is no explicit default value set, NULL becomes the implicit
default value of the column.
11
UNIT 2
After this command is executed, all the data in the table along with table
structure will be deleted.
S.NO.
DELETE COMMAND
DROP TABLE COMMAND
The INSERT command can be used to insert one or more tuples into a
relation.
Syntax:
or
INSERT INTO relationName(attributeList) VALUES ( .... )
12
UNIT 2
You must specify all attribute values in the exact order at
the relation schema.
Example:
Example 1:
Example 2:
13
UNIT 2
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.
1. Domain constraints
14
UNIT 2
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:
15
UNIT 2
o 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.
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:
16
UNIT 2
Primary Key
A Primary Key is the minimal set of attributes of a table that has the task
to uniquely identify the rows, or we can say the tuples of the given
particular table.
17
UNIT 2
Syntax for creating primary key constraint:
The primary key constraint can be defined at the column level or
table level.
At column level:
At table level:
Primary key(<column_name1>[,column_name>]....);
Candidate Key
A candidate key is a part of a key known as Super Key (discussed in
the previous section), where the super key is the super set of all those
attributes that can uniquely identify a table.
A candidate key is a subset of a super key set where the key which
contains no redundant attribute is none other than a Candidate Key. In
order to select the candidate keys from the set of super key, we need to
look at the super key set.
18
UNIT 2
So, from the above table, we obtained the below given super keys
(discussed in the previous section):
19
UNIT 2
The FOREIGN KEY constraint is used to prevent actions that would
destroy links between tables.
The table with the foreign key is called the child table, and the table with
the primary key is called the referenced or parent table.
Persons Table
PersonID LastName FirstName
1 Hansen Ola
2 Svendson Tove
3 Pettersen Kari
Orders Table
OrderID OrderNumber PersonID
1 77895 3
2 44678 3
3 22456 2
4 24562 1
Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.
The FOREIGN KEY constraint prevents invalid data from being inserted
into the foreign key column, because it has to be one of the values
contained in the parent table
20
UNIT 2
Referential Integrity
A referential integrity 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 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.
21
UNIT 2
4.DCL (Data Control Language)
5.TCL (Transaction Control Language)
2 Vishal M Physics 79
22
UNIT 2
1 Vaibhav M Mathematic
2 Vishal M Physics
23
UNIT 2
Enrollment Name Gender Subject Mark State
no
1 Vaibhav M Mathematic 100 NULL
24
UNIT 2
5. ORDER BY Clause: Used to sort the result set in ascending or
descending order.
Syntax: SELECT column1, column2 FROM table_name ORDER
BY column1 [ASC|DESC], column2 [ASC|DESC];
2 Vishal M Physics 79
25
UNIT 2
4. SELECT: Although often categorized separately,
the SELECT command is sometimes considered part of DML as it
is used to retrieve data from the database.
Syntax: SELECT column1, column2, ... FROM table_name
WHERE condition;
The SELECT statement is used to query and extract data from a
table, which can then be used for various purposes.
DCL (Data Control Language)
DCL includes commands such as GRANT and REVOKE which
mainly deal with the rights, permissions, and other controls of the
database system.
List of DCL commands:
1. GRANT: This command is used to give users access privileges to
the database. These privileges can include the ability to select, insert,
update, delete, and so on, over database objects like tables and
views.
Syntax: GRANT privilege_name ON object_name TO
user_name;
For example, GRANT SELECT ON employees TO
user123; gives user123 the permission to read data from
the employees table.
2. REVOKE: This command is used to remove previously granted
access privileges from a user.
Syntax: REVOKE privilege_name ON object_name FROM
user_name;
For example, REVOKE SELECT ON employees FROM
user123; would remove user123‘s permission to read data from
the employees table.
Syntax:
REVOKE SELECT, UPDATE ON MY_TABLE FROM U
SER1, USER2;
TCL (Transaction Control Language)
Transactions group a set of tasks into a single execution unit. Each
transaction begins with a specific task and ends when all the tasks in the
group are successfully completed. If any of the tasks fail, the transaction
fails. Therefore, a transaction has only two results: success or failure.
You can explore more about transactions here. Hence, the following
TCL commands are used to control the execution of a transaction:
26
UNIT 2
BEGIN: Opens a Transaction.
COMMIT: Commits a Transaction.
Syntax:
COMMIT;
ROLLBACK: Rollbacks a transaction in case of any error occurs.
Syntax:
ROLLBACK;
SAVEPOINT: Sets a save point within a transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
Normalization
Normalization is the process of organizing data in a database. It
includes creating tables and establishing relationships between those
tables according to rules designed both to protect the data and to make the
database more flexible by eliminating redundancy and inconsistent
dependency.
27
UNIT 2
If a relation contains a composite or multi-valued attribute, it violates
the first normal form, or the relation is in the first normal form if it does
not contain any composite or multi-valued attribute. A relation is in
first normal form if every attribute in that relation is single-valued
attribute.
A table is in 1 NF if:
There are only Single Valued Attributes.
Attribute Domain does not change.
There is a unique name for every Attribute/Column.
The order in which data is stored does not matter.
Example 1:
28
UNIT 2
key) is dependent on any proper subset of any candidate key of the table.
In other words,
A relation that is in First Normal Form and every non-
primary-key attribute is fully functionally dependent on the
primary key, then the relation is in Second Normal Form
(2NF).
Example: Consider following functional dependencies in relation R
(A, B, C, D )
AB -> C [A and B together determine C]
BC -> D [B and C together determine D]
Answer:
First, we can check if there are any partial dependencies. A partial
dependency occurs when a non-prime attribute (not part of any
candidate key) depends on only part of a candidate key.
The candidate keys for relation R can be determined by finding the
closure of each attribute:
AB determines every keys.
Now, let’s check for partial dependencies:
There are no partial dependencies in this relation because each non-
prime attribute (C and D) depends on the whole candidate key(s) it is
part of (AB and BC, respectively).
Therefore, the relation R is already in 3rd Normal Form (3NF) because
it satisfies the conditions of 1st Normal Form (1NF) and 2nd Normal
Form (2NF) and does not have any transitive dependencies.
Third Normal Form (3NF)
A relation is in the third normal form, if there is no transitive
dependency for non-prime attributes as well as it is in the second normal
form. A relation is in 3NF if at least one of the following conditions
holds in every non-trivial function dependency X –> Y.
X is a super key.
Y is a prime attribute (each element of Y is part of some
candidate key).
In other words,
A relation that is in First and Second Normal Form and in
which no non-primary-key attribute is transitively
dependent on the primary key, then it is in Third Normal
Form (3NF).
29
UNIT 2
Example 1:
In relation STUDENT given in Table 4,
30
UNIT 2
BCNF
You came across a similar hierarchy known as the Chomsky Normal
Form in the Theory of Computation. Now, carefully study the hierarchy
above. It can be inferred that every relation in BCNF is also in 3NF.
To put it another way, a relation in 3NF need not be in BCNF. Ponder
over this statement for a while.
To determine the highest normal form of a given relation R with
functional dependencies, the first step is to check whether the BCNF
condition holds. If R is found to be in BCNF, it can be safely deduced
that the relation is also in 3NF, 2NF, and 1NF as the hierarchy shows.
The 1NF has the least restrictive constraint – it only requires a relation
R to have atomic values in each tuple. The 2NF has a slightly more
restrictive constraint.
The 3NF has a more restrictive constraint than the first two normal
forms but is less restrictive than the BCNF. In this manner, the
restriction increases as we traverse down the hierarchy.
Example
Let us consider the student database, in which data of the student are
mentioned.
31
UNIT 2
Computer
101 Science & DBMS B_001 201
Engineering
Computer
Computer
101 Science & B_001 202
Networks
Engineering
Electronics
&
VLSI
102 Communicati B_003 401
Technology
on
Engineering
Electronics
& Mobile
102 Communicati Communicati B_003 402
on on
Engineering
Functional Dependency of the above is as mentioned:
Stu_ID −> Stu_Branch
Stu_Course −> {Branch_Number, Stu_Course_No}
Candidate Keys of the above table are: {Stu_ID, Stu_Course}
32
UNIT 2
For a dependency A -> B, if for a single value of A, multiple values of B
exist, then the table may have a multi-valued dependency. The table
should have at least 3 attributes and B and C should be independent for A
->> B multivalued dependency.
Fourth Normal Form (4NF)
Properties
SID SNAME
S1 A
S2 B
Table R2
CID CNAME
33
UNIT 2
CID CNAME
C1 C
C2 D
When their cross-product is done it resulted in multivalued
dependencies.
Table R1 X R2
S1 A C1 C
S1 A C2 D
S2 B C1 C
S2 B C2 D
Multivalued dependencies (MVD) are:
SID->->CID; SID->->CNAME; SNAME->->CNAME
Properties
34
UNIT 2
Example – Consider the above schema, with a case as “if a company
makes a product and an agent is an agent for that company, then he
always sells that product for the company”. Under these circumstances,
the ACP table is shown as:
Table ACP
Agent Company Product
A1 PQR Nut
A1 PQR Bolt
A1 XYZ Nut
A1 XYZ Bolt
A2 PQR Nut
The relation ACP is again decomposed into 3 relations. Now, the natural
Join of all three relations will be shown as:
Table R1
Agent Company
A1 PQR
A1 XYZ
A2 PQR
Table R2
Agent Product
A1 Nut
35
UNIT 2
Agent Product
A1 Bolt
A2 Nut
Table R3
Company Product
PQR Nut
PQR Bolt
XYZ Nut
XYZ Bolt
The result of the Natural Join of R1 and R3 over ‘Company’ and then
the Natural Join of R13 and R2 over ‘Agent’and ‘Product’ will be Table
ACP.
Denormalization
Denormalization is a database optimization technique in which we add
redundant data to one or more tables. This can help us avoid costly joins
in a relational database. Note that denormalization does not mean
‘reversing normalization’ or ‘not to normalize’. It is an optimization
technique that is applied after normalization.
Basically, The process of taking a normalized schema and making it
non-normalized is called denormalization, and designers use it to tune the
performance of systems to support time-critical operations.
In a traditional normalized database, we store data in separate logical
tables and attempt to minimize redundant data. We may strive to have
only one copy of each piece of data in a database.
Denormalization, then, strikes a different compromise. Under
denormalization, we decide that we’re okay with some redundancy and
some extra effort to update the database in order to get the efficiency
advantages of fewer joins.
Pros of Denormalization:
36
UNIT 2
1. Retrieving data is faster since we do fewer joins
2. Queries to retrieve can be simpler(and therefore less likely to have
bugs),
since we need to look at fewer tables.
Cons of Denormalization:
1. Updates and inserts are more expensive.
2. Denormalization can make update and insert code harder to write.
3. Data may be inconsistent.
4. Data redundancy necessitates more storage.
In a system that demands scalability, like that of any major tech
company, we almost always use elements of both normalized and
denormalized databases.
Advantages of Denormalization:
Disadvantages of Denormalization:
37
UNIT 2
Increased Update and Maintenance Complexity: Denormalization
can increase the complexity of updating and maintaining the database by
introducing redundant data.
Limited Flexibility: Denormalization can reduce the flexibility of a
database system by introducing redundant data and making it harder to
modify the schema.
38