Relational Model Basics
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
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
Confiden
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);
Truncate Table:
Deleting All Rows of a TABLE TRUNCATE TABLE customerdemo;
1 4
Confiden
SQL-DML
Insert into:
INSERT INTO table-name [ column-name(s) ] VALUES ( ----------- constant (s) -------------) NULL
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-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 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
SELECT
Confiden
SELECT
Relational Operators:
Relational operators = , < , > , <= , >= , != or < >
List all items whose unit price is > 100
SELECT itemid,itemname FROM item WHERE unitprice > 100;
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
2 6
Confiden
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
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
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
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