0% found this document useful (0 votes)
5 views

SQL Select Null

Uploaded by

dusanikhil3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQL Select Null

Uploaded by

dusanikhil3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

SQL SELECT NULL

• First of all we should know that what null value is? Null values are
used to represent missing unknown data.
• There can be two conditions:
1.Where SQL is NULL
2.Where SQL is NOT NULL
• If in a table, a column is optional, it is very easy to insert data in
column or update an existing record without adding a value in this
column.
• This means that field has null value.
• Where SQL is NULL:
• How to select records with null values only? (in the marks column)
• There is an example of student table:
• Let's see the query to get all the records where marks is NULL:

1.SELECT SIR_NAME, NAME, MARKS FROM STUDENTS


2.WHERE MARKS IS NULL
• It will return the following records:
Where SQL is NOT NULL:

• How to select records with no null values(in marks column)? Let's see
the query to get all the records where marks is NOT NULL

1.SELECT SIR_NAME, FIRSTNAME, MARKS FROM STUDENTS

2.WHERE MARKS IS NOT NULL


Trigger

• A Trigger in Structured Query Language is a set of procedural


statements which are executed automatically when there is any
response to certain events on the particular table in the database.
• Triggers are used to protect the data integrity in the database.
• In SQL, this concept is the same as the trigger in real life. For
example, when we pull the gun trigger, the bullet is fired.
To understand the concept of trigger in
SQL,
• Suppose Rishabh is the human resource manager in a multinational company.
When the record of a new employee is entered into the database, he has to
send the 'Congrats' message to each new employee. If there are four or five
employees, Rishabh can do it manually, but if the number of new Employees
is more than the thousand, then in such condition, he has to use the trigger in
the database.
• Thus, now Rishabh has to create the trigger in the table, which will
automatically send a 'Congrats' message to the new employees once their
record is inserted into the database.
• The trigger is always executed with the specific table in the database. If we
remove the table, all the triggers associated with that table are also deleted
automatically.
In Structured Query Language, triggers are called
only either before or after the below events:
1.INSERT Event: This event is called when the new row is entered in
the table.
2.UPDATE Event: This event is called when the existing record is
changed or modified in the table.
3.DELETE Event: This event is called when the existing record is
removed from the table.
Types of Triggers in SQL

• Following are the six types of triggers in SQL:


• AFTER INSERT Trigger
This trigger is invoked after the insertion of data in the table.
• AFTER UPDATE Trigger
This trigger is invoked in SQL after the modification of the data in the
table.
• AFTER DELETE Trigger
This trigger is invoked after deleting the data from the table.
• BEFORE INSERT Trigger
This trigger is invoked before the inserting the record in the table.
• BEFORE UPDATE Trigger
This trigger is invoked before the updating the record in the table
• BEFORE DELETE Trigger
This trigger is invoked before deleting the record from the table.
Syntax of Trigger in SQL

1.CREATE TRIGGER Trigger_Name


2.[ BEFORE | AFTER ] [ Insert | Update | Delete]
3.ON [Table_Name]
4.[ FOR EACH ROW | FOR EACH COLUMN ]
5.AS
6.Set of SQL Statement
• In the trigger syntax, firstly, we have to define the name of the trigger
after the CREATE TRIGGER keyword. After that, we have to define
the BEFORE or AFTER keyword with anyone event.
• Then, we define the name of that table on which trigger is to occur.
• After the table name, we have to define the row-level or statement-
level trigger.
• And, at last, we have to write the SQL statements which perform
actions on the occurring of event.
Example of Trigger in SQL

• To understand the concept of trigger in SQL, first, we have to create


