0% found this document useful (0 votes)
56 views

Relational Model Basics

sql slides

Uploaded by

Avinash Garg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Relational Model Basics

sql slides

Uploaded by

Avinash Garg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Relational Model basics:

Data is viewed as stored in two dimensional tables, these tables are also known as relations A table (relation) consists of unique columns (attributes) and rows (tuples) If the value to be inserted into a cell is unknown, or it does not have any value. It is represented as NULL NULL is not equivalent to zero, blank or empty string If a database is organized based on relational data model then it is called Relational Database A DBMS that deals with the relational database is known as RDBMS

Keys:
Candidate key Any set of attribute (minimal) which can uniquely identify a tuple/row in a given relation/table is called candidate key. Primary Key Database designer selects one of the candidate keys as primary key for the purpose of identification of a row uniquely. Alternate Key All the candidate keys except the one which has been selected as primary key are called alternate keys.
No of Alternate Keys = No of Candidate Keys - 1

Key Attribute/Non Key Attribute


Key Attributes The attributes that participate in the Candidate key are Key Attributes

Non-Key Attributes The attributes other than the Candidate Key attributes in a table/relation are called Non-Key attributes. OR The attributes which do not participate in the Candidate key.

Example:
Given a relation Trainee(Empno, FirstName, LastName, Email, PhoneNo)

Assumptions: Empno for each trainee is different. Email for each trainee is different PhoneNo for each trainee is different Combination of FirstName and LastName for each trainee is different
Candidate key: {Empno},{Email},{PhoneNo},{FirstName,LastName}

Primary key:
{Empno} Alternate Key: {Email},{PhoneNo},{FirstName,LastName}

Foreign Key:
Foreign key The set of attributes whose values are required to match with the values of a column in the same or another table.
dept (Parent /Master/Referenced Table) emp (Child /Referencing Table)

Points to remember Foreign key values do not (usually) have to be unique. Foreign keys can also be null . To enter the data in child table corresponding data must be present in master table or NULL is the default entry in child table in the referenced column ( FK column)

SQL Statements:
DDL (Data Definition Language)
Create Alter Drop Truncate

DML (Data Manipulation Language)


Insert Update Delete SELECT

DCL (Data Control Language)


Grant Revoke Commit Rollback

SQL CREATE TABLE


Implementing PRIMARY KEY ,NOT NULL and UNIQUE

Copyright 2008, Infosys Technologies Ltd.

Confiden

SQL Data Definition Language <create>: .

create:
Implementing PRIMARY KEY ,CHECK,NOT NULL and UNIQUE constraints

EXAMPLE : CREATE TABLE customer (customerid VARCHAR(6) CONSTRAINT customer_pk PRIMARY KEY CONSTRAINT customer_cid CHECK (customerid LIKE 'C%'), customername VARCHAR(30) CONSTRAINT customer_customername_nnull NOT NULL, dateofreg DATE , userid VARCHAR(15) CONSTRAINT customer_userid_uq UNIQUE, password VARCHAR(15) CONSTRAINT customer_passwd NOT NULL);

Add/Drop/Modify Column

