Structured Query Language (SQL)
Structured Query Language (SQL)
NEHA J MENDJOGE
DATATYPES IN SQL
char(n). Fixed length character string, with userspecified length n. varchar(n). Variable length character strings, with user-specified maximum length n. int. Integer (a finite subset of the integers that is machine-dependent). numeric(p,d). Fixed point number, with userspecified precision of p digits, with d digits to the right of decimal point. number(n)
2
NEHA J MENDJOGE
NEHA J MENDJOGE
CREATE
An SQL relation is defined using the create table command: create table r (A1 D1, A2 D2, ..., An Dn) Where A1 = attribute name D1 = datatype
NEHA J MENDJOGE
Example: Branch(bid, bname, addr) Create table branch ( Bid int, Bname varchar(10), Addr varchar(20) )
ALTER
ALTER TABLE < table name > ADD (<new column name><data type> (<size>) ; <New column name><data type> (<size>);
NEHA J MENDJOGE
DROP ALTER TABLE <table name> DROP COLUMN <column name>; Eg: Alter table branch Drop column telno; Drop table branch;
NEHA J MENDJOGE
INSERT
UPDATE UPDATE <table name> SET <column name>= <expression >, <Column name >=<expression >;
NEHA J MENDJOGE
10
NEHA J MENDJOGE
11
BASIC STRUCTURE
12
The select clause list the attributes desired in the result of a query
E.g. find the names of all branches in the loan relation select branch-name from loan
Find the names of all branches in the loan relations, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates not be removed. select all branch-name from loan
NEHA J MENDJOGE
13
An asterisk in the select clause denotes all attributes select * from loan The query: select loan-number, branch-name, amount 100 from loan would return a relation which is the same as the loan relations, except that the attribute amount is multiplied by 100.
14
NEHA J MENDJOGE
The where clause specifies conditions that the result must satisfy
NEHA J MENDJOGE
To find all loan number for loans made at the Perryridge branch with loan amounts greater than $1200. select loan-number from loan where branch-name = Perryridge and amount > 1200
15
NEHA J MENDJOGE
16
NEHA J MENDJOGE
SET OPERATORS
18
The UNION operator returns results from both queries after eliminating duplications.
The UNION ALL operator returns results from both queries, including all duplications.
Use UNION to return all distinct rows Use UNION ALL to returns all rows, including duplicates Use INTERSECT to return all rows shared by both queries Use MINUS to return all distinct rows selected by the first query but not by the second
27
NEHA J MENDJOGE
AGGREGATE FUNCTIONS
These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values
28
NEHA J MENDJOGE
The average 3500 salary in EMPLOYEES 6400 table for each department.
10033
All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.
Add up the salaries in the EMPLOYEES table for each job, grouped by department.
branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)
33
NEHA J MENDJOGE
SUBQUERIES
Employee(eid, lname, jobid, commission, hire_date, did,salary) Department (did, dname, location_id) Location (location_id, city)
34
NEHA J MENDJOGE
Allotted(eno,pno,date,task)
Project(pno,location,type,deadline,budget)
36