the table on which trigger is to be executed.
• The following query creates the Student_Trigger table in the SQL
database:
1.CREATE TABLE Student_Trigger
2.(
3.Student_RollNo INT NOT NULL PRIMARY KEY,
4.Student_FirstName Varchar (100),
5.Student_EnglishMarks INT,
6.Student_PhysicsMarks INT,
7.Student_ChemistryMarks INT,
8.Student_MathsMarks INT,
9.Student_TotalMarks INT,
10.Student_Percentage );
• The following query shows the structure of theStudent_Trigger table:
1.DESC Student_Trigger;
The following query fires a trigger before the
insertion of the student record in the table:
1.CREATE TRIGGER Student_Table_Marks
2.BEFORE INSERT
3.ON
4.Student_Trigger
5.FOR EACH ROW
6.SET new.Student_TotalMarks = new.Student_EnglishMarks + new.St
udent_PhysicsMarks + new.Student_ChemistryMarks + new.Student_
MathsMarks,
7.new.Student_Percentage = ( new.Student_TotalMarks / 400) * 100;
• The following query inserts the record into Student_Trigger table:
• INSERT INTO Student_Trigger (Student_RollNo, Student_FirstNam
e, Student_EnglishMarks, Student_PhysicsMarks, Student_Chemistry
Marks, Student_MathsMarks, Student_TotalMarks, Student_Percentag
e) VALUES ( 201, Sorya, 88, 75, 69, 92, 0, 0);

• To check the output of the above INSERT statement, you have to type
the following SELECT statement:
• SELECT * FROM Student_Trigger;
Advantages of Triggers in SQL

• Following are the three main advantages of triggers in Structured


Query Language:
1.SQL provides an alternate way for maintaining the data and referential
integrity in the tables.
2.Triggers helps in executing the scheduled tasks because they are
called automatically.
3.They catch the errors in the database layer of various businesses.
4.They allow the database users to validate values before inserting and
updating.
Disadvantages of Triggers in SQL

• Following are the main disadvantages of triggers in Structured Query


Language:
1.They are not compiled.
2.It is not possible to find and debug the errors in triggers.
3.If we use the complex code in the trigger, it makes the application run
slower.
4.Trigger increases the high load on the database system.
Redundancy in DBMS

• Data redundancy means the occurrence of duplicate copies of similar


data.
• It is done intentionally to keep the same piece of data at different
places, or it occurs accidentally.
What is Data redundancy in the database management system?

• In DBMS, when the same data is stored in different tables, it causes


data redundancy.
• Sometimes, it is done on purpose for recovery or backup of data, faster
access of data, or updating data easily.
• Redundant data costs extra money, demands higher storage capacity,
and requires extra effort to keep all the files up to date.
• Sometimes, unintentional duplicity of data causes a problem for the
database to work properly, or it may become harder for the end user to
access data.
• Redundant data unnecessarily occupy space in the database to save
identical copies, which leads to space constraints, which is one of the
major problems.
Let us understand redundancy in DBMS
properly with the help of an example.
• In the above example, there is a "Student" table that contains data such
as "Student_id", "Name", "Course", "Session", "Fee", and
"Department". As you can see, some data is repeated in the table,
which causes redundancy.
Problems that are caused due to redundancy in the database

• Redundancy in DBMS gives rise to anomalies, and we will study it


further.
• In a database management system, the problems that occur while
working on data include inserting, deleting, and updating data in the
database.
We will understand these anomalies with the
help of the following student table:
1. Insertion Anomaly:

• Insertion anomaly arises when you are trying to insert some data into
the database, but you are not able to insert it.
• Example: If you want to add the details of the student in the above
table, then you must know the details of the department; otherwise,
you will not be able to add the details because student details are
dependent on department details.
2. Deletion Anomaly:

• Deletion anomaly arises when you delete some data from the database,
but some unrelated data is also deleted; that is, there will be a loss of
data due to deletion anomaly.
• Example: If we want to delete the student detail, which has student_id
2, we will also lose the unrelated data, i.e., department_id 102, from
the above table.
3. Updating Anomaly:

• An update anomaly arises when you update some data in the database,
but the data is partially updated, which causes data inconsistency.
• Example: If we want to update the details of dept_head from Jaspreet
Kaur to Ankit Goyal for Dept_id 104, then we have to update it
everywhere else; otherwise, the data will get partially updated, which
causes data inconsistency.
Advantages of data redundancy in DBMS

• Provides Data Security: Data redundancy can enhance data security


as it is difficult for cyber attackers to attack data that are in different
locations.
• Provides Data Reliability: Reliable data improves accuracy because
organizations can check and confirm whether data is correct.
• Create Data Backup: Data redundancy helps in backing up the data.
Disadvantages of data redundancy in DBMS

