You are on page 1of 20

CSE

Department Computer
Science
of &Engineering

Introduction to SQL

Lecture 15
CSC 401: Database Management System
Purposes of the SQL standard
To specify the syntax and semantics of SQL data definition and manipulation languages

To define the data structures and basic operations for designing, accessing, maintaining,

controlling, and protecting an SQL database


To provide a vehicle for portability of database definition and application modules

between conforming DBMSs


To specify both minimal (Level 1) and complete (Level 2) standards, which permit

different degrees of adoption in products


To provide an initial standard, although incomplete, that will be enhanced later to

include specifications for handling such topics as referential integrity, transaction


management, user-defined functions, join operators beyond the equi-join, and national
character sets

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Benefits
Reduced training costs

Productivity

Application portability

Application longevity

Reduced dependence on a single vendor

Cross-system communication

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
SQL Environment
Relational DBMS (RDBMS) A database management system

that manages data as a collection of tables in which all data


relationships are represented by common values in related tables.
Catalog A set of schemas that, when put together, constitute a

description of a database.
Schema A structure that contains descriptions of objects created

by a user, such as base tables, views, and constraints, as part of a


database.

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
CSE
Department Computer
Science
of &Engineering Database Group
SQL command
Data definition language (DDL) Commands used to define a

database, including those for creating, altering, and dropping tables


and establishing constraints.
Data manipulation language (DML) Commands used to maintain

and query a database, including those for updating, inserting,


modifying, and querying data.
Data control language (DCL) Commands used to control a

database, including those for administering privileges and


committing (saving) data.

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Notation used
All-capitalized words denote commands.

Lowercase and mixed-case words denote values that must be

supplied by the user.


Brackets enclose optional syntax.

An ellipsis (. . .) indicates that the accompanying syntactic clause

may be repeated as necessary.


Each SQL command ends with a semicolon (;).

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Generating SQL Database Definitions
CREATE SCHEMA
Used to define the portion of a database that a particular user owns. Schemas are
dependent on a catalog and contain schema objects, including base tables and
views, domains, constraints, assertions, character sets, collations, and so forth.
CREATE TABLE
Defines a new table and its columns. The table may be a base table or a derived
table. Tables are dependent on a schema. Derived tables are created by executing a
query that uses one or more tables or views.
CREATE VIEW
Defines a logical table from one or more tables or views. Views may not be
indexed. There are limitations on updating data through a view. Where views can
be updated, those changes can be transferred to the underlying base tables
originally referenced to create the view.

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Creating Tables (Data Dictionary)
Identify the appropriate data type, including length, precision, and scale, if required,

for each attribute.


Identify the columns that should accept null values.

Identify the columns that need to be unique.

Identify all primary key–foreign key mates. Foreign keys can be established

immediately, as a table is created, or later by altering the table.


Determine values to be inserted in any columns for which a default value is desired.

Identify any columns for which domain specifications may be stated that are more

constrained than those established by data type.


Create the table and any desired indexes, using the CREATE TABLE and CREATE

INDEX statements.

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
General syntax of the CREATE TABLE

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Typical CREATE TABLE in action
CREATE TABLE Customer_T(
CustomerID NUMBER(11,0) NOT NULL,
CustomerName VARCHAR2(25) NOT NULL,
CustomerAddress VARCHAR2(30),
CustomerCity VARCHAR2(20),
CustomerState CHAR(2),
CustomerPostalCode VARCHAR2(9),
CONSTRAINT Customer_PK PRIMARY KEY (CustomerID)
);

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
CREATE TABLE with FOREIGN KEY
CREATE TABLE Order_T (
OrderID NUMBER(11,0) NOT NULL,
OrderDate DATE DEFAULT SYSDATE,
CustomerID NUMBER(11,0),
CONSTRAINT Order_PK PRIMARY KEY (OrderID),
CONSTRAINT Order_FK FOREIGN KEY (CustomerID)
REFERENCES Customer_T(CustomerID)
);

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
CREATE TABLE with CHECK
CREATE TABLE Product_T(
ProductID NUMBER(11,0) NOT NULL,
ProductDescription VARCHAR2(50),
ProductFinish VARCHAR2(20) CHECK (ProductFinish IN
('Cherry', 'Natural Ash', 'White Ash', 'Red Oak', 'Natural Oak', 'Walnut')),
ProductStandardPrice DECIMAL(6,2),
ProductLineID INTEGER,
CONSTRAINT Product_PK PRIMARY KEY (ProductID)
);

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
CREATE TABLE with multiple FOREIGN KEY
CREATE TABLE OrderLine_T(
OrderID NUMBER(11,0) NOT NULL,
ProductID INTEGER NOT NULL,
OrderedQuantity NUMBER(11,0),
CONSTRAINT OrderLine_PK PRIMARY KEY (OrderID, ProductID),
CONSTRAINT OrderLine_FK1 FOREIGN KEY (OrderID)
REFERENCES Order_T(OrderID),
CONSTRAINT OrderLine_FK2 FOREIGN KEY (ProductID)
REFERENCES Product_T(ProductID)
);

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Ensuring data integrity through updates

Restricted Update: A customer ID can only be Cascaded Update: Changing a customer ID in the
deleted if it is not found in ORDER table. CUSTOMER table will result in that value changing
CREATE TABLE CustomerT( in the ORDER table to match.

CustomerID INTEGER DEFAULT ‘999’ NOT … ON UPDATE CASCADE);


NULL, Set Null Update: When a customer ID is changed,
CustomerName VARCHAR(40) NOT NULL, any customer ID in the ORDER table that matches
… the old customer ID is set to NULL.

CONSTRAINT Customer_PK PRIMARY KEY … ON UPDATE SET NULL);


Set Default Update: When a customer ID is
(CustomerID), changed, any customer ID in the ORDER tables that

ON UPDATE RESTRICT); matches the old customer ID is set to a predefined


default value.
… ON UPDATE SET DEFAULT);

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Changing Table Definitions
Syntax: ALTER TABLE table_name alter_table_action
Some of the alter_table_actions available are:
ADD [COLUMN] column_definition
ALTER [COLUMN] column_name SET DEFAULT default-value
ALTER [COLUMN] column_name DROP DEFAULT
DROP [COLUMN] column_name [RESTRICT] [CASCADE]
ADD table_constraint

Example:
ALTER TABLE CUSTOMER_T
ADD COLUMN CustomerType VARCHAR2 (2) DEFAULT “Commercial”;

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Removing Tables
DROP TABLE Customer_T;

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group
Thank You

CSE
Department Computer
Science
of &Engineering CSC 401: database Management System Database Group

You might also like