tu sql - siuestornar/courses/notes/cs234/tu-sql.pdf1nf - no duplication 22 inventory sid sloc szip...
TRANSCRIPT
![Page 1: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/1.jpg)
TUSQL
1
![Page 2: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/2.jpg)
Structured Query Language
2
![Page 3: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/3.jpg)
SQL lowdown
• Used by Relational Database Management Systems (RDBMS)
• Used to create and alter a DB
• Used to query a DB
3
![Page 4: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/4.jpg)
RDBMS quick l👀k
• The standard for DBs these days
• Based on relations
• Tables are related via common fields (FK = PK)
4
![Page 5: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/5.jpg)
Relations are modeled with tables
5
![Page 6: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/6.jpg)
Tables a.k.a Entities
6
![Page 7: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/7.jpg)
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
![Page 8: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/8.jpg)
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
![Page 9: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/9.jpg)
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.
![Page 10: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/10.jpg)
10
Course
id
number
title
credits
![Page 11: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/11.jpg)
• Uniquely identifies each row
• Each table should have one
• Use int values when possible
• Could be a composite (two or more fields)
11
![Page 12: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/12.jpg)
Foreign Key = FK
12
![Page 13: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/13.jpg)
Tables are related via FKFK = PK
13
![Page 14: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/14.jpg)
14
Course
id
number
title
credits
Transcript
student$id
course$id
grade
![Page 15: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/15.jpg)
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
![Page 16: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/16.jpg)
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.
![Page 17: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/17.jpg)
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
![Page 18: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/18.jpg)
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.
![Page 19: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/19.jpg)
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.
![Page 20: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/20.jpg)
Normalization benefits
• No data redundancy
• Data access flexibility
• No inconsistent data
• Removes horizontal/vertical duplication
20
![Page 21: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/21.jpg)
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
![Page 22: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/22.jpg)
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
![Page 23: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/23.jpg)
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
![Page 24: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/24.jpg)
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
![Page 25: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/25.jpg)
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.
![Page 26: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/26.jpg)
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.
![Page 27: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/27.jpg)
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.
![Page 28: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/28.jpg)
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.
![Page 29: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/29.jpg)
Recap
• 1NF - don't duplicate across a row
• 2NF - vertical duplication requires splits
• 3NF - functional dependence (PK only), requires splits
29
![Page 30: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/30.jpg)
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
![Page 31: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/31.jpg)
Our Normalization method
31
![Page 32: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/32.jpg)
ERFeD - say what?
• Identify Entities
• Establish Relations
• Add Fields
• Choose Primary Key
• Select Data Types
32
![Page 33: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/33.jpg)
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
![Page 34: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/34.jpg)
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
![Page 35: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/35.jpg)
3 Fields
35
supplies supplied
includes
Supplier Part
name
SupplierPart
qty
SupplierZip
location zipcode
![Page 36: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/36.jpg)
4 Primary Key
36
supplies supplied
includes
Supplier
id supplierzip$id
Part
id name
SupplierPart
supplier$id part$id qty
SupplierZip
id location zipcode
![Page 37: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/37.jpg)
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
![Page 38: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/38.jpg)
DDL
Data Definition Language
SQL's lingua Franca
38
DML
Data ManipulationLanguage
![Page 39: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/39.jpg)
CREATEDB/Table
DDL
39
ALTERDB/Table
DROPDB/Table
![Page 40: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/40.jpg)
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;
![Page 41: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/41.jpg)
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;
![Page 42: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/42.jpg)
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;
![Page 43: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/43.jpg)
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)
![Page 44: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/44.jpg)
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)
![Page 45: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/45.jpg)
45
ALTER TABLE customer ADD createdOn DATE NOT NULL;
customer
idfirst_namelast_namecreatedOn
customer
idfirst_namelast_name
![Page 46: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/46.jpg)
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
![Page 47: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/47.jpg)
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;
![Page 48: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/48.jpg)
SELECTrecords
DML
48
INSERTrecords
DELETErecords
UPDATErecords
![Page 49: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/49.jpg)
49
Select from a single table
SELECT * | field1 [, field2, … fieldn] FROM table [WHERE condition] [ORDER BY field1 [ASC | DESC] [, ...]]
![Page 50: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/50.jpg)
50
List all doll names and date purchased, list most recent first.
SELECT Character, DateAcquired FROM Dolls ORDER BY DateAcquired DESC
![Page 51: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/51.jpg)
51
List all doll names and date purchased, list most recent first.
SELECT Character, DateAcquired FROM Dolls ORDER BY DateAcquired DESC
![Page 52: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/52.jpg)
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
![Page 53: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/53.jpg)
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
![Page 54: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/54.jpg)
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
![Page 55: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/55.jpg)
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*’
![Page 56: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/56.jpg)
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]
![Page 57: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/57.jpg)
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]
![Page 58: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/58.jpg)
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 |+------------+-----------+-------+
![Page 59: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/59.jpg)
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
![Page 60: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/60.jpg)
60
+-----------+-------+-----------+-------+| id | last | course$id | grade |+-----------+-------+-----------+-------+| 111222111 | last1 | 1 | A || 111222111 | last1 | 2 | B || 111333111 | last2 | 3 | C || 111444111 | last3 | 1 | B |+-----------+-------+-----------+-------+
![Page 61: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/61.jpg)
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
![Page 62: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/62.jpg)
62
+--------+-------+--------+-------+| first | last | number | grade |+--------+-------+--------+-------+| first1 | last1 | CS234 | A || first1 | last1 | CS150 | B || first2 | last2 | CS240 | C || first3 | last3 | CS234 | B |+--------+-------+--------+-------+
![Page 63: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/63.jpg)
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
![Page 64: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/64.jpg)
64
+--------+-------+--------+-------+| first | last | number | grade |+--------+-------+--------+-------+| first1 | last1 | CS234 | A || first1 | last1 | CS150 | B || first2 | last2 | CS240 | C || first3 | last3 | CS234 | B |+--------+-------+--------+-------+
![Page 65: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/65.jpg)
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
![Page 66: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/66.jpg)
66
+-----------+---------------+| sid | Courses Taken |+-----------+---------------+| 111222111 | 2 || 111333111 | 1 || 111444111 | 1 |+-----------+---------------+
![Page 67: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/67.jpg)
67
Insert a record into a table
INSERT [INTO] table [(field-list)] VALUES (value-list)[, (value-list)]
![Page 68: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/68.jpg)
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#)
![Page 69: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/69.jpg)
69
Update some or all the records
UPDATE table SET expression1 [, expression2 …] [WHERE selection-criteria]
![Page 70: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/70.jpg)
70
Change Hillary’s name to Chelsea.
UPDATE Dolls SET Character = ‘Chelsea Clinton’ WHERE Character = ‘Hillary Clinton’
![Page 71: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/71.jpg)
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';
![Page 72: TU SQL - SIUEstornar/courses/notes/cs234/tu-sql.pdf1NF - 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](https://reader035.vdocument.in/reader035/viewer/2022071402/60efb51e1b06b26ccc3cce0d/html5/thumbnails/72.jpg)
72
Delete Chelsea’s record from Dolls.
DELETE FROM Dolls WHERE Character = ‘Chelsea Clinton’