Post on 02-Jan-2016
transcript
FEN 2014-02-06 1
Data Definition: CREATE TABLE, ALTER TABLE
Data Manipulation: INSERT, UPDATE, DELETE
Queries: SELECT
SQL: Structured Query Language – Part 1
2
SQL
SQL is a realisation of the relational model.SQL is much more than merely queries – it includes:
DDLData Definition Language
DMLData Manipulation Language
DCLData Control Language
FEN 2014-02-06
3
SQL-Versions
SQL has been implemented by many different DBMS-manufactures
SQL is to a large extend the same for most DBMSs – close to a de facto standard
Standards:SQL86 (SQL1), SQL89 (SQL1½), SQL92 (SQL2), SQL3 (SQL9x/SQL2000? - eventually SQL-99)
SQL2 is still the most common standard.
SQL-99 (Huge - released in 2002)
Now SQL:2003 (partly supported by MS SQL Server 2008,revisions SQL:2008, SQL:2011)
Most manufactures have their own extensions (and omissions) to the standard
FEN 2014-02-06
For instance:OracleMySQLMS SQL ServerPostgreSQL
???If you are confused – it’s
for a good reason.But in practice SQL2 is
still most used, the rest is mostly extensions.
4
Example: MiniBank
Table definitions:
FEN 2014-02-06
Constraint
5
Example: MiniBank
Sample queries:
FEN 2014-02-06
Retrieve information about customer number 3:
Note:The result is a table (with only one row, not a tuple).
6
Example: MiniBank
Sample queries:
FEN 2014-02-06
Retrieve account number, balance and customer number for accounts with a balance between 1000 and 2000:
Note:The result is a table.
7
Example: MiniBank
Sample queries:
FEN 2014-02-06
Retrieve information about customer Tommy and his accounts:
Note:The result is a table (with only one row, not a tuple).
8
Company: Exercise
FEN 2014-02-06
Do Exercise 2, phase 1 and 2 onCompanyExercise.pdf
9
Company: Relations (PK – FK)
FEN 2014-02-06
10
Company on SQL Server
Let’s see it work:MS SQL Server
Did you note the order of table creation?Did you note the order of inserting sample data?
FEN 2014-02-06
11
Company: Exercise
FEN 2014-02-06
Do exercise 2, phase 3 of CompanyExercise.pdf
12
Company on SQL Server
Do we miss a foreign key constraint here?
Let’s try to make an error: change mgrssn to a not existing ssn.
Why didn’t we add a constraint when the table was created?
Solution: ALTER TABLE – let’s try.
FEN 2014-02-06
13
SQL Data Definition Language - Alter Table
DROP SCHEMADROP TABLE
ALTER TABLEADD (column)DROP COLUMN
ALTER TABLEDROP CONSTRAINTADD CONSTRAINT
FEN 2014-02-06
14
VW: new database
FEN 2014-02-06
Look at this database: ..\lektion03 (SQL2)\vwDatabase.pdf
Create the database. These scripts may be helpful.