Presentation is loading. Please wait.

Presentation is loading. Please wait.

IMS1907 Database Systems Summer Semester 2004/2005 Lecture 9 Structured Query Language – SQL Data Definition Language - DDL.

Similar presentations


Presentation on theme: "IMS1907 Database Systems Summer Semester 2004/2005 Lecture 9 Structured Query Language – SQL Data Definition Language - DDL."— Presentation transcript:

1 IMS1907 Database Systems Summer Semester 2004/2005 Lecture 9 Structured Query Language – SQL Data Definition Language - DDL

2 Monash University 20042 Has become de facto language for creating and querying relational databases –mainframe and personal database environments Accepted as a standard –ANSI, FIPS, ISO First published in 1986 –updated in 1989, 1992 (SQL-92), 1999 (SQL-99) and 2003 (SQL:2003) Structured Query Language - SQL

3 Monash University 20043 SQL-99 was a significant extension on SQL-92 –added regular expression matching, recursive queries, triggers, non-scalar types and some object-oriented features (the last two are somewhat controversial and not yet widely supported) SQL:2003 –introduced XML-related features, standardized sequences and columns with auto-generated values (including identity-columns) Structured Query Language - SQL

4 Monash University 20044 Benefits of a standard relational language –reduced training costs –increased productivity –increased application portability –extended application longevity –reduced dependence on single vendor –enhanced cross-system communication Structured Query Language - SQL

5 Monash University 20045 Disadvantages of a standard relational language –stifle creativity and innovation standardisation – the natural enemy of variety –may not meet all user needs –not ideal as a result of being a set of compromises between many industry parties –difficult to change –vendor features can limit portability and offset other advantages Structured Query Language - SQL

6 Monash University 20046 Concepts –catalog –schema –data definition language (DDL) –data manipulation language (DML) –data control language (DCL) The SQL Environment

7 Monash University 20047 Catalog –a set of schemas which, when put together, constitute a description of a database –describes any object that is part of a database –a DBMS may manage many catalogs Schema –a structure containing descriptions of objects created by users base tables, views, domains, constraints, character sets, triggers, roles, … The SQL Environment

8 Monash University 20048 Data definition language (DDL) –commands used to define a database –used to create, change and remove objects from a database and establish constraints –generally restricted to one or more DBAs to protect database from unexpected changes –DDL statements include CREATE, ALTER, DROP In MySQL we also need the USE statement The SQL Environment

9 Monash University 20049 Data manipulation language (DML) –considered by many as the core SQL commands –commands used to maintain and query a database –used to update, insert, change and query data –can be interactive or embedded in programs embedded SQL gives programmer more control over report timing, interface appearance, error handling and database security –some common DML statements include INSERT, DELETE, SELECT, SHOW, DESCRIBE The SQL Environment

10 Monash University 200410 DML allows us to perform the following relational operations –select selects a subset of rows in a table satisfying a selection criteria –project selects only certain columns from a table –join combines data from two or more tables based on one or more common values The SQL Environment

11 Monash University 200411 Data control language (DCL) –commands used to control a database –used to administer privileges and the committing of data –controls granting and revoking of access, and retrieving and storing transactions that could affect database –generally restricted to DBAs to protect database from unexpected changes and unauthorised access –DCL statements include GRANT, REVOKE, LOAD, BACKUP, ROLLBACK The SQL Environment

12 Monash University 200412 Each DBMS has a defined list of data types it can handle All have equivalents for structured data types –numeric, string and date/time variables Many allow for unstructured data types –graphic, image, spatial When creating a table, the data type for each attribute must be specified Choice of data type depends on –data values that need to be stored –expected uses of data The SQL Environment – Data Types

13 Monash University 200413 MySQL Datatypes MySQLLengthOracleVB6 TINYINT-128 to 127 characters NUMBER integer TINYINT UNSIGNED0 to 255 charactersbyte SMALLINT-32,768 to 32,767 charactersinteger SMALLINT UNSIGNED0 to 65, 535 characters long MEDIUMINT-8,388,608 to 8,388,607 characters MEDIUMINT UNSIGNED0 to 16,777,215 characters INT-2,147,483,647 to 2,147,483,647 characters INT UNSIGNED0 to 4,294,967,295 charactersdouble BIGINT64 BitN/A

14 Monash University 200414 MySQL Datatypes MySQLLengthOracleVB6 FLOAT32 bit floating point NUMBER single DOUBLE64 bit floating point double DECIMALvariable floating point CHAR1 to 255 characters - fixed CHAR string VARCHAR1 to 255 characters - variable VARCHAR2 TINYTEXT1 to 255 characters - variable TEXT1 to 65,535 characters LONG MEDIUMTEXT1 to 16,777,215 characters LONGTEXT1 to 4,294,967,295 characters N/A

