SQL(Structured Query
Language)
What is SQL
SQL is the language RDBMS understands. It helps us to: Create tables Make changes in the tables Impose relationships between tables Enter, delete, update & retrieve data.
SQL - Background
Conceived in mid-1970s as a database language for the relational model Developed by IBM First standardized in 1986 by ANSI Enhanced in 1989 Revised again in 92 Non Procedural language Number of commercial products
Subdivision of SQL
Interactive SQL is used to operate directly on a DB to produce output Embedded SQL consists of SQL commands put inside the programs that are mostly written in high language (COBOL, C, ..). This makes the program more powerful and efficient.
SQL
SQL - DDL, DML, DCL DDL
CREATE ALTER DROP
DML
SELECT INSERT DELETE UPDATE
DCL
GRANT REVOKE COMMIT ROLLBACK
SQL DDL
Exercise...
ALTER TABLE EMP (ADD Grade short); ALTER TABLE EMP (DROP Grade);
ALTER TABLE EMP ADD CONSTRAINT Pkey1 PRIMARY KEY (EmpNo); ALTER TABLE EMP DROP CONSTRAINT Pkey1;
ALTER TABLE EMP ADD CONSTRAINT Fkey1 FOREIGN KEY (Mgr) REFERENCES EMP(EName); ALTER TABLE EMP DROP CONSTRAINT Fkey1;
SQL -DML
SQL - UPDATE
With or without WHERE clause
Update city of supplier table for supplier as S1 to KANPUR
Supplier table - S
SNO SNAME S1 S2 S3 S4 S5 Smith Jones Blake Clark Adams STATUS 20 10 30 20 30 CITY London Paris Paris London Athens
Product table - P
PNO PNAME P1 P2 P3 P4 P5 P6 Nut Bolt Screw Screw Cam Cog COLOR Red Green Blue Red Blue Red WEIGHT CITY 12 17 17 14 12 19 London Paris Rome London Paris London
Shipment table - SP
SNO S1 S1 S1 S1 S1 S1 S2 S2 S3 S4 S4 S4 PNO P1 P2 P3 P4 P5 P6 P1 P2 P2 P2 P4 P5 QTY 300 200 400 200 100 100 300 400 200 200 300 400
FROM S
Relational operators
Get SNO for all suppliers in Paris and status greater than 10
Logical operators
Retrieval using IN
Retrieval using IN
Get the list of Supplier numbers in the cities ROME, PARIS ?
Get SNO and STATUS for suppliers in Paris in descending order of status SELECT SNO, STATUS FROM S WHERE CITY=PARIS ORDER BY STATUS DESC
Retrieval using ORDER BY SELECT CITY,COLOR,WEIGHT FROM P WHERE WEIGHT IN (12,17) ORDER BY CITY,COLOR DESC
CITY COLOR WEIGHT -------------------- ---------- --------Shizuoka Blue 17 Osaka Red 12 Osaka Yellow 12
Queries involving calculated values Get PNO and WEIGHT in grams for all parts
SELECT PNO, SELECT PNO, WEIGHT*1000 WEIGHT*1000 AS FROM P Weight_grams FROM P
PNO
Weight_grams WEIGHT*1000 ?
Aggregate functionCOUNT
Get number of shipments for P2
SELECT SNO, PNO, SUM(QTY) FROM SP GROUP BY PNO PNO Wrong SNO,
SNo PNo QTY --------- --------- --------2 3 200 6 6 400 5 4 600 4 6 400 2 1 500 5 5 700 4 3 1000 3 2 600 5 4 600 2 4 400 SNo PNo SUM(QTY) ------- --------- --------2 1 500 2 3 200 2 4 400 3 2 600 4 3 1000 4 6 400 5 4 1200 5 5 700 6 6 400
Get PNO for parts which have more than two shipments SELECT PNO,COUNT(*) FROM SP GROUP BY PNO HAVING COUNT(*)>2
Independent sub-queries
Inner query is independent of outer query. Inner query is executed first and the results are stored. Outer query then runs on the stored results.
Get supplier names for all suppliers who supply part P2 SELECT SNAME FROM S WHERE SNO IN (SELECT SNO FROM SP WHERE PNO =P2)
SELECT PNO FROM SP X WHERE PNO IN (SELECT PNO FROM SP Y WHERE Y.SNO<>X.SNO)
JOIN
Inner join Outer join
Left-outer join Right-outer join
Self join
Inner Joins
Commonly type of join Combine records from two tables with matching values.
SELECT P.PNO, S.SNO FROM P, S WHERE S.CITY = P.CITY AND S.STATUS > = 20
Outer join
Retrieve all rows that match the WHERE clause and those that have a NULL
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 SNO with QTY supplied or SNO which have not yet supplied any QTY
SELECT S.SNO, SP.QTY FROM S, SP WHERE S.SNO (+)= SP.SNO;
All unmatched rows of S are also selected
SELECT SNAME FROM S WHERE NOT EXISTS (SELECT * FROM SP WHERE SP.SNO=S.SNO)
Index
Index can be created and dropped. By default RDBMS creates index on Primary Key
Views
Base tables. Views are like windows. Views are operated on by queries/DML. A view is actually a query that is executed whenever the view is subject to a command.
Creating a VIEW
Create a view from Supplier table
Creating a VIEW
Create a view from Supplier table for City in BHU
CREATE VIEW ViewSupplier AS SELECT S.SNO, S.SName, S.City FROM Supplier S WHERE City = BHU;
Often we do not specify new field names, but if we do, we will have to do so for every field in the view.
Grouped Views
CREATE VIEW ViewSupplier (Supp#, Name) AS SELECT S.SNO, S.SName FROM Supplier S WHERE City = BHU GROUP BY status;
Updation is not possible in case of Grouped views, Joined views & other complicated views.
Dropping Views
Views are dropped similar to the way in which the tables are dropped. However, you must own a view in order to drop it. DROP VIEW <view name>;
Check Option
CREATE VIEW ViewSupplier AS SELECT S.SNO, S.SName, S.City FROM Supplier S WHERE City = BHU WITH CHECK OPTION;
Security Concepts
User Authentication Database Objects Privileges
1. GRANT . database
GRANT { [DBADM[, ]] - Database administrator authority [DBCTRL[,]] - Database control authority [DBMAINT[, ]] - Database maintenance authority [CREATETAB[,]] - Privilege to create table [DROP[, ]] - Privilege to DROP/ALTER [STARTDB[, ]] - Start database [STOPDB[, ]] } - Stop database ON DATABASE database-name[,...] TO [AuthID][,...] [PUBLIC] [WITH GRANT OPTION]
GRANT . TO .
Used to grant access to new users; Permission can be granted for all DML commands; Permission is granted on a database/table/view; Permission for further grant.
GRANT Select On
User1 is an owner of Customer table. User1 wants User2 perform queries on it. User1 issues following command:
GRANT INSERT ON
User1 issues the command GRANT INSERT ON Customer to User2; GRANT SELECT, INSERT ON Customer to User2; GRANT INSERT ON Customer to User2, User3;
Restricting Privileges
GRANT SELECT, UPDATE ON Customer to User2 GRANT UPDATE(Comm) ON Customer to User2 GRANT UPDATE(CName,City) ON Customer to User2;
Example of REVOKE
REVOKE INSERT ON Customer FROM User2;