Download presentation
Presentation is loading. Please wait.
1
SQL Review (con’d) INLS 258 Fall 2003
2
Select Example SELECT teacher.name FROM teacher WHERE teacher.PID in (SELECT teachID FROM teaches WHERE teaches.coursenum = ‘258’);
3
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
4
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’);
5
Update UPDATE table SET col-1 = newval-1, col-2 = newval-2 WHERE col = value; UPDATE teacher SET salary = 100000.00 WHERE PID = ‘123456789’;
6
Delete DELETE FROM table WHERE col = value; DELETE FROM teacher WHERE PID = ‘123456789’;
7
Describe DESCRIBE table; returns list of columns, datatypes, PK defined in table. –(table metadata) Pulls info from data dictionary (aka. system catalog)
8
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
9
Grant Examples GRANT SELECT ON teacher, courses, teaches TO clerks; GRANT CREATE, DROP, SELECT, DELETE, UPDATE ON teacher TO fred WITH GRANT OPTION;
10
Revoke REVOKE privilege-1, privilege-2,...privilege-n ON table FROM user; REVOKE delete, update ON teacher FROM fred;
11
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.
12
Writing a SQL query Define the question to be answered –May need to rewrite Break query into component pieces “Pseudo” SQL Add any aggregation
13
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
14
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;
15
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
16
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)
17
SQL functions Avg(), min(), max(), count(), sum() Current_date() Current_user() Char_length() = len() or length() Lower(), upper()
18
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
19
Coming up… Next time –Modeling and Evaluation –Be thinking about Oracle project ideas and groups and your current issues presentations!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.