of 14/14
FEN 2014-02-06 1 Data Definition: CREATE TABLE, ALTER TABLE Data Manipulation: INSERT, UPDATE, DELETE Queries: SELECT SQL: Structured Query Language – Part 1

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

  • View
    216

  • Download
    2

Embed Size (px)

Text of FEN 2014-02-061 Data Definition: CREATE TABLE, ALTER TABLE Data Manipulation: INSERT, UPDATE,...

PowerPoint Presentation

FEN 2014-02-061Data Definition: CREATE TABLE, ALTER TABLEData Manipulation: INSERT, UPDATE, DELETEQueries: SELECTSQL: Structured Query Language Part 1

SQLSQL is a realisation of the relational model.SQL is much more than merely queries it includes:DDLData Definition LanguageDMLData Manipulation LanguageDCLData Control LanguageFEN 2014-02-062SQL-VersionsSQL has been implemented by many different DBMS-manufacturesSQL is to a large extend the same for most DBMSs close to a de facto standardStandards: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 standardFEN 2014-02-063For instance:OracleMySQLMS SQL ServerPostgreSQL???If you are confused its for a good reason.But in practice SQL2 is still most used, the rest is mostly extensions.Example: MiniBankTable definitions:FEN 2014-02-064

ConstraintExample: MiniBankSample queries:FEN 2014-02-065

Retrieve information about customer number 3:

Note:The result is a table (with only one row, not a tuple).Example: MiniBankSample queries:FEN 2014-02-066

Retrieve account number, balance and customer number for accounts with a balance between 1000 and 2000:

Note:The result is a table.Example: MiniBankSample queries:FEN 2014-02-067

Retrieve information about customer Tommy and his accounts:

Note:The result is a table (with only one row, not a tuple).8Company: ExerciseFEN 2014-02-06Do Exercise 2, phase 1 and 2 onCompanyExercise.pdf

9Company: Relations (PK FK)FEN 2014-02-06

Company on SQL ServerLets 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-061011Company: ExerciseFEN 2014-02-06Do exercise 2, phase 3 of CompanyExercise.pdf

Company on SQL ServerDo we miss a foreign key constraint here?

Lets try to make an error: change mgrssn to a not existing ssn.

Why didnt we add a constraint when the table was created?

Solution: ALTER TABLE lets try.FEN 2014-02-0612

SQL Data Definition Language - Alter TableDROP SCHEMADROP TABLE

ALTER TABLEADD (column)DROP COLUMN

ALTER TABLEDROP CONSTRAINTADD CONSTRAINTFEN 2014-02-0613

14VW: new databaseFEN 2014-02-06Look at this database: ..\lektion03 (SQL2)\vwDatabase.pdf

Create the database. These scripts may be helpful.