Chapter 08 1
Chapter 08 1
Chapter 8
Advanced SQL
Sections 8.1 to 8.3
1
Learning Objectives: Ch 8, Part 1
●
Use SQL to create a table manually
●
Use SQL to create a copy of a table using a
subquery
●
Manipulate the structure of existing tables to
add, modify, and remove columns and
constraints
2
DDL
●
Data Definition Language
●
Statements used to create database objects
– Tables, indexes, views, etc.
3
Database Schema
●
Shema
– A logical grouping of database objects, such as tables,
indexes, views, and queries, that are related to each
other
– Usually, a schema belongs to a single user or application
●
In MySQL/MariaDB
– Database = Schema
4
Using Databases in MySQL
●
MySQL in VS Code lists databases you have
access too
●
You have the ability to create new databases
– Names limited to beginning with <username>_
– My database user name is phuegler
6
Data Types
●
Table columns need to have a data type
– Helps with data errors
●
Many different data types
– Different in different DBMS
●
Data types in MariaDB (MySQL)
– https://mariadb.com/kb/en/data-types/
7
General SQL Data Types
Use
NUMERIC
in MySQL
and
MariaDB
8
Data Dictionary
●
Lists the fields in a table with
– Attribute name
– Description
– Data type
– Format
– Range
– Required
– Primary key, foreign key, FK table reference
●
Used to organize creating tables
9
10
Creating Tables
●
Use the CREATE TABLE statement
●
Identify
– Table name
– Columns with data types
– Constraints (primary key, foreign key, etc.)
11
Basic Command
CREATE TABLE VENDOR
(
V_CODE integer,
V_NAME varchar(35),
V_CONTACT varchar(25),
V_AREA_CODE char(3),
V_PHONE char(8),
V_STATE char(2),
V_ORDER char(2)
);
12
Creating the Same Table Again
●
If table exists, you cannot just use CREATE
TABLE
●
Need to drop table first
– DROP TABLE <TABLE NAME>;
– Or right click on table in VS Code and choose drop
13
Another Way
●
Use the OR REPLACE clause
CREATE OR REPLACE TABLE VENDOR
(
V_CODE integer,
V_NAME varchar(35),
V_CONTACT varchar(25),
V_AREA_CODE char(3),
V_PHONE char(8),
V_STATE char(2),
V_ORDER char(2)
);
14
Constraints
●
Used to add
– Primary Keys
– Foreign Keys
– Required (not null)
●
Field cannot be NULL
– Unique
●
Entries in column MUST all be unique
15
Two Ways to Add
●
Add as part of the column definition
– Good for NOT NULL and UNIQUE
●
Add as addition clause in CREATE TABLE
– Good for primary and foreign keys
16
NOT NULL and UNIQUE
●
NOT NULL
– Values cannot be NULL
– Use for fields that are required
●
UNIQUE
– No duplicate values in column
– Needed for primary keys but automatically included in
primary key constraint
17
Using the Column Definition
CREATE OR REPLACE TABLE VENDOR
(
V_CODE integer,
V_NAME varchar(35) NOT NULL,
V_CONTACT varchar(25) NOT NULL,
V_AREA_CODE char(3) NOT NULL,
V_PHONE char(8) NOT NULL,
V_STATE char(2) NOT NULL,
V_ORDER char(2) NOT NULL
);
18
Using Additional Clauses
●
Best way for primary and foreign keys
●
Can apply in column definition but…
– Only if there is one column in the key
●
Better to use additional clauses
– Will need to use when more than one column in the
key
19
Primary Key Clause
●
Basic clause
– PRIMARY KEY (column1, column2, ...)
CREATE OR REPLACE TABLE VENDOR
(
V_CODE integer,
V_NAME varchar(35) NOT NULL,
V_CONTACT varchar(25) NOT NULL,
V_AREA_CODE char(3) NOT NULL,
V_PHONE char(8) NOT NULL,
V_STATE char(2) NOT NULL,
V_ORDER char(2) NOT NULL,
PRIMARY KEY (V_CODE )
); 20
Foreign Key Clause
●
Basic clause
– FOREIGN KEY (column1, column2, ...)
REFERENCES <table name> (column1, ...)
CREATE OR REPLACE TABLE PRODUCT
(
P_CODE varchar(10),
P_DESCRIPT varchar(35) NOT NULL,
V_CODE integer,
PRIMARY KEY (P_CODE),
FOREIGN KEY (V_CODE)
REFERENCES VENDOR (V_CODE)
); 21
Changing Tables
●
Use the ALTER TABLE statement
●
Can
– Add or remove columns
– Modify columns
– Add or remove constraints
22
Add, Change, Drop a Column
ALTER TABLE PRODUCT
ADD ( P_QOH SMALLINT );
23
Adding Constraints
CREATE OR REPLACE TABLE PRODUCT
(
P_CODE varchar(10),
P_DESCRIPT varchar(35) NOT NULL,
V_CODE integer
);