Академический Документы
Профессиональный Документы
Культура Документы
Bahasa merupakan bahasa query deklaratif yang mengindikasikan properti-properti dari data yang hendak diambil. Bahasa yang terstruktur yang digunakan dalam mengelola suatu sistem database: Data Definition Language (DDL): bahasa khusus untuk menspesifikasikan database scheme dengan sekumpulan definisi-definisi tertentu dalam file khusus yang disebut: data dictionary (directory) yang berisi meta data. Metadata adalah data tentang data
Mengambil informasi dari database (retrieval) Menambah informasi ke dalam database (insertion) Mengurangi informasi dari database (deletion)
Overview of SQL
Data Definition (DDL) CREATE TABLE define table CREATE VIEW define user view of data ALTER TABLE DROP TABLE DROP VIEW add new columns or modify existing columns delete table delete user view
5
Overview of SQL
Data Retrieval (DML) SELECT retrieve data from table
INSERT UPDATE
DELETE
add a single row or copy rows from other table(s) amend column values
delete rows of data
Overview of SQL
Data Control COMMIT commit changes to the database ROLLBACK rollback previous changes
Data Security
GRANT REVOKE grant access privileges to users revoke access privileges
Creating Tables CREATE TABLE EMP (EMPNO CHAR(6), NAME CHAR(20), SALARY NUMBER(8,0), AGE NUMBER(3,0), DEPTNO CHAR(2));
A table is defined. Space is reserved. The system catalogue is updated. Table and Column Names begin with alpha (A-Z) less than or equal to 30 characters Table names and Column names contain (A-Z,0-9,$,#, __)
8
If you subsequently add a new row to the ACCOUNTS table and do not specify a value for the BAL column, the value of the BAL column is automatically 0. The Alter command lets you add a column, add an integrity constraint, redefine a column (datatype, size, default value), enable, disable, or drop an integrity constraint or trigger.
9
The description of the above objects will include owner and creation information and information concerning the above objects that is of use to the DBMS.
11
Zachman framework
Physical Design
Impleme ntation
Database implementation
Maintenanc e
Database maintenance
Physical schema
ISA EDM
Enterprise modeling
SELECT * FROM PART; Selects all column values for all rows SELECT PARTNO, PNAME FROM PART WHERE PRICE BETWEEN 0.2 AND 1.0; Selects rows where price ge .2 and le 1.0 SELECT PARTNO, PNAME FROM PART WHERE PNAME IN ('NUT','BOLT'); Selects rows where pname has a value in the following list
14
SELECT [ALL|DISTINCT] expression[AS result_column] {,expression[AS result_column]} FROM tablename[t_alias] {,tablename[t_alias]} [WHERE search_condition] [GROUP BY colname {,colname}] [HAVING search_condition]
MINISTRY
17
Expressions in SELECT
Arithmetic operators are + - ** * / Comparison operators are = != <> ^= > < >= <= Logical operators are AND OR NOT Parentheses may be used to alter order of evaluation unary, **, * /, + Wildcard % = any string of zero or more character _ = any one character [ ] = any of the characters enclosed in brackets A range of numeric, string, date and other functions are available.
18
20
SELECT PNAME FROM PART WHERE QOH IS NULL; Selects those rows where qoh has a null value SELECT * FROM PART WHERE PNAME LIKE '_ _T' or PNAME LIKE '%LT'; Selects rows where pname has three letters the last of which is a T or PNAME ends in LT
22
COUNT(*)
COUNT(DISTINCT PM_NAME)
Liberal PMs
23
UNION
PART PARTNO P1 P2 P3 PNAME NUT BOLT CARAVAN PRICE $0.20 $1.00 $5000.00 QOH 21 40 3
SELECT pname, 'q1' AS Query FROM PART WHERE QOH < 22 UNION pname Query SELECT pname, 'q2' AS Query FROM PART NUT q1 CARAVAN q1 WHERE QOH > 20; NUT q2
BOLT q2
24
DELETE
DELETE FROM tablename [t_alias] [ WHERE searchcondition ] Delete one or many rows in a table.
25
UPDATE
UPDATE tablename [t_alias] SET colname = expression { , colname = expression} [ WHERE search_condition ] Replaces values of the specified columns with expression values for all rows satisfying the search-condition. Expressions in the set clause may be constants, column values or subqueries.
26
SELECT count(partno) AS Part_count, avg(price) AS Av_price, count(distinct price) AS Price_count FROM part
Part_count 3 Av_price $1667.33 Price_count 2
Use of GROUP BY
List the number of prime ministers from each party. SELECT PARTY, COUNT(*) FROM MINISTRY GROUP BY PARTY
PARTY Country Free Trade Labor Liberal National Labor Nationalist Proctectionist United Australia COUNT(*) 3 1 15 17 1 3 4 5
28
29
32
Subqueries
Give the name and death age for each prime minister who died at an age less than the average death age of prime ministers. List in ascending order of death age.
SELECT FROM WHERE PM_NAME, DEATH_AGE PRIME_MINISTER DEATH_AGE < (SELECT AVG(DEATH_AGE) FROM PRIME_MINISTER) ORDER BY DEATH AGE
DEATH_AGE 59 60 60 63
33
Subqueries
Which prime minister died the oldest? Give his name and that age.
SELECT FROM WHERE PM_NAME, DEATH_AGE PRIME_MINISTER DEATH_AGE = (SELECT MAX(DEATH_AGE) FROM PRIME_MINISTER)
PM_NAME Forde F M
DEATH_AGE 93
34
35
SELECT partno, suppno, qty_supp FROM Part_supplier WHERE qty_supp > ANY (SELECT avg(qty_supp) FROM SUPPNO QTY_SUPP S1 30 Part-supplier) S2 25
PARTNO P2 P1
36
Subqueries
SELECT partno, suppno, qty_supp FROM Part_supplier WHERE qty_supp > ANY (SELECT avg(qty_supp) FROM Part-supplier) Subqueries may be used in a number of SQL statements. select, update, insert, delete, create table, create view, create permit, create38
Correlated Subqueries
PART_SUPPLIER PARTNO SUPPNO P1 S1 P2 S1 P1 S2 P2 S2 P3 S2 QTY_SUPP 20 30 25 20 10
SELECT partno, suppno, qty_supp FROM Part_supplier PS1 WHERE qty_supp > ANY
Correlated Subqueries
SELECT partno, suppno, qty_supp FROM Part_supplier PS1 WHERE qty_supp > ANY (SELECT avg(qty_supp) FROM Part-supplier PS2 WHERE PS2.partno = PS1.partno) Subselects (inner queries) are generally
40
Which suppliers are supplying more than the average for a part and how much of that part do they supply?
EMP EMPNO E1 E2 E3
Joining Tables
ENAME RED BLUE BROWN MGRNO E1 E1 E1 DEPTNO D1 D1 D2
SELECT e.empno AS Number, e.ename AS Name, d.dname AS Department FROM emp e, dep d
Number E1 E2 E3 Name RED BLUE BROWN
41
Multiple Joins
Give the name, birth year, party and the year of marriage of prime ministers born in or after 1900. SELECT DISTINCT P.PM_NAME, BIRTH_YR, MAR_YR, PARTY FROM PRIME_MINISTER P,PM_MARRIAGE W,MINISTRY M WHERE P.PM_NAME = W.PM_NAME AND P.PM_NAME = M.PM_NAME AND BIRTH_YR >= 1900
PM_NAME Fraser J M Gorton J G Hawke R J L Holt H E McEwen J McEwen J BIRTH_YR 1930 1911 1929 1908 1900 1900 MAR_YR PARTY 1956 Liberal 1935 Liberal 1956 Labor 1946 Liberal 1921 Country 1968 Country
43
SELECT X.empno AS Number, X.ename AS Name, Y.ename AS Manager FROM emp X, emp Y WHERE X.mgrno = Y.empno
Number E1 E2 E3 Name RED BLUE BROWN Manager RED RED RED
44
47
48
49
50
GROUP BY clause
group function
DISTINCT operator
51
. Views are created and dropped using SQL Data Definition statements
. Views can be created using any valid SELECT statement
52
From page 161 of Database Systems by C.J.Date 5th edition Get Supplier names for suppliers who supply all parts. SELECT SNAME FROM S WHERE NOT EXISTS (SELECT * FROM P
53