Embed presentation
Download as PDF, PPTX









![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 10March 17, 2017Database Creation ... The creation of catalogues is not covered by the SQLstandard and therefore implementation specific Schemas can be created and deleted via the CREATE andDROP statements The default parameter of the DROP SCHEMA statement isRESTRICT only empty schema can be deleted If CASCADE is specified, all objects associated with theschema will be droppedcreateSchema = "CREATE SCHEMA" , name , "AUTHORIZATION" , creator ,[ ddlStatements ];dropSchema = "DROP SCHEMA" , name , [ "RESTRICT" | "CASCADE" ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-10-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 11March 17, 2017Extended Backus-Naur Form (EBNF) Notation to describe computer program-ming languages (context-free grammars) developed by Niklaus WirthNotation Meaning= Definition, Sequence; Termination| Choice[...] Option{...} Repetition(...) Grouping"..." Terminal StringNiklaus Wirth We use the EBNFto describe differentSQL conceptshttp://en.wikipedia.org/wiki/Extended_Backus-Naur_Form](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-11-2048.jpg&f=jpg&w=240)





![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 17March 17, 2017Table DefinitioncreateTable = "CREATE TABLE" , table , "(" ,( columnElement | tableConstraint ) ,{ "," , ( columnElement | tableConstraint ) } , ")";columnElement = column , datatype ,[ "DEFAULT" , ( value | "NULL" ) ] , { columnConstraint };columnConstraint = "NOT NULL" | "UNIQUE" | "PRIMARY KEY" |( "REFERENCES" , table , [ "(" , column , ")" ] ,{ referentialAction } ) |( "CHECK (" , searchCondition , ")" );tableConstraint = ( ( "UNIQUE" | "PRIMARY KEY ) , "(" , column ,{ "," , column } , ")" ) |( "FOREIGN KEY (" , column , { "," , column } , ")" ,"REFERENCES" , table , [ "(" , column , { "," , column } , ")" ] ,{ referentialAction } ) |( "CHECK (" , searchCondition , ")" );](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-17-2048.jpg&f=jpg&w=240)

![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 19March 17, 2017SQL Datatypes Character data fixed-length or variable-length sequence of characters optional multibyte character sets (e.g. for Japanese etc.) Large character data or binary data often a so-called locator is returned to access a large object inpieces instead of loading the entire object into memorychar = fixedChar | varyingChar [charSet];fixedChar = "CHAR" , [ "(" , length , ")" ];varyingChar = "VARCHAR" , [ "(" , maxLength , ")" ];charSet = "CHARACTER SET" charSetName;lob = clob | blob;clob = "CLOB" , [ "(" , size , ")" ];blob = "BLOB" , [ "(" , size , ")" ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-19-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 20March 17, 2017SQL Datatypes ... Numeric data The DECIMAL datatype is sometimes used as a synonymfor the NUMERIC datatypenumeric = decimal | int | smallInt | float | real | double;decimal = "DECIMAL" , [ "(" , precision , [ "," , scale ] , ")" ];int = "INTEGER";smallInt = "SMALLINT";float = "FLOAT" , [ "(" , precision , ")" ];real = "REAL";double = "DOUBLE PRECISION";](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-20-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 21March 17, 2017SQL Datatypes ... Datetime data Format of the datetime values date: YYYY-MM-DD time: hh:mm:ss.p ± hh:mm timestamp: YYYY-MM-DD hh:mm:ss.p ± hh:mmdatetime = date | time | timestamp;date = "DATE";time = "TIME" , [ "(" , precision , ")" ] ,[ "WITH TIME ZONE" , timezone ];timestamp = "TIMESTAMP" , [ "(" , precision , ")" ] ,[ "WITH TIME ZONE" , timezone ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-21-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 22March 17, 2017SQL Datatypes ... Boolean the domain of boolean values consist of the two truth values TRUEand FALSE a thrid UNKNOWN truth value is used to represent NULL values introduced in SQL:1999 Bit data fixed or varying sequence of binary digits (0 or 1)boolean = "BOOLEAN";bit = fixedBit | varyingBit;fixedBit = "BIT" , [ "(" , length , ")" ];varyingBit = "BIT VARYING" , [ "(" , maxLength , ")" ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-22-2048.jpg&f=jpg&w=240)

![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 24March 17, 2017Data Manipulation After a table has been created, we can use the INSERTcommand to add tuples unspecified attribute values are set to the default value or NULL attribute order can be changed via optional column names "bulk loader" utilities to insert large amounts of tuples ExampleINSERT INTO Customer VALUES(8,'Max Frisch','ETH Zentrum', 8001, 'Zurich');insert = "INSERT INTO" , table ,[ "(" , column , { "," , column } , ")" ] ,( "VALUES (" , expr , { "," , expr } , ")" ) | ( "(" , query , ")" );](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-24-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 25March 17, 2017Expressionsexpr = exprElement { ( "+" | "-" | "*" | "/" ) , exprElement };exprElement = column | value |"COUNT" , "(" ( "*" | ( [ "ALL" | "DISTINCT" ] , column ) , ")" |( "MIN" | "MAX" ) , "(" , expr , ")" |( "SUM" | "AVG" ) , "(" , [ "DISTINCT" ] , expr , ")";](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-25-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 26March 17, 2017Data Manipulation ... The DELETE statement can be used to delete tuples Tuples can be updated via the UPDATE statement ExampleUPDATE Customer SET name = 'Walter Faber' WHERE customerID = 8;update = "UPDATE" , table , "SET" ,column , "=" , ( "NULL" | expr | "(" , query , ")" ) ,{ "," , column , "=" , ("NULL" | expr | "(" , query , ")" ) } ,[ "WHERE" , searchCondition ];delete = "DELETE FROM" , table [ "WHERE" , searchCondition ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-26-2048.jpg&f=jpg&w=240)

























![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 52March 17, 2017Basic SQL Query Structure The query statement can be used to retrieve informationfrom one or multiple database tables can perform the relational algebra's selection, projection and joinoperation in a single SELECT FROM WHERE commandquery = select { ("UNION" | "INTERSECT" | "EXCEPT") , [ "ALL" ] , select};select = "SELECT" [ "ALL" | "DISTINCT" ] ,("*" | ( expr , [ "AS" , newName ] ,{ "," , expr , [ "AS" , newName ] } ) ,"FROM" , table , [ correlationVar ] ,{ "," , table , [ correlationVar ] } ,[ "WHERE" , searchCondition ] ,[ "GROUP BY" , column , { "," , column } ,[ "HAVING" , searchCondition ] ];orderedQuery = query , "ORDER BY" , column , [ "ASC" | "DESC" ] ,{ "," , column , [ "ASC" | "DESC" ] };](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-52-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 53March 17, 2017Basic SQL Query Structure ...searchCondition = [ "NOT" ] , search ,{ ( "AND" | "OR" ) , [ "NOT" ] , search };search = ( expr , [ "NOT" ] , "BETWEEN" , expr , "AND" , expr ) |( expr , [ "NOT" ] , "LIKE" , "'" , ( string | "_" | "%" ) ,{ string | "_" | "%" } , "'" ) |( column | ( "(" , expr , ")" ) , "IS" , [ "NOT" ] , "NULL" ) |( expr , ( "=" | "<>" | ">" | ">=" | "<" | "<=" ) , ( expr |( [ "SOME" | "ALL" ] , "(" , query , ")" ) ) ) |( expr , [ "NOT" ] , "IN (" ,( ( value , { "," , value } ) | query ) , ")" |( "EXISTS (" , query , ")";](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-53-2048.jpg&f=jpg&w=240)

![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 55March 17, 2017Views New virtual relations (views) can be defined on top of anexisting logical model simplify queries provide access to only parts of the logical model (security) computed by executing the query whenever the view is used Some DBMS allow views to be stored (materialisedviews) materialised views have to be updated when its relations change(view maintenance)createView = "CREATE VIEW" , table ,[ "(" , column , { "," , column } , ")" ] ,"AS" , query;](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-55-2048.jpg&f=jpg&w=240)







The document discusses Structured Query Language (SQL) and its history and components. It notes that SQL is a declarative query language used to define database schemas, manipulate data through queries, and control transactions. The document outlines SQL's data definition language for defining schemas and data manipulation language for querying and modifying data. It also provides examples of SQL statements for creating tables and defining constraints.









![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 10March 17, 2017Database Creation ... The creation of catalogues is not covered by the SQLstandard and therefore implementation specific Schemas can be created and deleted via the CREATE andDROP statements The default parameter of the DROP SCHEMA statement isRESTRICT only empty schema can be deleted If CASCADE is specified, all objects associated with theschema will be droppedcreateSchema = "CREATE SCHEMA" , name , "AUTHORIZATION" , creator ,[ ddlStatements ];dropSchema = "DROP SCHEMA" , name , [ "RESTRICT" | "CASCADE" ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-10-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 11March 17, 2017Extended Backus-Naur Form (EBNF) Notation to describe computer program-ming languages (context-free grammars) developed by Niklaus WirthNotation Meaning= Definition, Sequence; Termination| Choice[...] Option{...} Repetition(...) Grouping"..." Terminal StringNiklaus Wirth We use the EBNFto describe differentSQL conceptshttp://en.wikipedia.org/wiki/Extended_Backus-Naur_Form](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-11-2048.jpg&f=jpg&w=240)





![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 17March 17, 2017Table DefinitioncreateTable = "CREATE TABLE" , table , "(" ,( columnElement | tableConstraint ) ,{ "," , ( columnElement | tableConstraint ) } , ")";columnElement = column , datatype ,[ "DEFAULT" , ( value | "NULL" ) ] , { columnConstraint };columnConstraint = "NOT NULL" | "UNIQUE" | "PRIMARY KEY" |( "REFERENCES" , table , [ "(" , column , ")" ] ,{ referentialAction } ) |( "CHECK (" , searchCondition , ")" );tableConstraint = ( ( "UNIQUE" | "PRIMARY KEY ) , "(" , column ,{ "," , column } , ")" ) |( "FOREIGN KEY (" , column , { "," , column } , ")" ,"REFERENCES" , table , [ "(" , column , { "," , column } , ")" ] ,{ referentialAction } ) |( "CHECK (" , searchCondition , ")" );](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-17-2048.jpg&f=jpg&w=240)

![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 19March 17, 2017SQL Datatypes Character data fixed-length or variable-length sequence of characters optional multibyte character sets (e.g. for Japanese etc.) Large character data or binary data often a so-called locator is returned to access a large object inpieces instead of loading the entire object into memorychar = fixedChar | varyingChar [charSet];fixedChar = "CHAR" , [ "(" , length , ")" ];varyingChar = "VARCHAR" , [ "(" , maxLength , ")" ];charSet = "CHARACTER SET" charSetName;lob = clob | blob;clob = "CLOB" , [ "(" , size , ")" ];blob = "BLOB" , [ "(" , size , ")" ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-19-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 20March 17, 2017SQL Datatypes ... Numeric data The DECIMAL datatype is sometimes used as a synonymfor the NUMERIC datatypenumeric = decimal | int | smallInt | float | real | double;decimal = "DECIMAL" , [ "(" , precision , [ "," , scale ] , ")" ];int = "INTEGER";smallInt = "SMALLINT";float = "FLOAT" , [ "(" , precision , ")" ];real = "REAL";double = "DOUBLE PRECISION";](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-20-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 21March 17, 2017SQL Datatypes ... Datetime data Format of the datetime values date: YYYY-MM-DD time: hh:mm:ss.p ± hh:mm timestamp: YYYY-MM-DD hh:mm:ss.p ± hh:mmdatetime = date | time | timestamp;date = "DATE";time = "TIME" , [ "(" , precision , ")" ] ,[ "WITH TIME ZONE" , timezone ];timestamp = "TIMESTAMP" , [ "(" , precision , ")" ] ,[ "WITH TIME ZONE" , timezone ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-21-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 22March 17, 2017SQL Datatypes ... Boolean the domain of boolean values consist of the two truth values TRUEand FALSE a thrid UNKNOWN truth value is used to represent NULL values introduced in SQL:1999 Bit data fixed or varying sequence of binary digits (0 or 1)boolean = "BOOLEAN";bit = fixedBit | varyingBit;fixedBit = "BIT" , [ "(" , length , ")" ];varyingBit = "BIT VARYING" , [ "(" , maxLength , ")" ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-22-2048.jpg&f=jpg&w=240)

![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 24March 17, 2017Data Manipulation After a table has been created, we can use the INSERTcommand to add tuples unspecified attribute values are set to the default value or NULL attribute order can be changed via optional column names "bulk loader" utilities to insert large amounts of tuples ExampleINSERT INTO Customer VALUES(8,'Max Frisch','ETH Zentrum', 8001, 'Zurich');insert = "INSERT INTO" , table ,[ "(" , column , { "," , column } , ")" ] ,( "VALUES (" , expr , { "," , expr } , ")" ) | ( "(" , query , ")" );](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-24-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 25March 17, 2017Expressionsexpr = exprElement { ( "+" | "-" | "*" | "/" ) , exprElement };exprElement = column | value |"COUNT" , "(" ( "*" | ( [ "ALL" | "DISTINCT" ] , column ) , ")" |( "MIN" | "MAX" ) , "(" , expr , ")" |( "SUM" | "AVG" ) , "(" , [ "DISTINCT" ] , expr , ")";](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-25-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 26March 17, 2017Data Manipulation ... The DELETE statement can be used to delete tuples Tuples can be updated via the UPDATE statement ExampleUPDATE Customer SET name = 'Walter Faber' WHERE customerID = 8;update = "UPDATE" , table , "SET" ,column , "=" , ( "NULL" | expr | "(" , query , ")" ) ,{ "," , column , "=" , ("NULL" | expr | "(" , query , ")" ) } ,[ "WHERE" , searchCondition ];delete = "DELETE FROM" , table [ "WHERE" , searchCondition ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-26-2048.jpg&f=jpg&w=240)

























![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 52March 17, 2017Basic SQL Query Structure The query statement can be used to retrieve informationfrom one or multiple database tables can perform the relational algebra's selection, projection and joinoperation in a single SELECT FROM WHERE commandquery = select { ("UNION" | "INTERSECT" | "EXCEPT") , [ "ALL" ] , select};select = "SELECT" [ "ALL" | "DISTINCT" ] ,("*" | ( expr , [ "AS" , newName ] ,{ "," , expr , [ "AS" , newName ] } ) ,"FROM" , table , [ correlationVar ] ,{ "," , table , [ correlationVar ] } ,[ "WHERE" , searchCondition ] ,[ "GROUP BY" , column , { "," , column } ,[ "HAVING" , searchCondition ] ];orderedQuery = query , "ORDER BY" , column , [ "ASC" | "DESC" ] ,{ "," , column , [ "ASC" | "DESC" ] };](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-52-2048.jpg&f=jpg&w=240)
![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 53March 17, 2017Basic SQL Query Structure ...searchCondition = [ "NOT" ] , search ,{ ( "AND" | "OR" ) , [ "NOT" ] , search };search = ( expr , [ "NOT" ] , "BETWEEN" , expr , "AND" , expr ) |( expr , [ "NOT" ] , "LIKE" , "'" , ( string | "_" | "%" ) ,{ string | "_" | "%" } , "'" ) |( column | ( "(" , expr , ")" ) , "IS" , [ "NOT" ] , "NULL" ) |( expr , ( "=" | "<>" | ">" | ">=" | "<" | "<=" ) , ( expr |( [ "SOME" | "ALL" ] , "(" , query , ")" ) ) ) |( expr , [ "NOT" ] , "IN (" ,( ( value , { "," , value } ) | query ) , ")" |( "EXISTS (" , query , ")";](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-53-2048.jpg&f=jpg&w=240)

![Beat Signer - Department of Computer Science - bsigner@vub.ac.be 55March 17, 2017Views New virtual relations (views) can be defined on top of anexisting logical model simplify queries provide access to only parts of the logical model (security) computed by executing the query whenever the view is used Some DBMS allow views to be stored (materialisedviews) materialised views have to be updated when its relations change(view maintenance)createView = "CREATE VIEW" , table ,[ "(" , column , { "," , column } , ")" ] ,"AS" , query;](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2flecture05sql-110406195130-phpapp02%2f75%2fStructured-Query-Language-SQL-Lecture-5-Introduction-to-Databases-1007156ANR-55-2048.jpg&f=jpg&w=240)





