IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL 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 Relational Databases and SQL
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.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
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.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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)
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
INTERNET APPLICATION DEVELOPMENT For More visit:
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
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.
1 What is database 2? What is normalization? What is SQL? What is transaction?
Database Programming Sections 11 & 12 – Creating, and Managing Views, Sequences, Indexes, and Synonymns.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
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)
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
Populating and Querying tables Insert, Update, Delete and View (DML)
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table,,,. (Data Manipulation Language) DML.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
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.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
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.
Fundamentals of DBMS Notes-1.
Rob Gleasure robgleasure.com
Web Systems & Technologies
From: SQL From:
Rob Gleasure robgleasure.com
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL Creating and Managing Tables
SQL Creating and Managing Tables
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)
CH 4 Indexes.
Oracle Data Definition Language (DDL)
Rob Gleasure robgleasure.com
Data Definition Language
Instructor: Samia arshad
Presentation transcript:

IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com

IS6146 Today’s session  The DDL Create Database Alter Database Create Table Alter Table Drop Table Create Index Drop Index  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  e.htm You can find a list of data types for MS Access, MySQL and MS SQL Server at 

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

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 int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) );  _constraint_not_null

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

Constraints: Unique  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 int 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  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 int PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) );

Constraints: Primary Key  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 int 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  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 int NOT NULL PRIMARY KEY AUTOINCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Address varchar(255), City varchar(255) );

Constraints: Auto-Increment  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');

Constraints: Foreign Key  FOREIGN KEY Ensures referential integrity with other table(s) E.g. CREATE TABLE Persons( PersonID int 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 int 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 int 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 int 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 IDCompany 1Barclays 2Fidelity 3Apple 4CBS 5Dell 6Ericsson CompanyID 1Apple3 2Barclays1 3CBS4 4Dell5 5Ericsson6 6Fidelity

Create Index We can create three types of index in SQL 1. Single column indices CREATE INDEX index_name ON table_name (column_name)  E.g. CREATE INDEX Nm_index ON Employees (LastName) 2. 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) 3. 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 or modify the data type of one or more columns 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 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 1. 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 2. Views for security, e.g. we want specific permissions to apply to the view but not the whole table 3. 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 1. 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 2. Views for security, e.g. we want specific permissions to apply to the view but not the whole table 3. 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? 1. We want to create a new table in our database called Inventory with the following criteria I. Three columns for Part_ID, Part_Name, and Part_of_Product II. All three of these columns set to not null III. The Part_ID set as primary key IV. The Part_of_Product set as a foreign key to Products(ProductID) V. 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'); 2. We want to create an index for the Part_Name, so that this can be searched independently? 3. We want to add a column to Inventory called Cost with type varchar(20)? 4. We want to create a view [Phalanges] showing where Part_ID, Part_of_Product, and Cost where the Part_Name is ‘Phalange’?