0% found this document useful (0 votes)
3 views

Chapter 7.1 - SQL DDL v1.0

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 7.1 - SQL DDL v1.0

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Chapter 7 (SQL DDL)

Data Definition Language

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
Introduction to SQL
 Standard language for querying and manipulating data
 Structured Query Language
 Categories of SQL functions:
 Data definition language (DDL)
 Data manipulation language (DML)

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
2
SQL Functions
 Data Definition Language (DDL)
 Create/alter/delete tables and their attributes
 Data Manipulation Language (DML)
 Query one or more tables
 Insert/delete/modify tuples(rows) in tables
Top DBMS

USE THIS
https://livesql.oracle.com

For ERD Modelling: https://lucid.app


©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
SQL Data Definition Command

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
4
SQL Data Manipulation Commands

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
5
Creating the Database
 Authentication is the process DBMS uses to verify
that only registered users access the database
 Required for the creation tables/manipulating tables
 User should log on to RDBMS using user ID and
password created by database administrator
 http://ec2-52-14-236-152.us-east-2.compute.amazonaw
s.com/phpmyadmin/

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
6
Syntax for CREATE
NOTE: SQL scripts are not case-sensitive

 Create a table
CREATE TABLE table_name
(
Each field is
fieldname_1 datatype(size) CONSTRAINT,
separated by
…. comma. The last
field does not
fieldname_n datatype(size), require a comma.

PRIMARY KEY(fieldname_1)
);

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
7
Data Definition Language- Create Table
 Create table – is a command use to create a new table
structure.
Table name When creating a table, you
have to determine:
CREATE TABLE course
Datatype Size • Datatype and size
( Column Constraint • Constraints
course_code VARCHAR(6) NOT NULL , • Primary/Foreign keys

course_title VARCHAR(100) NOT NULL , Note for data types:


VARCHAR – alphanumeric
description VARCHAR(100) , INTEGER – numeric (whole
prerequisite VARCHAR(6) NULL , number only)
When assigning
units INTEGER, column/field names, do
PRIMARY KEY (course_code) not use:
• Special characters
); • Space
>> To display the structure of the table
DESCRIBE course;
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
8
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
SQL Data Types

TIME
Numeric with decimal digits
Fixed length alphanumeric ex. Gender CHAR(1)
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
9
Practice Exercise
Create a SQL table as shown below. Determine the correct datatypes.

Declare as primary key

varchar

birthday date

ANSWER:
CREATE TABLE Stud_youridnumber
(
stud_id VARCHAR(6) NOT NULL,
firstname VARCHAR(30),
lastname VARCHAR(30),
email VARCHAR(50),
birthday DATE,
PRIMARY KEY(stud_id)
);
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
10
SQL Constraints
NOT NULL
• Ensures that column does not accept nulls

UNIQUE
• Ensures that all values in column are unique

DEFAULT
• Assigns value to attribute when a new row is
added to table

CHECK
• Validates data when attribute value is entered
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
11
Primary Keys (PK)
 A PRIMARY KEY can be re-established using
ALTER command
 Use ALTER TABLE command to ADD/CHANGE
primary key. Primary key was declared only
after creating a table

ALTER TABLE student ADD PRIMARY KEY(stud_id);

ALTER TABLE student DROP PRIMARY KEY, ADD PRIMARY


KEY(stud_id);

Changes the primary key after


creating a table
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
12
MORE THAN ONE PK?
CREATE TABLE enrollment
(
stud_id VARCHAR(6) NOT NULL,
course_id VARCHAR(6) NOT NULL,
final_grade DECIMAL(3,2),
date_enrolled DATE,
PRIMARY KEY(stud_id, course_id)
);

Composite identifier

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
FOREIGN KEYS (FK)
 A foreign key is a column or combination of columns which can be used to set a
link between the data in two tables. PRIMARY KEY of a table is linked to the
FOREIGN KEY of another table to enhance data integrity.

Syntax :

FOREIGN KEY [column list] REFERENCES [primary key table]


([column list]);
Parent Table Child Table CREATE TABLE client(
(Referenced Table) (Referencing Table) clientid INT NOT NULL,
name VARCHAR(50),
phone VARCHAR(25),
PRIMARY KEY(clientid)
);
CREATE TABLE booking(
bookingid INT NOT NULL,
datebooked DATE,
Example of Weak Relationship deliveredby VARCHAR(50),
clientid INT,
NOTE: When creating tables, you need PRIMARY KEY(bookingid),
to create first the parent table, then the FOREIGN KEY(xxx) REFERENCES xxx(xxx)
);
child table follows.
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
FOREIGN KEYS (FK)

CLIENT
ClientID Name Phone
2018045 Peter Parker 0561580523
2019423 Tom Jones 0723456231

BOOKING
BookingI DateBooke Deliveredb ClientID
D d y This generates an
error because it
101 2021-06-03 Bruce James 2018045 does not exist in
102 2021-07-23 John Doe 2016231 CLIENT table

103 2021-08-18 Jane Doe 2019423


©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
Data Definition Language - ALTER
 Besides altering primary key, ALTER TABLE can
