Rob Gleasure robgleasure.com

Slides:



Advertisements
Similar presentations
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
Advertisements

SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Using SQL to create tables Ways of using Databases.
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
INTEGRITY Enforcing integrity in Oracle. Oracle Tables mrobbert owner granted access.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
Oracle Data Definition Language (DDL)
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Database Programming Sections 11 & 12 –Sequences, Indexes, and Synonymns.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
Populating and Querying tables Insert, Update, Delete and View (DML)
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
SQL Basics Review Reviewing what we’ve learned so far…….
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
From: SQL From:
Fundamental of Database Systems
Rob Gleasure robgleasure.com
Rob Gleasure robgleasure.com
CS320 Web and Internet Programming SQL and MySQL
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL Creating and Managing Tables
Structured Query Language (SQL) William Klingelsmith
SQL Creating and Managing Tables
STRUCTURED QUERY LANGUAGE
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Creating and Managing Tables
Chapter 4 Indexes.
CH 4 Indexes.
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
Introduction To Structured Query Language (SQL)
Rob Gleasure robgleasure.com
CH 4 Indexes.
Oracle Data Definition Language (DDL)
CS122 Using Relational Databases and SQL
Rob Gleasure robgleasure.com
CS3220 Web and Internet Programming SQL and MySQL
Data Definition Language
Data Definition Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Rob Gleasure robgleasure.com
IST 318 Database Administration
Indexes and more Table Creation
CS3220 Web and Internet Programming SQL and MySQL
Instructor: Samia arshad
CS122 Using Relational Databases and SQL
Including Constraints
Presentation transcript:

Rob Gleasure R.Gleasure@ucc.ie robgleasure.com IS6126 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure R.Gleasure@ucc.ie robgleasure.com

IS6126 Today’s session The DDL Create Database Alter Database Create Table Alter Table Create Index Drop Index, Table, or Database Exercise

The Data Definition Language (DDL) So far we have worked the Data Manipulation Language (DML) component of SQL The DML lets us access and modify the data stored in specific table, which is typically all we will want to do from the perspective of application developers, sophisticated users, and naïve users From a Database Administrator perspective, we will also need to know how to use the Data Definition Language (DDL), which lets us create tables with new schemas or modify the schemas of existing tables

Create Table The most basic part of the DDL is the CREATE TABLE query This query lets us add a new table in a database The basic structure of a CREATE TABLE query is as follows CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), .... );

An Aside on Data Types Note that the data types we specify vary according to the DBMS that we are using You can find a list of data types for oracle at http://docs.oracle.com/cd/B28359_01/server.111/b28318/datatype.htm You can find a list of data types for MS Access, MySQL and MS SQL Server at http://www.w3schools.com/sql/sql_datatypes.asp

Create Table For example, say we want to create a new student for Exchange_Students, that contains a Student_ID, a Name, and a Referring_University, we might have the following CREATE TABLE Exchange_Students ( Student_ID varchar(10), Name varchar (50), Referring_University varchar(255) ) ; Example http://www.w3schools.com/sql/trysql.asp?filename=trysql_create_table

Create Table and Constraints As we discussed last term, often there are constraints for certain fields that must be captured in the database (failure to comply with constraints will cause actions to be rejected) We can nest these constraints in our CREATE TABLE query as follows CREATE TABLE table_name ( column_name1 data_type(size) constraint_name, column_name2 data_type(size) constraint_name, column_name3 data_type(size) constraint_name, .... );

Constraints: Not Null The basic constraints in SQL are NOT NULL E.g. CREATE TABLE Persons( PersonID integer NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) ); http://www.w3schools.com/sql/trysql.asp?filename=trysql_create_constraint_not_null

Constraints: Unique UNIQUE We can specify that an individual column must be unique E.g. CREATE TABLE Persons( PersonID integer NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) );

Constraints: Unique CREATE TABLE Persons( UNIQUE Alternatively, we can specify that two or more combined columns must be unique when taken together using the CONSTRAINT keyword E.g. CREATE TABLE Persons( PersonID integer NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255), CONSTRAINT ID_Nm UNIQUE (PersonID,LastName) );

Constraints: Primary Key Combines NOT NULL and UNIQUE but may only be used once per table As with UNIQUE, we can specify that an individual column must be the primary key E.g. CREATE TABLE Persons( PersonID integer PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) );

Constraints: Primary Key Alternatively, we can specify that two or more combined columns must be unique when taken together using the CONSTRAINT keyword E.g. CREATE TABLE Persons( PersonID integer NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255), CONSTRAINT Pk PRIMARY KEY (PersonID,LastName) );

Constraints: Auto-Increment Sometimes (most often for the primary key) we want to automatically generate a new unique number E.g. for MS Access, we will write CREATE TABLE Persons( PersonID integer NOT NULL PRIMARY KEY AUTOINCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) );

Constraints: Auto-Increment Note the syntax for Oracle is little different and significantly more complicated First we need to create a sequence object CREATE SEQUENCE Cust_sequence MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10; Then we refer to a function of that object called nextval INSERT INTO Persons (ID) VALUES (Cust_sequence .nextval'); Don’t worry about memorising this – you can look it up if it comes up in the future

Constraints: Foreign Key Ensures referential integrity with other table(s) E.g. CREATE TABLE Persons( PersonID integer NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255), CONSTRAINT Pk PRIMARY KEY (PersonID, LastName), CONSTRAINT fk_PerOrders FOREIGN KEY (PersonID) REFERENCES Customers(CustomerID) );

