Transcript
Page 1: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

FEN 2014-02-06 1

Data Definition: CREATE TABLE, ALTER TABLE

Data Manipulation: INSERT, UPDATE, DELETE

Queries: SELECT

SQL: Structured Query Language – Part 1

Page 2: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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

Page 3: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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.

Page 4: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

4

Example: MiniBank

Table definitions:

FEN 2014-02-06

Constraint

Page 5: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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).

Page 6: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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.

Page 7: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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).

Page 8: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

8

Company: Exercise

FEN 2014-02-06

Do Exercise 2, phase 1 and 2 onCompanyExercise.pdf

Page 9: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

9

Company: Relations (PK – FK)

FEN 2014-02-06

Page 10: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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

Page 11: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

11

Company: Exercise

FEN 2014-02-06

Do exercise 2, phase 3 of CompanyExercise.pdf

Page 12: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

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

Page 13: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

13

SQL Data Definition Language - Alter Table

DROP SCHEMADROP TABLE

ALTER TABLEADD (column)DROP COLUMN

ALTER TABLEDROP CONSTRAINTADD CONSTRAINT

FEN 2014-02-06

Page 14: FEN 2014-02-061  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language

14

VW: new database

FEN 2014-02-06

Look at this database: ..\lektion03 (SQL2)\vwDatabase.pdf

Create the database. These scripts may be helpful.


Recommended