Search Header Logo
DBMS-2

DBMS-2

Assessment

Presentation

Computers

University

Hard

Created by

Veda V

Used 8+ times

FREE Resource

24 Slides • 4 Questions

1

DBMS-Concepts

(Session 2)



By

V.Veda Sri

Asst.Professor

Dept. of Comp.Science

RBVRR Women's College

Slide image

2

SQL JOIN

  • In SQL, JOIN clause is used to combine the records from two or more tables in a database.

  • Types of SQL JOIN: INNER JOIN, LEFT JOIN, RIGHT JOIN ,FULL JOIN.

3

Slide image

4

1. INNER JOIN

  • In SQL, INNER JOIN selects records that have matching values in both tables as long as the condition is satisfied. It returns the combination of all rows from both the tables where the condition satisfies.

  • Syntax

    SELECT table1.column1, table1.column2, table2.column1,....   FROM table1    INNER JOIN table2   ON table1.matching_column = table2.matching_column;  

5

Query

SELECT EMPLOYEE.EMP_NAME, 

PROJECT.DEPARTMENT   FROM EMPLOYEE  

INNER JOIN PROJECT  

ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Slide image

6

2. LEFT JOIN

  • The SQL left join returns all the values from left table and the matching values from the right table. If there is no matching join value, it will return NULL.

  • Syntax

    SELECT table1.column1, table1.column2, table2.column1,....   FROM table1    LEFT JOIN table2   ON table1.matching_column = table2.matching_column;  

7

Query

SELECT EMPLOYEE.EMP_NAME, 

PROJECT.DEPARTMENT   

FROM EMPLOYEE  

LEFT JOIN PROJECT  

ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Slide image

8

3. RIGHT JOIN

  • In SQL, RIGHT JOIN returns all the values from the values from the rows of right table and the matched values from the left table. If there is no matching in both tables, it will return NULL.

  • Syntax

    SELECT table1.column1, table1.column2, table2.column1,....   FROM table1    RIGHT JOIN table2   ON table1.matching_column = table2.matching_column; 

9

Query

SELECT EMPLOYEE.EMP_NAME, 

PROJECT.DEPARTMENT   

FROM EMPLOYEE  

RIGHT JOIN PROJECT   ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Slide image

10

4. FULL JOIN

  • In SQL, FULL JOIN is the result of a combination of both left and right outer join. Join tables have all the records from both tables. It puts NULL on the place of matches not found.

  • Syntax

    SELECT table1.column1, table1.column2, table2.column1,....   FROM table1    FULL JOIN table2   ON table1.matching_column = table2.matching_column;  

11

Query

SELECT EMPLOYEE.EMP_NAME, 

PROJECT.DEPARTMENT   

FROM EMPLOYEE  

FULL JOIN PROJECT    ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;  

Slide image

12

Slide image

13

Multiple Choice

Insert the missing parts in the JOIN clause to join the two tables Orders and Customers, using the CustomerID field in both tables as the relationship between the two tables.

SELECT *

FROM Orders

LEFT JOIN Customers

_______=________

1

ON Orders.CustomerID=

Customers.CustomerID;

2

Customers.CustomerID=

orders.customerID

3

Orders.CustomerID=

Customers.CustomerID;

4

ON Customers.CustomerID=

Orders.CustomerID

14

Multiple Choice

Choose the correct JOIN clause to select all the records from the Customers table plus all the matches in the Orders table.

SELECT *

FROM Orders

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

ON Orders.CustomerID=Customers.CustomerID;

1

JOIN Customers

2

RIGHT JOIN Customers

3

RIGHT JOIN Orders

4

OUTER JOIN Customers

15

Multiple Choice

The LEFT JOIN keyword returns all records from the left table (Customers), even if there are ______ in the right table (Orders).

1

matches

2

no matches

16

Multiple Choice

The FULL OUTER JOIN keyword returns all matching records from both tables whether the other table _______.

1

only matches

2

only not matches

3

matches or not

4

None

17

Slide image

18

SQL Constraints

  • SQL constraints are used to specify rules for the data in a table.

  • This ensures the accuracy and reliability of the data in the table.

  • Constraints can be column level or table level.

19

The following constraints are commonly used in SQL

  • NOT NULL - Ensures that a column cannot have a NULL value

  • UNIQUE - Ensures that all values in a column are different

  • PRIMARY KEY - A combination of a NOT NULL and UNIQUE. 

  • FOREIGN KEY - Uniquely identifies a row/record in another table

  • CHECK - Ensures that all values in a column satisfies a specific condition

  • DEFAULT - Sets a default value for a column when no value is specified

  • INDEX - Used to create and retrieve data from the database very quickly

20

Example

CREATE TABLE Persons (

   ID int NOT NULL UNIQUE/PRIMARY KEY,

   LastName varchar(255) NOT NULL,

   FirstName varchar(255) NOT NULL,

   Age int CHECK (Age>=18),

City varchar(255) DEFAULT 'Sandnes'

);

21

1. Domain constraints

  • Domain constraints can be defined as the definition of a valid set of values for an attribute.

  • The data type of domain includes string, character, integer, time, date, currency, etc. The value of the attribute must be available in the corresponding domain.

22

Slide image

Example

23

2. Entity integrity constraints

  • The entity integrity constraint states that primary key value can't be null.

  • This is because the primary key value is used to identify individual rows in relation and if the primary key has a null value, then we can't identify those rows.

  • A table can contain a null value other than the primary key field.

24

Slide image

Example

25

3. Referential Integrity Constraints

  • A referential integrity constraint is specified between two tables.

  • In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table 2.

26

Slide image

Example

27

4. Key constraints

  • Keys are the entity set that is used to identify an entity within its entity set uniquely.

  • An entity set can have multiple keys, but out of which one key will be the primary key. A primary key can contain a unique and null value in the relational table.

28

Slide image

Example

DBMS-Concepts

(Session 2)



By

V.Veda Sri

Asst.Professor

Dept. of Comp.Science

RBVRR Women's College

Slide image

Show answer

Auto Play

Slide 1 / 28

SLIDE