15
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.

Insert & Delete

  • Upload
    magee

  • View
    21

  • Download
    1

Embed Size (px)

DESCRIPTION

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. For conceptual simplicity, these all involve sets of tuples. Updating a Relation. - PowerPoint PPT Presentation

Citation preview

Page 1: Insert & Delete

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.

Page 2: Insert & Delete

Updating a Relation

Having created a relation, we need to be able to : Insert tuples into it, Delete tuples from it, 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.

Page 3: Insert & Delete

Relational Assignment In principle, all that is needed for all updating is

relational assignmentwhich 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.

Page 4: Insert & Delete

Example of Insertion

ENo1233

121E3

E5E1

567

EName

7

565Smith

MitchellRobson

246

M-S

6

242SMD

246

Sal

6

24212,500

21,00032,500

444E6E8

888BlakeJones

888MW

88854,00068,000

EMPLOYEE

444E2E4

888NashArcher

888MS

88850,00040,000

Insert 444E2E4

888NashArcher

888MS

88850,00040,000

ENo1233

121E3

E5E1

567

EName

7

565Smith

MitchellRobson

246

M-S

6

242SMD

246

Sal

6

24212,500

21,00032,500

444E6E8

888BlakeJones

888MW

88854,00068,000

EMPLOYEE Value

Variable

Resulting value of variable

Page 5: Insert & Delete

Insertion in Principle

RelVar Insert ‘set of tuples’

This is a set of tuple values.

This is the name of anyrelation 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 relationalexpression, or any combination of these.

In principle, therecan be any numberof tuples in the set,including zero !

The tuples can be in anyorder, because they are a set.

There should be no tuples in commonwith “RelVar”, or an error will result.

Page 6: Insert & Delete

SQL : Insert

There are 2 forms of SQL syntax :-

Insert Into RELATION_NAMEValues ( a value for each attribute in one tuple ) ;

Insert Into RELATION_NAMESelect …..

From …

…..

A tuple literal is writteninside the brackets.

The retrieval of either a namedrelation or of any SQL expressionis written here.

Page 7: Insert & Delete

Examples of SQL Insert1. 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 EMPLOYEESelect *From EMP ;

SQL permits only one tuple literalto be entered at a time.So two insertions have to be made.

Page 8: Insert & Delete

Insertion : Attribute Order Although a tuple is a set of attributes, the attribute values have

to be written out in some physical order.

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 !

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.

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.

Page 9: Insert & Delete

SQL : Insert with Attribute Order

The 2 revised forms of SQL syntax are :-

Insert Into RELATION_NAME

( names of all the attributes in sequence )

Values ( a value for each attribute in one tuple ) ;

Insert Into RELATION_NAME

( names of all the attributes in sequence )

Select …..

From …

…..

Page 10: Insert & Delete

Further Examples of SQL Insert1. 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 couldhave been different to the first one.

Any attribute sequencecould have been used.

Page 11: Insert & Delete

SQL : Insertions Involving NULLs Sometimes tuples containing nulls must be inserted. 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 ) ;

Use the Insert statement with attribute names; omitnames 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

Page 12: Insert & Delete

Example of Deletion

ENo1233

121E3

E5E1

567

EName

7

565Smith

MitchellRobson

246

M-S

6

242SMD

246

Sal

6

24212,500

21,00032,500

444E6E8

888BlakeJones

888MW

88854,00068,000

EMPLOYEE

Delete

Value

Variable

Resulting value of variable

12121E3

E556565Smith

Mitchell24242SM

2424212,500

21,000

ENo EName M-S Sal33E1 77Robson 66D 6632,500444E6

E8888Blake

Jones888MW

88854,00068,000

EMPLOYEE

Page 13: Insert & Delete

Deletion in Principle

RelVar Delete ‘set of tuples’

Typically an expressionis used that picks outsome particular tuples.

In principle, therecan be any numberof tuples in the set,including zero !If all the RelVar’s tuples

are deleted, the RelVarremains but is empty.

This is the name of anyrelation 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 relationalexpression, or any combination of these.

All tuples should be in “RelVar”,or an error will result.

Page 14: Insert & Delete

SQL : Delete

The SQL syntax is :-

Delete From RELATION_NAMEWhere 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.

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).

Page 15: Insert & Delete

Examples of SQL Delete1. 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 EMPLOYEEWhere 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 EMPLOYEEWhere 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 tuplesdeleted depends onhow many tuplessatisfy the condition.