DB - Day3

Joins

Examples will be provided on this two tables Student Table

Sid
Sname
did

1

ahmed

10

2

khalid

10

3

eman

20

4

omar

NULL

Department Table

Did
Dname

10

SD

20

HR

30

IS

40

Admin

Type Of Joins

Cross Join

Cartesian Product

SELECT Sname,Dname
FROM Student,Department
SELECT Sname,Dname
FROM student CROSS JOIN Department
Sname
Dname

ahmed

SD

khalid

SD

eman

SD

omar

SD

ahmed

HR

khalid

HR

eman

HR

omar

HR

ahmed

IS

khalid

IS

eman

IS

omar

IS

ahmed

Admin

khalid

Admin

eman

Admin

omar

Admin

Inner Join

Equi Join Only Matched Values No null Values from any table FK =PK

These two queries are the same

Sname
Dname

ahmed

SD

khalid

SD

eman

HR

Outer Join

Left Outer Join If You want every row from the left table even if there no matched values

Sname
Dname

ahmed

SD

khalid

SD

eman

HR

Omar

NULL

Right Outer Join If You want every row from the right table even if there no matched values

Sname
Dname

ahmed

SD

khalid

SD

eman

HR

NULL

IS

NULL

ADMIN

Full Outer Join If You want every row from the right and left tables even if there no matched values its combination of Left And Right Outer Join

Sname
Dname

ahmed

SD

khalid

SD

eman

HR

omar

NULL

NULL

IS

NULL

ADMIN

Self Join

Unary Relation Primary Key and foreign key is in the same table

eid
ename
superid

1

ahmed

NULL

2

eman

1

3

khalid

1

4

nada

2

5

ali

2

we want a query to know the employee and his supervisor name

Empname
Supername

eman

ahmed

khalid

ahmed

nada

eman

ali

eman

Examples on Database

TRY IT YOURSELF Cross Join

Inner Join

Display student name and department for students from alex

Display student name and all department data for the student

Display student name and department for student for students ages between 20 and 25 then order the result by Department name


Outer Join

Display all Student even it no department match


Self Join

Student Name and his Supervisor

Display Supervisors Name

Display all students even if they have no super


Joins With More than Two Tables

Display Student name and his courses and grades

The same output as the last query

Joining four Tables


Join With DML

Joining two table and update grades based on condition in another table


Built In Functions

ISNull

Replace null values with other value

Coalesce

Replace null values with any of multiple replacement values if first name is null display last name and if its null display address and if null display "Student has no name"


Autoincrement ID

Auto increment For Id

reset identity


Like Statement


  • _ is one character

  • % is Zero or more character Names Of student there name starts with "a"

Names Of student there name ends with "a"

Some Patterns

  • a%h starts with a and ends with h

  • %d_ the second last char is d

  • ___ three chars word

  • _m__ four chars word the second char is m

  • ___% at least four char word

  • ahm% word starts with ahm

  • [ahm]% word starts with a or h or m

  • [a-h]% word starts with range from a to h

  • [(am)(gh)]% word starts with "am" or "gh"

  • %[%] string that ends with %

  • %[_]% words contains __


Database Integrity

Domain Integrity
Entity Integrity
refrential Integrity

Range of values

Uniqueness

Relationship

DB Constraints

Data Types

Primary Key constraint

(tiny int => 1byte)

unique constraint => multi columns with unique constraints and allow one NULL

Foreign Key Constraints

Default values 'cairo'

Custom Constraints like Stored Procedures

allow NULL or NOT NULL

Check(Constraint) when Creating tables

-----------

---------------------

----------------------

-----------------

DB Objects

Rule

Index

Trigger

Trigger

Trigger

Types of constraints

  • Primary Key constraint

  • unique constraint

  • Check(Constraint)

  • Foreign Key Constraints

  • Custom Constraints like Stored Procedures


Constraints


Last updated