• Data corruption: Redundant data leads to high chances of data


corruption.
• Wastage of storage: Redundant data requires more space, leading to a
need for more storage space.
• High cost: Large storage is required to store and maintain redundant
data, which is costly.
How to reduce data redundancy in DBMS

• We can reduce data redundancy using the following methods:

• Database Normalization: We can normalize the data using the


normalization method. In this method, the data is broken down into
pieces, which means a large table is divided into two or more small
tables to remove redundancy. Normalization removes insert anomaly,
update anomaly, and delete anomaly.
• Deleting Unused Data: It is important to remove redundant data from
the database as it generates data redundancy in the DBMS. It is a good
practice to remove unwanted data to reduce redundancy.
• Master Data: The data administrator shares master data across
multiple systems. Although it does not remove data redundancy, but it
updates the redundant data whenever the data is changed
Decomposition

• Decomposition means dividing a relation R into {R1, R2,......Rn}. It is


dependency preserving and lossless.

• Dependency preserving decomposition


• Let R is decomposed into {R1, R2,....,Rn} with projected FD set
{F1,F2,......Fn}. This decomposition is dependency preserving if
F+ ={F1 U F2 U.........Fn}+.
• Example
• Let the relation R{A,B,C,D,E} F:{AB->C, C->D, AB->D} R is
decomposed to R1(A,B,C), R2(D,E). Prove decomposition is dependency
preserving.

• Solution
• F1={AB->C}
• F2={C->D}
• => (F1 u F2) = {AB->C, C->D}
Decomposition is not preserving

• Now consider another example where decomposition is not preserved.


• Let the relation R{A,B,C,D,E,F,G,H,I,J} where F: {AB->C, A->DE,
B->F, F->GH. D->IJ}
• R is decomposed to R1(A,B,C,D), R2(D,E), R3(B,F), R4(F,G,H) AND
R5(D,I,J). Check decomposition is dependency preserving or not.
• Solution
• F1={AB->C}
• F2={}
• F3={B->F}
• F4={F->GH}
• F5={D->IJ}
• => (F1 U F2 U F3 U F4 U F5) = {AB->C, B->F, F->GH, D->IJ}
• A+ under (F1 U F2 U F3 U F4 U F5) = {AB->C, B->F, F->GH, D->IJ}
• =>A->DE is not under (F1 U F2 UF3 U F4 U F5)
• =>F+ ≠ (F1 U F2 U F3 U F4 U F5)+
• => Decomposition is not dependency preserving.
Lossless-join decomposition

• It is a process in which a relation is decomposed into two or more


relations.
• This property guarantees that the extra or less tuple generation
problem does not occur, and no information is lost from the original
relation during the decomposition.
• It is also known as non-additive join decomposition.
• The decomposition is lossless when it satisfies the following statement

• If we union the sub–Relation R1 and R2 then it must contain all the
attributes that are available in the original relation R before
decomposition.
• Intersections of R1 and R2 cannot be Null. The sub relation must
contain a common attribute. The common attribute must contain
unique data.
• The common attribute must be a super key of sub relations either
R1 or R2.
• Here,
• R = (A, B, C)
• R1 = (A, B)
• R2 = (B, C)
• The relation R has three attributes A, B, and C. The relation R is
decomposed into two relation R1 and R2. . R1 and R2 both have 2-2
attributes. The common attributes are B.
• The Value in Column B must be unique. if it contains a duplicate value
then the Lossless-join decomposition is not possible.
Draw a table of Relation R with Raw Data
−R (A, B, C)
It decomposes into the two sub relations as follows −
R1 (A, B)
• We can now check the first condition for Lossless-join decomposition.
• The union of sub relation R1 and R2 is the same as relation R.
• R1U R2 = R
• We get the following result −
Example:
EMPLOYEE_DEPARTMENT table:
The above relation is decomposed into two
relations EMPLOYEE and DEPARTMENT
• EMPLOYEE table:
DEPARTMENT table
Now, when these two relations are joined on the common
column "EMP_ID", then the resultant relation will look like:
• Hence, the decomposition is Lossless join decomposition.

You might also like