Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
TUSQL
1
Structured Query Language
2
SQL lowdown
• Used by Relational Database Management Systems (RDBMS)
• Used to create and alter a DB
• Used to query a DB
3
RDBMS quick l👀k
• The standard for DBs these days
• Based on relations
• Tables are related via common fields (FK = PK)
4
Relations are modeled with tables
5
Tables a.k.a Entities
6
The schema ...
7
Course
Field Details
id INT(11) NOT NULL AUTO_INCREMENT
number VARCHAR(10) NOT NULL
title VARCHAR(50) DEFAULT NULL
credits INT(1) DEFAULT NULL
The records ...
8
Course
id number title credits
1 cs234 Database and Web Development 3
2 cs150 Introduction to Computing II 3
3 cs240 Introduction to Computing III 3
id is the PK
Primary Key = PK
9
Each table must have a primary key (PK)
The PK uniquely identifies each record. Each record should be locatable by one and only one PK.
10
Course
id
number
title
credits
• Uniquely identifies each row
• Each table should have one
• Use int values when possible
• Could be a composite (two or more fields)
11
Foreign Key = FK
12
Tables are related via FKFK = PK
13
14
Course
id
number
title
credits
Transcript
student$id
course$id
grade
Three types of relationships
• one-to-many (most popular)
• one-to-one (rare: table too big, security is needed)
• many-to-many (common, must be modeled with two one-to-many)
15
One-to-many
16
Course
id
number
title
credits
Transcript
student$id
course$id
grade
One record in the primary table is linked to zero or more records in the secondary table.
One-to-one
17
Student
id
first
last
StudentPrivate
ssn
student$id
One record in the primary table is linked to zero or one record in the secondary table.
Used primarily for security and when a table is too big and needs to be broken down
Many-to-many
18
Course
id
number
title
credits
Transcript
student$id
course$id
grade
Student
id
first
last
One record in the primary may be linked to zero or more records in the secondary table, and one record in the secondary table may be linked to zero or more records in the primary table.
This kind of relationship must be split into two 1-to-many relationships.
Tables must be normalized
19
Normalizing is the process by which a table is restructured to eliminate design problems.
Duplication of data is to be dealt with.Ease of searching must be first.Records must be uniquely selectable.
Basically, we want a db that is easy to work with and does not waste space.
Normalization benefits
• No data redundancy
• Data access flexibility
• No inconsistent data
• Removes horizontal/vertical duplication
20
Apply three NFs
• 1NF - No horizontal duplication
• atomic fields, no repeating fields
• 2NF - 1NF & No vertical duplication
• fields should depend on entire PK
• 3NF - 2NF & Functional dependence
• fields don't depend on other fields
21
1NF - No ⬌ duplication
22
Inventory
sid sLoc sZip pNo1 pName1 qty1 pNo2 pName2 qty2
1 Chicago 60632 1 stool 5 2 lamp 15
2 Dallas 75206 1 stool 25 3 desk 10
3 Chicago 60632 2 lamp 10 4 chair 3
No repeating groups within rows No similar fields (ph1, ph2 ...) No multi value fields (first+last)What if more parts need to be stored? Design must changeSpace is waisted if supplier has fewer than two partsQuerying on parts is more difficult
23
Inventory
sid sLoc sZip pNo1 pName1 qty1 pNo2 pName2 qty2
1 Chicago 60632 1 stool 5 2 lamp 15
2 Dallas 75206 1 stool 25 3 desk 10
3 Chicago 60632 2 lamp 10 4 chair 3
Inventorysid sLoc sZip pNo pName qty1 Chicago 60632 1 stool 5
1 Chicago 60632 2 lamp 152 Dallas 75206 1 stool 252 Dallas 75206 3 desk 103 Chicago 60632 2 lamp 103 Chicago 60632 4 chair 3
We use a composite PK (sid + pNo)
Now we can have as many parts as needed
2NF - No ⬍ duplication
24
Inventorysid sLoc sZip pNo pName qty1 Chicago 60632 1 stool 51 Chicago 60632 2 lamp 152 Dallas 75206 1 stool 252 Dallas 75206 3 desk 103 Chicago 60632 2 lamp 103 Chicago 60632 4 chair 3
2NF: In 1NF and every non-key value is fully dependent on the primary key value.
The part values depend only on the pNo, not the sidThe supplier values depend only on the sid, not the pNoNotice how sid and pNo are repeated vertically
Removing is an issue: If you remove a part, you could be removing the only supplier information on hand.Updating supplier or part data necessitates multi-row updates. Change zip or part name and see what I mean.
A non-key value cannot depend only on some part of the PK
25
Supplierid location zipcode1 Chicago 606322 Dallas 752063 Chicago 60632
✅Part
supplier$id id name qty1 1 stool 51 2 lamp 152 1 stool 252 3 desk 103 2 lamp 103 4 chair 3
❌
Inventorysid sLoc sZip pNo pName qty1 Chicago 60632 1 stool 51 Chicago 60632 2 lamp 152 Dallas 75206 1 stool 252 Dallas 75206 3 desk 103 Chicago 60632 2 lamp 103 Chicago 60632 4 chair 3
Vertical repetition can be solved by splitting the table into two. One for supplier and one for part.
Basically each entity should have its own table.
26
Partsupplier$id id name qty
1 1 stool 51 2 lamp 152 1 stool 252 3 desk 103 2 lamp 103 4 chair 3
❌
SupplierPartsupplier$id id qty
1 1 51 2 152 1 252 3 103 2 103 4 3
✅Part
id name
1 stool
2 lamp
3 desk
4 chair
✅
The part table is not in 2NF since the part name only depends on the part id and not the sid.
The part table needs to be spit into two.
A supplier supplies many parts and a part (name) can be supplied by many suppliers. A stool for instance is supplied by supplier 1 and 2
This leads to a Part table describing the parts, and an associative table SupplierPart that relates the supplier with the parts it supplies. Notice the qty is an attribute of this table, representing parts on hand.
3NF - PK only describes record
27
Partid name1 stool2 lamp3 desk4 chair
✅Supplierid location zipcode1 Chicago 606322 Dallas 752063 Chicago 60632
❌ SupplierPartsupplier$id id qty
1 1 51 2 152 1 252 3 103 2 103 4 3
✅
3NF: In 2NF and every non-key value depends directly on the primary key and not on some other non-key value.
Now, the supplier table is not in 3NF, since zip code depends on location which depends on the PK. Therefore, we must split into two tables.
28
Supplierid location zipcode1 Chicago 606322 Dallas 752063 Chicago 60632
❌
Supplierid supplierzip$id1 12 23 1
✅ SupplierZipid location zipcode
1 Chicago 606322 Dallas 75206
✅
3NF: In 2NF and every non-key value depends directly on the primary key and not on some other non-key value.
Recap
• 1NF - don't duplicate across a row
• 2NF - vertical duplication requires splits
• 3NF - functional dependence (PK only), requires splits
29
30
Supplierid supplierzip$id1 12 23 1
SupplierZipid location zipcode1 Chicago 606322 Dallas 75206
SupplierPartsupplier$id part$id qty
1 1 51 2 152 1 252 3 103 2 103 4 3
Partid name1 stool2 lamp3 desk4 chair
Our Normalization method
31
ERFeD - say what?
• Identify Entities
• Establish Relations
• Add Fields
• Choose Primary Key
• Select Data Types
32
1 Entities
33
Our database needs to store supplier locations and part names. The inventory
should store a qty at hand of each partName and its supplier. A location/zip
code can have multiple suppliers.
Supplier PartSupplierPart
SupplierZip
2 Relations
34
Notice the crow feet relations that exist
The single bar is on the one-side The crow foot is not the many-side
supplies supplied
includes
Supplier PartSupplierPart
SupplierZip
3 Fields
35
supplies supplied
includes
Supplier Part
name
SupplierPart
qty
SupplierZip
location zipcode
4 Primary Key
36
supplies supplied
includes
Supplier
id supplierzip$id
Part
id name
SupplierPart
supplier$id part$id qty
SupplierZip
id location zipcode
5 Data types
37
Supplierid supplierzip$id
INT NOT NULL AUTO_INCREMENT INT NOT NULL
Partid name
INT NOT NULL AUTO_INCREMENT VARCHAR(25)
SupplierPartsupplier$id part$id qty
INT NOT NULL INT NOT NULL INT NOT NULL
SupplierZipid location zipcode
INT NOT NULL AUTO_INCREMENT VARCHAR(20) NOT NULL VARCHAR(10) NOT NULL
DDL
Data Definition Language
SQL's lingua Franca
38
DML
Data ManipulationLanguage
CREATEDB/Table
DDL
39
ALTERDB/Table
DROPDB/Table
40
Create a database CREATE DATABASE mydb;
Create a database if it does not exist CREATE DATABASE IF NOT EXISTS mydb;
Create a DB with some customization CREATE DATABASE IF NOT EXISTS mydb CHARACTER SET utf8 COLLATE utf8_danish_ci;
41
Create a table CREATE TABLE IF NOT EXISTS customer ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, first_name CHAR(30) NOT NULL, last_name CHAR(30) NOT NULL, PRIMARY KEY(id), INDEX (last_name) ) ENGINE=InnoDB;
42
CREATE TABLE IF NOT EXISTS order ( customer$id INT UNSIGNED NOT NULL, orderDetails$id INT UNSIGNED NOT NULL date DATE, amount DOUBLE(6,2), PRIMARY KEY(customer$id, orderDetails$id), FOREIGN KEY(customer$id) REFERENCES customer(id), FOREIGN KEY (orderDetails$id) REFERENCES orderDetails(id) ) ENGINE=InnoDB;
43
Alter a database's character set and collation ALTER DATABASE mydb CHARACTER SET latin1 COLLATE latin1_swedish_ci;
To see the db's schema SELECT * FROM information_schema.schemata WHERE schema_name = 'mydb'\G mysql> select * from information_schema.schemata -> where schema_name = 'courses'\G*************************** 1. row *************************** CATALOG_NAME: def SCHEMA_NAME: coursesDEFAULT_CHARACTER_SET_NAME: latin1 DEFAULT_COLLATION_NAME: latin1_swedish_ci SQL_PATH: NULL1 row in set (0.00 sec)
44
To get a table schema DESCRIBE student;
mysql> describe student;+-------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+-------+| id | int(9) | NO | PRI | NULL | || first | varchar(25) | YES | | NULL | || last | varchar(30) | YES | | NULL | |+-------+-------------+------+-----+---------+-------+3 rows in set (0.01 sec)
45
ALTER TABLE customer ADD createdOn DATE NOT NULL;
customer
idfirst_namelast_namecreatedOn
customer
idfirst_namelast_name
46
ALTER TABLE customer MODIFY last_name CHAR(40) NOT NULL;
customer
idfirst_namelast_name C(40) NNcreatedOn
customer
idfirst_namelast_name C(30) NN
47
Drop a database DROP DATABASE mydb;
Drop a database if it exists DROP DATABASE IF EXISTS mydb;
Drop a table DROP TABLE customer;
Drop a table if it exists DROP TABLE IF EXISTS customer;
SELECTrecords
DML
48
INSERTrecords
DELETErecords
UPDATErecords
49
Select from a single table
SELECT * | field1 [, field2, … fieldn] FROM table [WHERE condition] [ORDER BY field1 [ASC | DESC] [, ...]]
50
List all doll names and date purchased, list most recent first.
SELECT Character, DateAcquired FROM Dolls ORDER BY DateAcquired DESC
51
List all doll names and date purchased, list most recent first.
SELECT Character, DateAcquired FROM Dolls ORDER BY DateAcquired DESC
52
List all the dolls in the $3.00 to $9.00 range, starting with the most expensive; list their name and price. SELECT Character, PurchasePrice FROM Dolls WHERE PurchasePrice >= 3.0 AND PurchasePrice <= 9.0 ORDER BY PurchasePrice DESC
53
List all the dolls in the $3.00 to $9.00 range, starting with the most expensive; list their name and price. SELECT Character, PurchasePrice FROM Dolls WHERE PurchasePrice >= 3.0 AND PurchasePrice <= 9.0 ORDER BY PurchasePrice DESC
54
List all dolls and makers whose names begin with ‘J’; list their name and manufacturer. SELECT Character, ManufacturerID FROM Dolls WHERE Character LIKE ‘J%’
% - match 0 or more characters_ - match 1 character
55
List all dolls and makers whose names begin with ‘J’; list their name and manufacturer. SELECT Character, ManufacturerID FROM Dolls WHERE Character LIKE ‘J*’
56
Inner Join - multi-table select
SELECT parent.field,...,child.field,... FROM parent INNER JOIN child ON parent.PK = child.FK [GROUP BY field] [ORDER BY field]
57
Where Join - preferred multi-table select
SELECT parent.field,...,child.field,... FROM parent, child WHERE parent.PK = child.FK [GROUP BY field] [ORDER BY field]
58
+----+--------+-------------------------------+---------+| id | number | title | credits |+----+--------+-------------------------------+---------+| 1 | CS234 | DATABASE AND WEB DEVELOPMENT | 3 || 2 | CS150 | INTRODUCTION TO COMPUTING II | 3 || 3 | CS240 | INTRODUCTION TO COMPUTING III | 3 |+----+--------+-------------------------------+---------+
+-----------+--------+-------+| id | first | last |+-----------+--------+-------+| 111222111 | first1 | last1 || 111333111 | first2 | last2 || 111444111 | first3 | last3 |+-----------+--------+-------+
+------------+-----------+-------+| student$id | course$id | grade |+------------+-----------+-------+| 111222111 | 1 | A || 111222111 | 2 | B || 111333111 | 3 | C || 111444111 | 1 | B |+------------+-----------+-------+
59
list student id, last name, course id and grade SELECT student.id, student.last, transcript.course$id, transcript.grade FROM student, transcript WHERE student.id = transcript.student$id
student
idfirstlast
transcript
course$idstudent$idgrade
course
idnumbertitlecredits
60
+-----------+-------+-----------+-------+| id | last | course$id | grade |+-----------+-------+-----------+-------+| 111222111 | last1 | 1 | A || 111222111 | last1 | 2 | B || 111333111 | last2 | 3 | C || 111444111 | last3 | 1 | B |+-----------+-------+-----------+-------+
61
list student name, course number and grade SELECT student.first, student.last, course.number, transcript.grade FROM student, course, transcript WHERE student.id = transcript.student$id AND course.id = transcript.course$id
student
idfirstlast
transcript
course$idstudent$idgrade
course
idnumbertitlecredits
62
+--------+-------+--------+-------+| first | last | number | grade |+--------+-------+--------+-------+| first1 | last1 | CS234 | A || first1 | last1 | CS150 | B || first2 | last2 | CS240 | C || first3 | last3 | CS234 | B |+--------+-------+--------+-------+
63
list student name, course number and grade SELECT s.first, s.last, c.number, t.grade FROM student AS s, course AS c, transcript AS t WHERE s.id = t.student$id AND c.id = t.course$id
student
idfirstlast
transcript
course$idstudent$idgrade
course
idnumbertitlecredits
64
+--------+-------+--------+-------+| first | last | number | grade |+--------+-------+--------+-------+| first1 | last1 | CS234 | A || first1 | last1 | CS150 | B || first2 | last2 | CS240 | C || first3 | last3 | CS234 | B |+--------+-------+--------+-------+
65
list sid and count of courses taken SELECT student$id AS sid, COUNT(course$id) AS 'Courses Taken' FROM transcript GROUP BY student$id
student
idfirstlast
transcript
course$idstudent$idgrade
course
idnumbertitlecredits
66
+-----------+---------------+| sid | Courses Taken |+-----------+---------------+| 111222111 | 2 || 111333111 | 1 || 111444111 | 1 |+-----------+---------------+
67
Insert a record into a table
INSERT [INTO] table [(field-list)] VALUES (value-list)[, (value-list)]
68
Add the record (‘Hillary Clinton’, ‘MagicPlastic’, 9.95, 11/23,2008 into the Dolls table
INSERT INTO Dolls (Character, ManufacturerID, PurchasePrice, DateAcquired) VALUES ('Hillary Clinton', 'MagicPlastic', 9.95, #11/23/2008#)
69
Update some or all the records
UPDATE table SET expression1 [, expression2 …] [WHERE selection-criteria]
70
Change Hillary’s name to Chelsea.
UPDATE Dolls SET Character = ‘Chelsea Clinton’ WHERE Character = ‘Hillary Clinton’
71
Delete all records from a table DELETE FROM customer;
Delete all records from a table TRUNCATE TABLE customer;
Delete a single record from a table DELETE FROM customer WHERE cid='5';
72
Delete Chelsea’s record from Dolls.
DELETE FROM Dolls WHERE Character = ‘Chelsea Clinton’