Logical Schema Design: Schema Definition with SQL (DDL) Standard
Total Page:16
File Type:pdf, Size:1020Kb
Standard Query Language: Current standard Logical Schema Design: 1999: SQL:1999 standard (ANSI SQL-3) Schema Definition with SQL (DDL) about 2200 pages as full standard Various parts (not finished): Framework (SQL/Framework), introduction Foundation (SQL/Foundation), core SQL Call-Level Interface (SQL/CLI), ODBC 3 Persistent Stored Modules (SQL/PSM), stored procedures SQL history and standards Host Language Bindings (SQL/ Bindings), embedded SQL SQL type system Temporal data (SQL/Temporal) FU-Berlin, I DBS 2006, Hinze / Scholz Specifying constraints with SQL Management of external data (SQL/MED) Object Language Bindings (SQL/OLB), embedded SQL- Java Multimedia (SQL/MM), full-text and spatial data SQL and XML 4 Standard Query Language: Introduction Standard Query Language: Standards SQL-92 compliance levels: No pure relations in DBMS but tables (1) Entry SQL: basically SQL-89, essential Duplicate data not deleted (2) Intermediate SQL, Tuples – rows, Attributes – columns (3) Full SQL No implementation of SQL-92 on level 2 or3 Standard Query Language (SQL) Core Declarative language for DB SQL:1999 levels: SQL:1999 Available in all relational DBMS Core SQL: essential for standard compliance enhanced Additional Features, e.g. multimedia SQL:1999 FU-Berlin, I DBS 2006, Hinze / Scholz Support of two interfaces: FU-Berlin, I DBS 2006, Hinze / Scholz Interactive Interface: User friendly Interface (UFI) New standards replace old ones Application-program Interface: “embedded SQL” In DBMS implementations much added / left out 2 5 Standard Query Language: History Standard Query Language: Course Information 1974 Prototype “System R” (IBM, San Jose) Within the course: First relational DBMS Basic concepts of SQL:1999 based on Codd’s relational model Oracle10i (commercial) Structured English Query Language (SEQUEL) Core SQL:1999 compliant + additional features 1975 SEQUEL renamed SQL (pronounced “Sequel” in US) PostgreSQL (open source) Core SQL:1999 compliant 1986 First standardization attempt based on system R MySQL (open source) traditionally not SQL compliant 1989 SQL standard (no subqueries, foreign keys,…) ANSI SQL-1 , SQL-89 FU-Berlin, I DBS 2006, Hinze / Scholz about 120 pages FU-Berlin, I DBS 2006, Hinze / Scholz 1992 SQL2 standard Self study of further SQL concepts ANSI SQL-2, SQL-92 about 600 pages 3 6 1 Standard Query Language: Components SQL / DDL: Namespaces Data definition Language (DDL) Confusing terminology implemented Definition and change of data structures on all three database levels: Namespaces, relations with attributes, domains, data types, integrity constraints, triggers, Oracle functions on the database,views, placement of data, space Database = set of physical storage areas ("tablespaces") needed, access structures,… Schema name = dbUsername Object names prefixed with <dbUsername> Data manipulation language (DML) Create, change, delete data PostgreSQL Interactive query formulation Database = schema FU-Berlin, I DBS 2006, Hinze / Scholz Embedding of SQL commands in host language FU-Berlin, I DBS 2006, Hinze / Scholz Schema name = database name Specification of begin, abort, and end of transaction MySQL Data Administration language Database = directory in File system where data reside Access rights, authorization Schema not defined in MySQL 7 10 SQL / DDL: SQL Objects SQL / DDL: Predefined data types Examples: Catalog, schema, table, trigger,… Descriptor = Object identificator (e.g., name) Basic data types: Object hierarchy: Numbers catalog Characters, strings Date and time schema Binary objects table column Type systems of different DBS very different FU-Berlin, I DBS 2006, Hinze / Scholz FU-Berlin, I DBS 2006, Hinze / Scholz Use standard compatible types if possible Catalog: Named group of schemas Created implicitly 8 11 SQL / DDL: Schema SQL / DDL: Predefined data types Core Numeric data types Named group of SQL-objects by particular user SQL:1999 NUMERIC(p,s) e.g. 300.00 DECIMAL(p,s) Creates namespace INTEGER (alias: INT) e.g. 32767 Unambiguous object names SMALLINT small integers FLOAT(p,s) e.g. -1E+03 <catalog>.<schema>.<table>.<column> REAL (for short floats) <catalog>.<schema>.<trigger> DOUBLE (for long floats) Not supported by all systems Examples: Always supported: <table>.<column> Oracle: NUMBER(precision, scale) PostgreSQL: SMALLINT, INTEGER, BIGINT, REAL, FU-Berlin, I DBS 2006, Hinze / Scholz FU-Berlin, I DBS 2006, Hinze / Scholz NUMERIC(precision,scale), DECIMAL(precision, Syntax: CREATE SCHEMA <schemaName>; scale), MONEY, SERIAL (=autoincrement!) MySQL: TINYINT[(M)], SMALLINT[(M)], MEDIUMINT[(M)], INT[(M)], BIGINT[(M)], FLOAT(precision), FLOAT[(M,D)], DOUBLE[(M,D)], DOUBLE PRECISION[(M,D)], REAL[(M,D)], DECIMAL[(M[,D])], NUMERIC[(M[,D])] 9 12 2 SQL / DDL: Predefined data types SQL / DDL: Domain definition, Type definition Core Core Some string data types SQL:1999 User-created domains SQL:1999 CHARACTER(n) (fixed length) Named sets of values CHARACTER (variable lenght) Helps avoiding semantically meaningless operations, e.g., comparing money with length attributes CHARACTER VARYING(n) (alias: VARCHAR(n)) Syntax: CLOB (Character Large Object, e.g., for large text), CREATE DOMAIN <domainName> [AS] <typeDef>; NCLOB (National CLOB) CREATE TYPE <typeName> as <typeDef> FINAL; Example: CREATE DOMAIN Money AS DECIMAL(10,2); Examples: CREATE TYPE Euro AS DECIMAL(8,2) FINAL; Oracle: VARCHAR2(size), CHAR(size), CLOB, RAW, FU-Berlin, I DBS 2006, Hinze / Scholz FU-Berlin, I DBS 2006, Hinze / Scholz LONG RAW Oracle: PostgreSQL: CHARACTER(size), CHAR(size), hDomain - not supp., Type – implemented differently VARYING(size), VARCHAR(size), TEXT PostgreSQL: MySQL: CHAR(M), VARCHAR(M), TINYTEXT, TEXT, hDomain – supp.,Type – implemented differently MEDIUMTEXT, LONGTEXT MySQL: 13 hDomain - not supp., Type – not supp. 16 SQL / DDL: Predefined data types SQL / DDL: Table definition Core Date data types SQL:1999 Syntax: DATE e.g. DATE '1993-01-02' CREATE TABLE <TableName> ( TIME e.g. TIME '13:14:15' <attributName><attributeType>[<constraint>] TIMESTAMP e.g. TIMESTAMP '1993-01-02 [, <attributName><attributeType>[<constraint>]] 13:14:15.000001' {, <tableConstraints>}); INTERVAL FirstUnitofTime [to LastUnitofTime] e.g. INTERVAL '01-01' YEAR TO MONTH Example: Examples: Oracle: DATE, INTERVAL DAY TO SECOND, INTERVAL CREATE TABLE Troll( YEAR TO MONTH, TIMESTAMP, TIMESTAMP WITH TIME Name CHAR(10) primary key, FU-Berlin, I DBS 2006, Hinze / Scholz ZONE, TIMESTAMP WITH LOCAL TIME ZONE FU-Berlin, I DBS 2006, Hinze / Scholz Height DECIMAL (3,2)); PostgreSQL: DATE, TIME, TIME WITH TIMEZONE, TIMESTAMP, INTERVAL MySQL: DATE, DATETIME, TIMESTAMP[(M)], TIME, YEAR[(2|4)] SQL is case-insensitive for restricted words 14 17 SQL / DDL: Predefined data types SQL / DDL: Integrity constraints Core Important technique Binary data types SQL:1999 Syntax: BIT[(n)] e.g. B'01000100' [CONSTRAINT [<name>]]<def> BLOB[(n)] e.g. X'49FE' (Binary Large Objects, e.g., for multimedia) Column constraints Example: “must not be NULL”, “larger than 1.50” Examples: Specified as part of column definition Oracle: BLOB, BFILE, RAW, LONG RAW, ROWID PostgreSQL: TEXT, BYTEA, or in large object Cardinalities Column constraints on keys and foreign keys MySQL: TINYBLOB, BLOB, MEDIUMBLOB, LONGBLOB Complex “semantic” constraints ("business rules") FU-Berlin, I DBS 2006, Hinze / Scholz FU-Berlin, I DBS 2006, Hinze / Scholz Example: "The percentage of movies not older than one year must be 25% or more“ Additionally: More than one row involved, specify after column definitions BOOLEAN (true, false or unknown) (table constraint) 15 18 3 SQL / DDL: Integrity constraints SQL / DDL: Integrity constraints PRIMARY KEY Only once per table Column constraint: Not necessary, but omission is very bad style CREATE TABLE BankAccount( Column constraint (single attribute) or table constraint accountno NUMBER(10) primary key, Examples: amount DECIMAL(9,2) CHECK (amount > 0), CREATE TABLE Troll( credit DECIMAL(7,2)); Name CHAR(10) primary key, Height DECIMAL (3,2)); Multicolumn constraint: CREATE TABLE BankAccount( CREATE TABLE Troll( accountno NUMBER(10) primary key, FU-Berlin, I DBS 2006, Hinze / Scholz Name CHAR(10) primary key, FU-Berlin, I DBS 2006, Hinze / Scholz amount DECIMAL(9,2), Height DECIMAL (3,2), credit DECIMAL(7,2), Weight INTEGER, CONSTRAINT account CHECK (amount+credit>0)); CONSTRAINT pk primary key(Name, Height)); 19 22 SQL / DDL: Integrity constraints SQL / DDL: Referential Integrity NOT NULL Foreign Key Important concept Value must not be NULL Consider relation R with key k and relation S Column constraint fk ⊂ S is foreign key if for all tuples s∈S holds: Example: 1. s.fk contains only NULL values or only values ≠ NULL CREATE TABLE Format ( 2. If s.fk contains no NULL values ∃ tuple r∈R: s.fk=r.k name CHAR(10) primary key, charge DECIMAL(3,2) not NULL); Referential integrity: Foreign key constraint (above) holds UNIQUE Column contains only unique values FU-Berlin, I DBS 2006, Hinze / Scholz FU-Berlin, I DBS 2006, Hinze / Scholz Referential Integrity in SQL Requires NOT NULL Candidate keys with UNIQUE Should be used for candidate keys Primary keys with PRIMARY KEY Column constraint, (table constraint) Foreign keys with REFERENCES 20 23 SQL / DDL: Integrity constraints SQL / DDL: Integrity constraints CHECK clause: FOREIGN KEY Important technique Defines predicates that must hold for each row References keys in other tables Ensures references to existing key