Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rob Gleasure robgleasure.com

Similar presentations


Presentation on theme: "Rob Gleasure robgleasure.com"— Presentation transcript:

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

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

3 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

4 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), .... );

5 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 You can find a list of data types for MS Access, MySQL and MS SQL Server at

6 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

7 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, .... );

8 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) );

9 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) );

10 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) );

11 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) );

12 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) );

13 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) );

14 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

15 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) );

16 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) );

17 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]') );

18 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]') );

19 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

20 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

21 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)

22 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;

23 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;

24 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;

25 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

26 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

27 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;

28 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];

29 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’

30 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’?


Download ppt "Rob Gleasure robgleasure.com"

Similar presentations


Ads by Google