06 SQL1 Create
06 SQL1 Create
ICT - 209
Database Applications
Introduction to MySQL
Creating Tables
1
Contents
• Introduction to MySQL
• Create a table
• Specify keys and relations
• Empty and Drop tables
2
Introduction
3
Online Resource
http://dev.mysql.com/doc/refman/5.0/en/index.html
• Is Oracle’s MySQL documentation site – note the 5.0 is the version we will
use in practicals here. You will need:
4
Database Engines
http://dev.mysql.com/doc/refman/5.0/en/storage-engines.html
5
Making MySQL Statements
E.G
• You can drop the use of quotes if it is safe to do so, for example for names
with no spaces or special characters.
6
SQL: CREATE TABLE
• The simplest form of CREATE TABLE looks like this:
CREATE TABLE IF NOT EXISTS tablename
(colname datatype,
... )
7
Data Types
• VARCHAR
• INT
• DECIMAL
• DATE
• TEXT
• BOOL
• And others that you can read about here
http://dev.mysql.com/doc/refman/5.0/en/data-types.html
8
Table Constraints
• And we can add table constraint(s) before the closing bracket. Both types of
constraint are used to protect integrity (next slide)
9
SQL: CREATE TABLE: Integrity
10
SQL: CREATE TABLE: integrity (continued): cascaded
deletion
• We can add ON DELETE CASCADE to the REFERENCES part
• This means that if a row in the other table is deleted, all matching
rows in this table should be deleted too
• So if we delete employee 123 from the Employee table, then then all
his/her dependants are deleted from the Dependant table, thus
protecting referential integrity
• We should only do this for weak entities
– it would be a disaster if we deleted (say) student records as a
result of deleting an Adviser of Studies
11
SQL: CREATE TABLE: integrity (continued)
12
SQL: CREATE TABLE: integrity (continued)
– these look like column-constraints, but they can reference more than
one column
CREATE TABLE HTR
(Hour char(6),
Teacher char(3),
Room char(4),
PRIMARY KEY (Hour, Teacher)) ;
13
SQL: CREATE TABLE: Integrity (continued)
could be written as
CREATE TABLE Staff
(Sno NUMBER,
Sname VARCHAR(20) NOT NULL,
Dept VARCHAR(20),
Grade VARCHAR(7),
PRIMARY KEY(Sno),
CONSTRAINT FOREIGN KEY (Dept) REFERENCES Depts (Dname),
CONSTRAINT FOREIGN KEY (Grade) REFERENCES Paytable)
14
SQL: ALTER TABLE
• We can change tables, using ALTER TABLE, even after they contain data
• Amongst other possibilities, we can add or modify columns
ALTER TABLE Staff ADD
(StreetAddress VARCHAR(20),
TownAddress VARCHAR(20)) ;
(we can enter staff before departments, then switch on the constraint)
15
SQL: ALTER TABLE, continued
16
Dropping and deleting
• We can completely remove a table: both its data (if any) and its definition
DROP TABLE tablename ;
DROP TABLE tablename CASCADE CONSTRAINTS ;
– the second form removes Foreign Key constraints in associated tables
(which otherwise could not be updated)
17
Getting data into tables
• There are two ways of using SQL to get data into tables
• Firstly, with the values in the SQL statement
INSERT INTO Staff VALUES
(123,'Lee','CompSci','II.7') ;
18
Getting More Data In
19
Changing Data in a Table
20