0% found this document useful (0 votes)
7 views24 pages

Chapter 08 1

Chapter 8 of COMP255 covers advanced SQL concepts including creating and manipulating tables using SQL commands. It explains Data Definition Language (DDL), database schemas, data types, and constraints such as primary and foreign keys. The chapter also details how to alter existing tables and manage database objects effectively in MySQL/MariaDB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Chapter 08 1

Chapter 8 of COMP255 covers advanced SQL concepts including creating and manipulating tables using SQL commands. It explains Data Definition Language (DDL), database schemas, data types, and constraints such as primary and foreign keys. The chapter also details how to alter existing tables and manage database objects effectively in MySQL/MariaDB.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

COMP255

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

CREATE DATABASE phuegler_newdb;


5
Creating Databases in MySQL

Go ahead and create the databases you need
to work on the assignments

Run CREATE command in any database

Refresh database in MySQL to show

Create new query to use

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

ALTER TABLE PRODUCT


MODIFY P_QOH INTEGER;

ALTER TABLE PRODUCT


DROP P_QOH;

23
Adding Constraints
CREATE OR REPLACE TABLE PRODUCT
(
P_CODE varchar(10),
P_DESCRIPT varchar(35) NOT NULL,
V_CODE integer
);

ALTER TABLE PRODUCT


ADD CONSTRAINT PRIMARY KEY (P_CODE);
ALTER TABLE PRODUCT
ADD CONSTRAINT FOREIGN KEY (V_CODE)
REFERENCES VENDOR (V_CODE);
24

You might also like