Professional Documents
Culture Documents
Chandra S. Amaravadi
IN THIS PRESENTATION
Codds rules for relational systems Types of SQL DDL and DML Examples
10
11
Action DatabaseComponent ComponentName Create Table Cust. Drop View Emp. Index
Alter Table Emp.
12
CREATE TABLE Table name (attribute attr. type, attribute attr.type..) [CONSTRAINT Constr name TYPE attr];
Command used to change attributes in tables
ALTER TABLE Table name ADD attr. attr. type, attr. attr. type;
Command used to delete table definitions
13
CREATE TABLE
Creates a table (schema)
Dept. d_no d_name d_mgr_ssn d_phone
14
15
CREATE VIEW
Command used to create VIEWS
16
CREATE VIEW..
Creates a query; AS is an alias or name Emp. e_ssn e_name e_title
17
18
19
INSERT insert a record UPDATE change values DELETE delete records SELECT choose record
20
21
22
UPDATE EMP /* table name */ SET e_title = analyst /* new values */ WHERE e_name = Sullivan; /* condition */ EMP.
e_ssn 978-98-9878 e_name Sullivan e_title analyst
23
e_name
Sullivan
e_title
analyst
24
25
Group by is used to organize data into groups and provide summary information. Having is used for the group condition.
26
27
DISCUSSION
The output of a SELECT statement is: a) an attribute ? b) a single record ? c) table ?
28
FUNCTIONS IN SQL
Operators to carry out different types of calculations Logical Arithmetic String Date
IN WHICH PART OF THE QUERY ARE FUNCTIONS USED?
29
LOGICAL OPERATORS
Logical operators are generally used to carry out comparison
=, >, < >=, <= <> OR != (NOT) BETWEEN X1 AND X2 (inclusive) LIKE _ or % IN Dept. (NOT) NULL
d_name d_phone 845-9878 989-0087 787-9934 884-5768
30
LOGICAL OPERATORS..
To select employees between certain income range
Select emp#, emp name From emp Where income between 70000 and 90000;
31
Select count(emp_name)
Select max(emp_salary)..
32
STRING FUNCTIONS
String functions are used to carry out string manipulation LENGTH(string) SUBSTR(string, start, no of ch.) LOWER UPPER
Select substr(prob_descr, 0, 10) Prob_descr = I am unable to log in
33
DATE FUNCTIONS
Date functions are used to carry out date arithmetic ADD_MONTHS(1/1/00, 5) = 6/1/00 MONTHS_BETWEEN (sysdate, hiredate) NEXT_DAY(hiredate, Friday) TO_DATE(string, picture) TO_DATE(12/09/10, DY th MM, YYYY) DEC, 2010 = 9 th
34
SIMPLE RETRIEVALS
Select employees who are analysts EMP. e_ssn e_name e_title
35
SIMPLE RETRIEVALS..
EMPLOYEE
e_ssn 456-34-8895 459-66-6785 467-89-8898 478-64-8005 489-12-5575 492-93-4438 467-89-8898 e_name Smith Johnson Weintraub Dickson Ferrel Rao McDonald e_title developer analyst developer manager analyst analyst manager e_salary $35,000 $27,000 $60,000 $64,000 $47,000 $71,000 $85,000
36
SIMPLE RETRIEVALS..
Select employees whose name ends with son EMP. e_ssn e_name e_title
37
SIMPLE RETRIEVALS..
Select employees whose name does not begin with a EMP. e_ssn e_name e_title
38
39
SELECT Average Salary=, Avg(e_salary) From EMP Where e_title = developer or e_title = analyst ; What does this query do?
40
e_ssn
e_name
e_title
e_salary
41
42
GROUP BY..
EMPLOYEE
e_ssn 456-34-8895 459-66-6785 467-89-8898 478-64-8005 489-12-5575 492-93-4438 467-89-8898 e_name Smith Johnson Weintraub Dickson Ferrel Rao McDonald e_title developer analyst developer manager analyst analyst manager e_salary $35,000 $27,000 $60,000 $64,000 $47,000 $71,000 $85,000
43
e_ssn
e_name
e_title
e_salary
SELECT Number of: , e_title, =, COUNT(e_name) FROM EMP GROUP BY e_title HAVING e_salary > 50000
44
45
DEPARTMENT
d_name Manugistics IMS Pilot InfoSec d_no 142 230 345 467 d_mgr_ssn 467-89-8898 479-99-0045 478-64-8005 898-98-0967 d_phone 845-9878 989-0087 787-9934 884-5768
48
DISCUSSION
Write SQL queries for the following:
1. Create Emp table with E_SSN, E_Name, E_title as attr. -- assume data types. 2. Add E_salary to the Employee table. 3. Create an index, Ti_ndx on E_title. 4. List employees and job titles in order of title. 5. List employees other than developers. 6. Create a view hi_flier listing developers with salary > $100K. 7. Count the #. of employees who are managers.
49