Chapter 7.1 - SQL DDL v1.0
Chapter 7.1 - SQL DDL v1.0
©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
©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
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.
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
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 :
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
©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
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
©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.
©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
104 Iva Mcclain 6045 Musket Ball Circle Summit KY 42783 0571223564
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)
);
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.