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

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Virtual training week 4 structured query language (SQL)
Pertemuan ke 2 Tipe data & ERD Kurniawan Eka Permana.
Chapter 4 5 6_ SQL SQL Is: Structured Query Language
Day 3 - Basics of MySQL What is MySQL What is MySQL How to make basic tables How to make basic tables Simple MySQL commands. Simple MySQL commands.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 David M. Kroenke Database Processing Chapter 2 Structured Query Language.
Chapter 7: Introduction to SQL
Fundamentals, Design, and Implementation, 9/e Chapter 12 ODBC, OLE DB, ADO, and ASP.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 © Prentice Hall, 2002 Chapter 7: SQL Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
1 IS 4420 Database Fundamentals Chapter 7: Introduction to SQL Leon Chen.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
IMS1907 Database Systems Summer Semester 2004/2005 Lecture 2 Relational DBMS Software An Overview of Microsoft Access.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Copyright © Curt Hill SQL The Data Definition Language.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
Chapter 6: Introduction to SQL
Database Technical Session By: Prof. Adarsh Patel.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
© 2011 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 6: Introduction to SQL Modern Database Management 10 th Edition Jeffrey A. Hoffer,
CSC 2720 Building Web Applications Database and SQL.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
Quick review of SQL And conversion to Oracle SQL.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
CHAPTER 6: INTRODUCTION TO SQL Copyright © 2014 Pearson Education, Inc. 1 Essentials of Database Management Jeffrey A. Hoffer, Heikki Topi, V. Ramesh.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Lec 3- B Database Integrity 1. Overview Define a database using SQL data definition language Work with Views Write single table queries Establish referential.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
© 2005 by Prentice Hall 1 Chapter 7: Introduction to SQL Modern Database Management 7 th Edition Jeffrey A. Hoffer, Mary B. Prescott, Fred R. McFadden.
Chapter 7: Introduction to SQL
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Creating and Maintaining Geographic Databases. Outline Definitions Characteristics of DBMS Types of database Relational model SQL Spatial databases.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Sql DDL queries CS 260 Database Systems.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
SQL Overview Structured Query Language
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
Lecture # 24 Introduction to SQL Muhammad Emran Database Systems.
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Lecture 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A. Hoffer,
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
SQL Query Getting to the data ……..
Managing Tables, Data Integrity, Constraints by Adrienne Watt
DATABASE MANAGEMENT SYSTEM
STRUCTURED QUERY LANGUAGE
SQL OVERVIEW DEFINING A SCHEMA
SQL-1 Week 8-9.
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
IST 318 Database Administration
SQL (Structured Query Language)
Presentation transcript:

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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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)

Monash University 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)

Monash University 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)

Monash University 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)

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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

Monash University 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 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.

Monash University 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