15 Monash University 200415 MySQL Datatypes MySQLLengthOracleVB6 all BLOB types1 to 4,294,967,295 bytesLOBVariant DATEDate without time DATE Date and time value DATETIMEDate and time TIMESTAMPDate and time TIMETime YEARYearinteger ENUMEnumeration of value setENUM string SETSet of values

16 Monash University 200416 Three DDL CREATE commands are included in SQL-92 and later versions of standard –CREATE DATABASE (or CREATE SCHEMA in Oracle) used to define portion of database owned by user contains tables, views, domains, constraints, … –CREATE TABLE defines table and its columns dependent on a schema –CREATE VIEW defines logical table from one or more tables or views Data Definition Language (DDL)

17 Monash University 200417 Other DDL CREATE commands are also included in SQL- 92 and later versions of standard –CREATE CHARACTER SET –CREATE COLLATION –CREATE TRANSLATION –CREATE ASSERTION –CREATE DOMAIN Other statements such as CREATE INDEX are performance-related and not part of standard Data Definition Language (DDL)

18 Monash University 200418 The DROP command is used to reverse CREATE commands –DROP DATABASE (or DROP SCHEMA in Oracle) removes entire database and related schemas –DROP TABLE removes table and related columns and constraints from database –DROP VIEW removes logical table from database schema Other DROP commands can remove keys, indexes, users,.. Data Definition Language (DDL)

19 Monash University 200419 The ALTER command is used to change objects created using the CREATE command –ALTER DATABASE (or ALTER SCHEMA in Oracle) changes DB characteristics – character sets, collations –ALTER TABLE, ALTER COLUMN changes table definitions by altering column specifications –ALTER VIEW changes specifications of logical table Can include ALTER, ADD, DROP keywords Data Definition Language (DDL)

20 Monash University 200420 At the mysql> prompt enter the command and the name of the database you want to create –mysql> CREATE DATABASE menagerie; Creating a database in MySQL does not automatically select it for use Select a database for use with the use command and the name of the database you want to use –mysql> USE menagerie; Creating a Database

21 Monash University 200421 Once a database has been selected you can create tables Creating tables requires that you define –column names –data types and lengths –column and table keys and constraints Although MySQL does not necessarily require you to specify keys and constraints, they should be defined if known Creating Tables

22 Monash University 200422 Use the CREATE TABLE statement to define the layout of the table CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); You can specify more detail about your table by defining keys and constraints Creating Tables

23 Monash University 200423 CREATE TABLE person (person_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(60) NOT NULL, PRIMARY KEY (person_id)); CREATE TABLE shirt (shirt_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, style ENUM('t-shirt', 'polo', 'dress') NOT NULL, color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL, owner SMALLINT UNSIGNED NOT NULL REFERENCES person(person_id), PRIMARY KEY (shirt_id)); Creating Tables

24 Monash University 200424 Use the ALTER TABLE statement to re-define the layout of the table –add/remove/rename columns, constraints, keys, types, … ALTER TABLE person ADD (address VARCHAR(60) NOT NULL); ALTER TABLE person MODIFY (name VARCHAR(40) NOT NULL); Altering Tables

25 Monash University 200425 Use the DROP DATABASE command to remove a database and all related tables and schemas Enter the command and the name of the database DROP DATABASE menagerie; You can add the optional keywords IF EXISTS to prevent errors occurring for non-existent databases DROP DATABASE IF EXISTS menagerie; Use this command with caution! Deleting a Database

26 Monash University 200426 Use the DROP TABLE command to remove a table and all its related definitions Enter the command and the name of the table DROP TABLE pet; You can add the optional keywords IF EXISTS to prevent errors occurring for non-existent tables DROP TABLE IF EXISTS pet; You can use this command to remove one or more tables Deleting a Table

27 Monash University 200427 References Bordoloi, B. and Bock, D., (2004), Oracle SQL, Pearson Education Inc., Upper Saddle River, NJ, USA. Hillyer, M., (2004), Visual Basic / MySQL Datatypes, last accessed 10 th September 2004 at http://dev.mysql.com/tech-resources/articles/visual-basic- datatypes.html Hoffer, J.A., Prescott, M.B. and McFadden, F.R., (2005), Modern Database Management, (7 th edn.), Pearson Education Inc., Upper Saddle River, NJ, USA.

28 Monash University 200428 References Kroenke, D.M., (2004), Database Processing: Fundamentals, Design and Implementation, (9 th edn.), Pearson Education Inc., Upper Saddle River, NJ, USA. MySQL Reference Manual, (2004), last accessed 10 th September 2004 at http://dev.mysql.com/doc/mysql/


Download ppt "IMS1907 Database Systems Summer Semester 2004/2005 Lecture 9 Structured Query Language – SQL Data Definition Language - DDL."

Similar presentations


Ads by Google