Constraints: Check CHECK Limits the value range that can be placed in a column Can be used for a simply check on one column E.g. CREATE TABLE Persons( PersonID integer NOT NULL CHECK (PersonID > 0), LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255), CONSTRAINT Pk PRIMARY KEY (PersonID, LastName), CONSTRAINT fk_PerOrders FOREIGN KEY (PersonID) REFERENCES Customers(CustomerID) );

Constraints: Check CHECK May also be used for a more complex check E.g. CREATE TABLE Persons( PersonID integer NOT NULL CHECK (PersonID > 0), LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255), CONSTRAINT Pk PRIMARY KEY (PersonID, LastName), CONSTRAINT Fk_PerOrders FOREIGN KEY (PersonID) REFERENCES Customers(CustomerID) CONSTRAINT Chk CHECK (PersonID > 0 AND City LIKE '[a-z]') );

Constraints: Default DEFAULT (specifies a default value) Inserts a default value into a column E.g. CREATE TABLE Persons( PersonID integer NOT NULL CHECK (PersonID > 0), LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) DEFAULT ‘Cork’, CONSTRAINT Pk PRIMARY KEY (PersonID, LastName), CONSTRAINT Fk_PerOrders FOREIGN KEY (PersonID) REFERENCES Customers(CustomerID) CONSTRAINT Chk CHECK (PersonID > 0 AND City LIKE '[a-z]') );

SQL Indices An index is a pointer to data on a table, which is included to speed up searches of long tables (though they slow down other queries) Think of an index at the back of a book The content of a book is ordered in a standard path Sometimes we want content by a different order, e.g. by recurring phrases, by references to specific authors By adding an index we can now search the book more efficiently according to other predefined paths However, adding a page to the book means all of our indices are thrown off and need to be reconfigured

Example of an Index ID Company 1 Barclays 2 Fidelity 3 Apple 4 CBS 5 Dell 6 Ericsson 2 6 1 3 4 5 Company ID 1 Apple 3 2 Barclays CBS 4 Dell 5 Ericsson 6 Fidelity

Create Index We can create three types of index in SQL Single column indices CREATE INDEX index_name ON table_name (column_name) E.g. CREATE INDEX Nm_index ON Employees (LastName) Multiple/composite column indices CREATE INDEX index_name ON table_name (col_name, col_name2) E.g. CREATE INDEX Nm_index ON Employees (LastName,FirstName) Unique indices CREATE UNIQUE INDEX index_name ON table_name (column_name) E.g. CREATE UNIQUE INDEX Nm_index ON Employees (LastName)

Drop We use DROP queries when we wish to remove indices, tables, or entire databases within our SQL Dropping an index is done from the following structure DROP INDEX index_name ON table_name; Dropping a table is done as follows DROP TABLE table_name; Dropping an database as follows DROP DATABASE database_name;

Alter Finally, sometimes we want to add a column or modify a column’s data type in our SQL To add one or more columns ALTER TABLE table_name ADD column_name datatype; We can also combine ALTER and DROP to remove specific columns as follows ALTER TABLE table_name DROP COLUMN column_name;

Alter Altering datatypes varies subtly for different DBMSs (see http://www.w3schools.com/sql/sql_alter.asp) For MS Access, we use ALTER TABLE table_name ALTER COLUMN column_name datatype For Newer versions of Oracle, we use ALTER TABLE table_name MODIFY column_name datatype;

Views We use views as virtual tables based on the result-set of a SELECT query There are basically three reasons we do this Views hide complexity, e.g. we want to assemble rows and columns from several tables but don’t want to have to keep doing it every time Views for security, e.g. we want specific permissions to apply to the view but not the whole table Views for legacy code, e.g. the underlying table structure has changed but we don’t want users to have to worry about this

Views We use views as virtual tables based on the result-set of a SELECT query There are basically three reasons we do this Views hide complexity, e.g. we want to assemble rows and columns from several tables but don’t want to have to keep doing it every time Views for security, e.g. we want specific permissions to apply to the view but not the whole table Views for legacy code, e.g. the underlying table structure has changed but we don’t want users to have to worry about this Views are always up to date, as they are populated anew each time a user queries them

Create View We use views as virtual tables based on the result-set of a SELECT query The basic syntax for creating a view is: CREATE VIEW [view_name ] AS SELECT column_name(s) FROM table_name WHERE condition E.g. CREATE VIEW [Early_Customer_List] AS SELECT CustomerName, Address FROM Customers WHERE CustomerID < 20;

Replacing or Dropping Views In addition to creating views, we can modify/replace them as follows: CREATE OR REPLACE VIEW [view_name ] AS SELECT column_name(s) FROM table_name WHERE condition; Or alternatively, drop them completely DROP VIEW [view_name];

Exercise Consider the following problems related to the Customers database, what queries best solve them? We want to create a new table in our database called Inventory with the following criteria Three columns for Part_ID, Part_Name, and Part_of_Product All three of these columns set to not null The Part_ID set as primary key The Part_of_Product set as a foreign key to Products(ProductID) The default Part_Name is ‘Phalange’

Exercise Run the following query to see what the database looks like INSERT INTO Inventory (Part_ID,Part_of_Product) VALUES ('1', '4'); INSERT INTO Inventory (Part_ID,Part_of_Product,Part_Name) VALUES ('3', '5', 'Cogs'); We want to create an index for the Part_Name, so that this can be searched independently? We want to add a column to Inventory called Cost with type varchar(20)? We want to create a view [Phalanges] showing where Part_ID, Part_of_Product, and Cost where the Part_Name is ‘Phalange’?