Top Banner
CSE 312, DIU MOHSHI MASNAD 1
35

Basic SQL Command

Jan 22, 2018

Download

Data & Analytics

Amin Omi
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Basic SQL Command

CSE 312, DIU

MOHSHI MASNAD

1

Page 2: Basic SQL Command

What we have done so far……..

LOGICAL TESTS

AGGREGATE FUNCTIONS (BASIC)

JOINING OPERATIONS

SIMPLE SUB QUERY

SET OPERATIONS

GROUP BY

HAVING

2

Page 3: Basic SQL Command

What we are going to learn.…

ALIASES

SET COMPARISONS

CHECKING EMPTY RELATION

VIEW

INTEGRITY CONSTRAINTS

CHECK

Primary Key

Foreign Key

3

Page 4: Basic SQL Command

Our Tables

4

Page 5: Basic SQL Command

ALIASES12

5

Page 6: Basic SQL Command

SET

comparisons13

6

Page 7: Basic SQL Command

SET

comparisons13

7

Page 8: Basic SQL Command

SET

comparisons13

8

Page 9: Basic SQL Command

check for

EMPTY13

9

Similarly NOT EXISTS can be used if necessary

Page 10: Basic SQL Command

10

User 1

User 2

ID Name Address Age

Select ID , Name from Orginal_Table

Select Name, Address, Age from Orginal_Table

Boss of

Orginal_Table

Scenario 1

Page 11: Basic SQL Command

11

User 1

User 2

ID Name Address Age

Boss of

Orginal_Table

VIEW of the Desired

portion of Original_Table

VIEW of the Desired

portion of Original_Table

Page 12: Basic SQL Command

A1 A2 A3 A4 A5

B1 B2 B3

C1 C2

Scenario 2

Page 13: Basic SQL Command

A1 A2 A3 A4 A5

B1 B2 B3

C1 C2

Intermediate Table

(VIEW)

Result

Scenario 2

Page 14: Basic SQL Command

The

VIEW14

Page 15: Basic SQL Command

The

VIEW14

Page 16: Basic SQL Command

The

VIEW14

Page 17: Basic SQL Command

The

VIEW14

Page 18: Basic SQL Command

The

VIEW14

Page 19: Basic SQL Command

CONSTRAINTOn a single relation

19

NOT NULL

UNIQUE

CHECK

16

Page 20: Basic SQL Command

PRIMARY KEY

20

16

Page 21: Basic SQL Command

PRIMARY KEY

21

16

Page 22: Basic SQL Command

PRIMARY KEY

22

16

Page 23: Basic SQL Command

PRIMARY KEY

23

16

Page 24: Basic SQL Command

Referential Integrity

THE FOREIGN KEY

24

Attribute 3

10

11

11

12

12

Attribute 1

10

11

12

Table_1 (CHILD)

Table_2 (PARENT)

FOREIGN KEY

17

Page 25: Basic SQL Command

Referential Integrity

THE FOREIGN KEY

25

17

Page 26: Basic SQL Command

Referential Integrity

THE FOREIGN KEY

26

17

Page 27: Basic SQL Command

Referential Integrity

THE FOREIGN KEY

27

17

Page 28: Basic SQL Command

Referential Integrity

THE FOREIGN KEY

28

17

Page 29: Basic SQL Command

NAMING

CONSTRAINTS

29

18

Page 30: Basic SQL Command

DROPPING A TABLE

with constraints

30

20

Page 31: Basic SQL Command

31

DROPPING A TABLE

with constraints20

Page 32: Basic SQL Command

32

ALTER : changing the table structure21

Page 33: Basic SQL Command

33

ALTER : changing the table structure21

Page 34: Basic SQL Command

34

ALTER : changing the table structure

The clause cascade automatically

disables foreign key constraints that depend

on the (disabled) primary key

21

Page 35: Basic SQL Command

CLASS

TASK

1.Create a view showing the students’ ID and their corresponding number of phone no. entries.

2.Write any SQL example using NOT EXIST.3.Find out the ID of the students whose semesters are above all the

semesters of the students who have informed their blood group and contact information.

4.From (3), If at least 1 result is found, then show their CGPA’s .5.Create DEPARTMENT and EMPLOYEE tables according to given

schema and design with appropriate constraints (unique, not null,

check and primary key)

6.Now put referential integrity where necessary.7.Add a new column EMP_AGE in EMPLOYEE table.

8.Impose ON DELETE CASCADE and check deletion.

STUDENT (ID, NAME, SEMESTER, DATE_OF_BIRTH)

STUDENT _RESULT (ID, CGPA)

STUDENT _CONTACT (ID, PHONE_NO)

STUDENT _BLOOD_GROUP (ID, B_GROUP)

DEPARTMENT (DEPT_ID, DEPT_NAME)

EMPLOYEE (EMP_ID, EMP_NAME, EMP_DEPT)