also be use to:
 ADD - Adds a column
 MODIFY - Changes column characteristics
 DROP - Deletes a column
 Used to:
 Add table constraints
 Remove table constraints

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
16
Changing a Column’s Data Type and Data
Characteristics
 ALTER used to change data type and characteristics
 Some RDBMSs do not permit changes to data types unless
column is empty
 Changes in characteristics are permitted if they do not alter
the existing data type
 Syntax:
 ORIGINAL:
 `PROF_ID` VARCHAR(25) NOT NULL
 ALTER TABLE PROFESSOR MODIFY PROF_ID INT(10) NULL;
 This syntax change the PROF_ID data type from VARCHAR to INT and making it from
NOT NULL to NULL

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
17
Adding and Dropping Columns
 Adding a column
 Use ALTER and ADD

ALTER TABLE `PROFESSOR` ADD `PROF_OFFICE_NUM` INT NULL


AFTER `DEPARTMENT`;

 Dropping a column
 Use ALTER and DROP
ALTER TABLE `PROFESSOR` DROP `DEPARTMENT`;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
18
Deleting a Table from the Database
• DROP TABLE: Deletes table from database
DROP TABLE STUDENT

• RDBMS generates a foreign key integrity violation


error message if the table is dropped

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
19
PRACTICE EXERCISE
Design an ERM (Crowe’s Foot) and database to manage the
list of members renting an apartment.

Each member may rent as many apartments as they wish.


Each apartment should be rented by one member only at a time.
However, there are instances that an apartment do not have any
members renting it at all.

MEMBER rents APARTMENT


mem_num (PK) ap_num (PK)
mem_fname Rent_date
mem_lname
mem_city

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
MEMBERSHIP
LET’S CREATE REL.-DB
Mem_ Mem_ Mem_ Mem_ Mem_Zi
Mem_Street Mem_City Mobile
Num Fname Lname State p

101 Tami Dawson 2632 Takli Circle Norene TN 37136 0561523564


102 Curt Knight 4025 Cornell Court Flatgap KY 41219 0582523565
103 Jamal Melendez 788 East 145th Avenue Quebeck TN 38579 0564523564

104 Iva Mcclain 6045 Musket Ball Circle Summit KY 42783 0571223564

105 Miranda Parks 4469 Maxwell Place Germantown TN 38183 0511527564

APARTMENT
Ap_Num Rent_Date Mem_Num
1001 01-MAR-16 102
1002 01-MAR-16 101
1003 02-MAR-16 102
1007 02-MAR-16 104
1008 03-MAR-16 105
Write the SQL code to create the table structures for the entities shown in figure
above. Also try to include the values shown in the table into your new developed
database.
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
CREATE member TABLE
CREATE TABLE xxx
(
xxx INTEGER NOT NULL ,
mem_fname VARCHAR(25) NOT NULL ,
mem_lname VARCHAR(50) NULL ,
mem_street VARCHAR(100),
mem_city VARCHAR(50),
mem_state xxx,
mem_zip INTEGER,
mobile VARCHAR(20) NOT NULL,
PRIMARY KEY (xxx)
);

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
CREATE apartment TABLE
CREATE TABLE apartment
( NOTE:
ap_num INTEGER NOT NULL , • This code does not
rent_date DATE NOT NULL , contain yet the
mem_num INTEGER NOT NULL , FOREIGN KEY
PRIMARY KEY (ap_num)
Default format of date in Oracle:
);
dd-mmm-yy or dd-mmm-yyyy

To double check the success of your table creation, run the script:
describe apartment;
describe member;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
ASSIGN FOREIGN KEY (Option 1)
CREATE TABLE apartment
( NOTE:
ap_num INTEGER NOT NULL , • This is the suggested
rent_date DATE NOT NULL , option to assign
mem_num INTEGER NOT NULL , FOREIGN KEY
PRIMARY KEY (ap_num),
FOREIGN KEY (xxx) REFERENCES xxx(xxx)
);

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
Practice Exercise
 Write a script to create the two tables below. Ensure
to declare the PK and FK. Insert only the first two
records in each table.

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
Practice Exercise Answer Key
CREATE TABLE salesman(
salesman_id VARCHAR(5) NOT NULL,
name VARCHAR(50),
city VARCHAR(20),
commission DECIMAL(3,2),
PRIMARY KEY(salesman_id)
);

CREATE TABLE Customer(


customer_id VARCHAR(5) NOT NULL,
customer_name VARCHAR(15),
city VARCHAR(20),
INT does not
grade INT, require a size
salesman_id VARCHAR(5) NOT NULL,
PRIMARY KEY(customer_id),
FOREIGN KEY(salesman_id) REFERENCES salesman(salesman_id)
);
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
Practice Exercise Answer Key

INSERT INTO salesman VALUES('5001', 'James Hoog', 'New York', '0.15');


INSERT INTO salesman VALUES('5002', 'Nail Knite', 'Paris', '0.13');

INSERT INTO Customer VALUES('3002', 'Nick Rimando', 'New York', '100', '5001');
INSERT INTO Customer VALUES('3007', 'Brad Davis','New York', '200','5001');

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
ASSIGN FOREIGN KEY (Option 2)
ALTER TABLE apartment
ADD CONSTRAINT FK_mem_num
FOREIGN KEY (mem_num) REFERENCES member(mem_num)
ON DELETE RESTRICT
ON UPDATE CASCADE;

©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.

You might also like