Transcript Document

SQL Review (con’d)
INLS 258
Fall 2003
Select Example
SELECT teacher.name
FROM teacher
WHERE teacher.PID in
(SELECT teachID
FROM teaches
WHERE teaches.coursenum = ‘258’);
Select Example
SELECT dept, avg(salary), count(PID)
FROM teacher
WHERE bdate < ‘1950-01-01’
GROUP BY dept
HAVING avg(salary) > 18000
ORDER BY dept DESC;
SQL
Insert Examples
INSERT INTO teacher
VALUES (‘123456789’, ‘Mary Brown’,
91253.83, ‘1949-03-21’, ‘English’);
INSERT INTO teacher (PID, name, dept)
VALUES (‘123456789’, ‘Mary Brown’,
‘English’);
Update
UPDATE table
SET col-1 = newval-1, col-2 = newval-2
WHERE col = value;
UPDATE teacher
SET salary = 100000.00
WHERE PID = ‘123456789’;
Delete
DELETE FROM table
WHERE col = value;
DELETE FROM teacher
WHERE PID = ‘123456789’;
Describe
• DESCRIBE table;
• returns list of columns, datatypes, PK
defined in table.
– (table metadata)
• Pulls info from data dictionary (aka.
system catalog)
Grant
GRANT privilege-1, privilege-2,.. privilege-
n
ON table-1, table-2,...table-n
TO user;
• privileges: create, alter, drop, select,
insert, delete, update
Grant Examples
GRANT SELECT
ON teacher, courses, teaches TO clerks;
GRANT CREATE, DROP, SELECT, DELETE,
UPDATE ON teacher
TO fred WITH GRANT OPTION;
Revoke
REVOKE privilege-1, privilege-2,...privilege-n
ON table
FROM user;
REVOKE delete, update
ON teacher
FROM fred;
Remember
• SQL dialects and version vary in what
they have implemented; some are
“minimal”, some are “augmented”. For
example, MySQL doesn’t support
foreign keys; Oracle 8i has the ability to
drop a column.
• Be careful with alter, update, delete.
• Be careful granting privileges.
Writing a SQL query
• Define the question to be answered
– May need to rewrite
• Break query into component pieces
• “Pseudo” SQL
• Add any aggregation
Nulls
• Types of nulls
– Exists, but not given or not known
– Does not exist
– Does not apply
• incomplete, incorrect, undefined
• Null values are ignored in calculations of
aggregates, but not in GROUP BY
More nulls…
Why do these two statement sets get different
results?
• select * from table where ColA = 2;
• select * from table where ColA <> 2;
• select * from table where ColA is null;
• select * from table where ColA is not null;
Date and Time
• Datatypes
– Interval
– Timestamp with time zone
– date, time
• Dates and times are treated differently
than numbers
• yyyy-mm-dd hh:mm:ss
• Arithmetic functions are available
More (ANSI) SQL99
• CASE statement
UPDATE employees
SET salary = CASE
WHEN dept =‘Video’ THEN salary * 1.1
WHEN dept = ‘Music’ THEN salary*1.2
ELSE 0
END;
• cast function
– allows casting of datatypes
– may need to use CAST in a union join
– CAST (null as datatype)
SQL functions
•
•
•
•
•
Avg(), min(), max(), count(), sum()
Current_date()
Current_user()
Char_length() = len() or length()
Lower(), upper()
Database vs. database server
• A relational database is made up of files
that contain information that is related
in some way
• A database server has processes that
manage the database and its
connections
Coming up…
• Next time
– Modeling and Evaluation
– Be thinking about Oracle project ideas and
groups and your current issues
presentations!