Download presentation
Presentation is loading. Please wait.
Published byAmbrose Moody Modified over 9 years ago
1
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To consider how to do insertions and deletions in SQL.
2
Updating a Relation Having created a relation, we need to be able to : l Insert tuples into it, l Delete tuples from it, l Amend tuples in it. It is also essential that, having changed one or more tuples in a relation, the changed value of the relation still satisfies all the integrity constraints. The insertion and deletion of tuples is considered in this lecture, the amendment of tuples and handling of integrity constraints in the next lecture. For conceptual simplicity, these all involve sets of tuples.
3
Relational Assignment l In principle, all that is needed for all updating is relational assignment which is the relational equivalent of value assignment in a 3GL programming language. 3GL : y 3 + x Relation : RelVar “expression evaluating to set of tuples” From the user’s perspective, it is more convenient to have special assignments that execute insertions, deletions and amendments. RelVar Insert ‘set of tuples’ RelVar Delete ‘set of tuples’ The RHS set of tuples is inserted / deleted from the LHS relational variable.
4
Example of Insertion ENo 1 2 33 1 2 1E3 E5 E1 5 6 7 EName 7 5 6 5Smith Mitchell Robson 2 4 6 M-S 6 2 4 2S M D 2 4 6 Sal 6 2 4 212,500 21,000 32,500 444E6 E8 888Blake Jones 888M W 88854,000 68,000 EMPLOYEE 444E2 E4 888Nash Archer 888M S 88850,000 40,000 Insert 444E2 E4 888Nash Archer 888M S 88850,000 40,000 ENo 1 2 33 1 2 1E3 E5 E1 5 6 7 EName 7 5 6 5Smith Mitchell Robson 2 4 6 M-S 6 2 4 2S M D 2 4 6 Sal 6 2 4 212,500 21,000 32,500 444E6 E8 888Blake Jones 888M W 88854,000 68,000 EMPLOYEE Value Variable Resulting value of variable
5
Insertion in Principle RelVar Insert ‘set of tuples’ This is a set of tuple values. This is the name of any relation in the DB. The tuples could be written out literally, be the value of a named relation variable, be the result of evaluating a complex relational expression, or any combination of these. In principle, there can be any number of tuples in the set, including zero ! The tuples can be in any order, because they are a set. There should be no tuples in common with “RelVar”, or an error will result.
6
SQL : Insert There are 2 forms of SQL syntax :- l Insert Into RELATION_NAME Values ( a value for each attribute in one tuple ) ; l Insert Into RELATION_NAME Select ….. From … ….. A tuple literal is written inside the brackets. The retrieval of either a named relation or of any SQL expression is written here.
7
Examples of SQL Insert 1. Insert 2 literal tuples, i.e. the earlier graphical example : Insert Into EMPLOYEE Values ( ‘E2’, ‘Nash’, ‘M’, 50,000 ) ; Insert Into EMPLOYEE Values ( ‘E4’, ‘Archer’, ‘S’, 40,000 ) ; 2. Insert the contents of relation EMP into EMPLOYEE : Insert Into EMPLOYEE Select * From EMP ; SQL permits only one tuple literal to be entered at a time. So two insertions have to be made.
8
Insertion : Attribute Order l Although a tuple is a set of attributes, the attribute values have to be written out in some physical order. l Attribute values are matched with the relvar’s corresponding attributes by attribute name, so that the physical order doesn’t matter. But we haven’t used any attribute names so far ! l This is because the default sequence has been used, which is the sequence in which the attributes appeared in the Create Table statement. If the attribute values are written out in the same physical sequence, then they will be matched by position. l To avoid relying on the default sequence, the attribute names can be written out in any desired sequence as an additional parameter, and then the attribute values written in this same sequence, in order to match value with attribute.
9
SQL : Insert with Attribute Order The 2 revised forms of SQL syntax are :- l Insert Into RELATION_NAME ( names of all the attributes in sequence ) Values ( a value for each attribute in one tuple ) ; l Insert Into RELATION_NAME ( names of all the attributes in sequence ) Select ….. From … …..
10
Further Examples of SQL Insert 1. Insert 2 literal tuples, i.e. the earlier graphical example : Insert Into EMPLOYEE ( EName, Sal, ENo, M-S ) Values ( ‘Nash’, 50,000, ‘E2’, ‘M’ ) ; Insert Into EMPLOYEE ( EName, Sal, ENo, M-S ) Values ( ‘Archer’, 40,000, ‘E4’, ‘S’ ) ; 2. Insert the contents of relation EMP into EMPLOYEE : Insert Into EMPLOYEE ( EName, M-S, Sal, ENo ) Select EName, M-S, Sal, ENo From EMP ; The second attribute sequence could have been different to the first one. Any attribute sequence could have been used.
11
SQL : Insertions Involving NULLs Sometimes tuples containing nulls must be inserted. l Use the Insert statement without an attribute name sequence parameter; use the keyword NULL for the relevant attribute(s). Example :- Insert Into EMPLOYEE Values ( ‘E9’, ‘Collins’, NULL, null ) ; l Use the Insert statement with attribute names; omit names and values of attributes that are to receive nulls. Example :- Insert Into EMPLOYEE (EmpNo, EName) Values ( ‘E9’, ‘Collins’ ) ; No speech marks. Upper or lower case. Same
12
Example of Deletion ENo 1 2 33 1 2 1E3 E5 E1 5 6 7 EName 7 5 6 5Smith Mitchell Robson 2 4 6 M-S 6 2 4 2S M D 2 4 6 Sal 6 2 4 212,500 21,000 32,500 444E6 E8 888Blake Jones 888M W 88854,000 68,000 EMPLOYEE Delete Value Variable Resulting value of variable 1 2 1 2 1E3 E5 5 6 5 6 5Smith Mitchell 2 4 2 4 2S M 2 4 2 4 212,500 21,000 ENoENameM-SSal 33E177Robson66D6632,500 444E6 E8 888Blake Jones 888M W 88854,000 68,000 EMPLOYEE
13
Deletion in Principle RelVar Delete ‘set of tuples’ Typically an expression is used that picks out some particular tuples. In principle, there can be any number of tuples in the set, including zero ! If all the RelVar’s tuples are deleted, the RelVar remains but is empty. This is the name of any relation in the DB. This is a set of tuple values. The tuples could be written out literally, be the value of a named relation variable, be the result of evaluating a complex relational expression, or any combination of these. All tuples should be in “RelVar”, or an error will result.
14
SQL : Delete l The SQL syntax is :- Delete From RELATION_NAME Where condition ; In the Where phrase, a condition is written that picks out the required tuples from RELATION_NAME. These tuples are then deleted from RELATION_NAME. l The Where phrase is optional. One can just write Delete From RELATION_NAME ; In this case, all the tuples in RELATION_NAME are deleted from it (but RELATION_NAME still exists, albeit empty).
15
Examples of SQL Delete 1. Delete 2 tuples, i.e. the earlier graphical example. This will be done by specifying tuples where the salary is less than £30,000 : Delete From EMPLOYEE Where Sal < 30000 ; 2. Delete all the tuples in EMPLOYEE : Delete From EMPLOYEE ; Relation EMPLOYEE is now empty. 3. Delete a specific tuple, i.e. the one referring to employee ‘E1’ : Delete From EMPLOYEE Where ENo = ‘E1’ ; Because ‘ENo’ is a candidate key, we can be sure that we have deleted one specific tuple, which will correspond to the right person. The number of tuples deleted depends on how many tuples satisfy the condition.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.