Syntax:
ALTER TABLE <TABLE NAME> (ADD /MODIFY /DROP <COLUMN NAME> ALTER TABLE customer ADD contactphone Char(10); ALTER TABLE customer MODIFY contactphone Char(12); ALTER TABLE customer DROP (contactphone);

SQL -DROP Table


DROP TABLE
Deletes TABLE structure Cannot be recovered Use with caution

DROP TABLE customerdemo;

Truncate Table:
Deleting All Rows of a TABLE TRUNCATE TABLE customerdemo;

1 4

SQL - INSERT INTO


INSERT INTO table-name [ column-name(s) ] VALUES ( ----------- constant (s) -------------) NULL

INSERT INTO customer VALUES(C4',Allan','13-Mar-09', Allan1004', Allan@123');

Copyright 2008, Infosys Technologies Ltd.

Confiden

SQL-DML

Insert into:
INSERT INTO table-name [ column-name(s) ] VALUES ( ----------- constant (s) -------------) NULL

INSERT INTO customer VALUES(C4',Allan','13-Mar-09', Allan1004', Allan@123');

Insert Into:
Inserting NULL in to the TABLE-Method1 INSERT INTO customer VALUES('C5','Simon',NULL,'Symon1005','Symon@123');

INSERT INTO customer( customerid, customerfname, useid, password) VALUES( 'C5', 'Simon, 'Symon1005', 'Symon@123');

SQL DELETE FROM


With or without WHERE clause Syntax: DELETE FROM <TABLE NAME> WHERE condition Deleting All Rows DELETE FROM customer; Deleting Specific Rows

DELETE FROM customer WHERE customerid = C1;

SQL-update
Syntax: UPDATE <TABLE NAME> SET columnname =value [ WHERE condition] Updating All Rows UPDATE customer SET dateofreg = NULL;

Updating Particular rows UPDATE customer SET dateofreg = NULL WHERE customerid = 'C1; UPDATE customer SET dateofreg = NULL , password = John@321 WHERE customerid = 'C1;

Retrieving columns from TABLE:


l

RETRIEVING ALL COLUMNS: SELECT * FROM customer; Retrieving only customerid and userid FROM customer TABLE SELECT customerid, userid FROM customer;

Get all customer names: SELECT ALL customername FROM customer; Get all distinct customer names SELECT DISTINCT customername FROM customer;

2 0

Retrieving a subset of rows (Working of WHERE Clause)


Select customerid and userid of the customer whose date of registration is 13 March 2009.

SELECT

customerid, userid FROM customer WHERE dateofreg ='13-Mar-2009;

Copyright 2008, Infosys Technologies Ltd.

Confiden

Retrieving rows based on condition:


COL1,COL2,......... FROM TABLE NAME lSyntax: WHERE < SEARCH CONDITION>; SELECT

SELECT

customerid, userid FROM customer WHERE dateofreg ='13-Mar-2009;

Relational Operators:
Relational operators = , < , > , <= , >= , != or < >
List all items whose unit price is > 100
SELECT itemid,itemname FROM item WHERE unitprice > 100;

List the customerid and userid of Allan


SELECT customerid, userid FROM customer WHERE customername = Allan;

Logical Operators:
Logical operator: AND, OR, and NOT
lList either the unit price is 100 lessand than 100of ormeasurement Unit of List all all items items where Unit Price is less than Unit ismeasurement Dozen. is Dozen

LIKE:
l

Retrieval using LIKE: List all customers whose name starts with A and has l as the second character SELECT customerid,customername FROM customer WHERE customername LIKE Al%;

List all customer whose name has a as the second character. SELECT customerid,customername FROM customer WHERE customername LIKE _a%;

JOINS:
Cartesian Product Inner join Outer join

Left-outer join Right-outer join

2 6

Cartesian Product Or Cross Join


In Cartesian product All rows from first table are combined with all rows from the second table Example SELECT * FROM table1,table2;

Copyright 2008, Infosys Technologies Ltd.

Confiden

CARTESIAN PRODUCT OR CROSS JOIN


In Cartesian product All rows from first table are combined with all rows from the second table Example SELECT * FROM table1,table2;

INNER JOINS:
Get all combinations of emp and cust information such that the emp and cust are co-located.
SELECT table1.empid, table1.city, table2.custid, table2.city FROM table1, table2 WHERE table1.city = table2.city;

We define inner join as the Cartesian product which satisfies the join conditi

OUTER JOIN:
Retrieve all rows that match the WHERE clause and also those that have a NULL value in the column used for join. LEFT/RIGHT -OUTER JOIN:

Left outer joins include all records from the first (left) of two tables, A = B (+)
Right outer joins include all records from the second (right) of two tables, A (+) = B

EXAMPLE OF LEFT JOIN:


.List all cities of table1 if there is match in cities in table2 & also unmatched
Cities from table1

SELECT table1.empid, table1.city, table2.custid, table2.city FROM table1, table2 WHERE table1.city = table2.city (+);
Table1 Emp_ID A1 A2 A3 A4 A5 CITY New YorK NULL Chicago Chicago Paris Table2 Cust_ID B1 B2 B3 B4 B5 CITY New York New York NULL Chicago Moscow

INNER JOIN Unmatched rows

Left_Outer_Join Table Table1.Emp_ID Table1.City A1 New York A1 New York A3 Chicago A4 Chicago A5 Paris A2 NULL

Table2.Cust_ID Table2.City B1 New York B2 New York B4 Chicago B4 Chicago NULL NULL NULL NULL

EXAMPLE OF RIGHT JOIN:


List all cities of table2 if there is match in cities in table1 & also unmatched Cities from table2
SELECT table1.empid, table1.city, table2.custid, table2.city FROM table1, table2 WHERE table1.city (+) = table2.city;
Table1 Emp_ID A1 A2 A3 A4 A5 CITY New YorK NULL Chicago Chicago Paris Table2 Cust_ID B1 B2 B3 B4 B5 CITY New York New York NULL Chicago Moscow Unmatched rows

INNER JOIN

Right_Outer_Join Table Table1.Emp_ID Table1.City A1 New York A1 New York A3 Chicago A4 Chicago NULL NULL NULL NULL

Table2.Cust_ID Table2.City B1 New York B2 New York B4 Chicago B4 Chicago B5 Moscow B3 NULL

You might also like