SC4x W2L1 Clean
SC4x W2L1 Clean
2
SQL and SQL Data Types
3
Structured Query Language (SQL)
• Data definition: Operations to build tables and views (virtual
tables)
6
Core MySQL Data Types – Numeric
Numeric Type Description
INT A standard integer
BIGINT A large integer
DECIMAL A fixed-point number
FLOAT A single-precision, floating-point number
DOUBLE A double-precision, floating-point number
BIT A bit field
7
Core MySQL Data Types – Strings (Text)
String Type Description
CHAR A fixed-length, non-binary string (character)
VARCHAR A variable-length, non-binary string
NCHAR Same as above + Unicode Support
NVARCHAR Same as above + Unicode Support
BINARY A fixed-length, binary string
VARBINARY A variable-length, binary string
TINYBLOB A very small BLOB (binary large object)
BLOB A small BLOB
TEXT A small, non-binary string
8
Core MySQL Data Types – Dates/Times
Date / Time Type Description
DATE A date value in 'CCYY-MM-DD' format
TIME A time value in 'hh:mm:ss' format
DATETIME Date/Time in 'CCYY-MM-DD hh:mm:ss’ format
TIMESTAMP Timestamp in 'CCYY-MM-DD hh:mm:ss’ format
YEAR A year value in CCYY or YY format
9
Key points from lesson
• SQL (Structured Query Language) contains the commands we
use to create, manage and query relational databases
10
Creating Databases and Tables
11
Example data model
• It is typical to use the same name for columns with the same
meaning across different tables
• We give them different names here so that we don’t have to
say tablename.columnname:
n Here: OfficeNbr and RepOffice
12
Create a database for SC4x
• Recall, database is a collection of tables
• First we must CREATE a database:
13
Create Offices table
CREATE TABLE Offices (
OfficeNbr NCHAR(2) NOT NULL PRIMARY KEY,
City NVARCHAR(20) NOT NULL,
State NCHAR(2) NOT NULL,
Region NCHAR(5) NOT NULL, Offices
Target DECIMAL(10,2) NOT NULL,
OfficeNbr
Sales DECIMAL(10,2) NOT NULL,
Phone NVARCHAR(15) NOT NULL); City
State
Region
Target
Sales
Phone
14
Create SalesReps table
CREATE TABLE SalesReps (
RepNbr NCHAR(3) NOT NULL PRIMARY KEY,
Name NVARCHAR(20) NOT NULL,
RepOffice NCHAR(2) NOT NULL,
Quota DECIMAL(10,2), # Allow NULLs
Sales DECIMAL(10,2) NOT NULL,
FOREIGN KEY (RepOffice) SalesReps
REFERENCES Offices(OfficeNbr));
RepNbr
Name
RepOffice
Quota
Sales
15
Create Customers table
CREATE TABLE Customers (
CustNbr NCHAR(3) NOT NULL PRIMARY KEY,
Company NVARCHAR(30) NOT NULL,
CustRep NCHAR(3) NOT NULL,
CreditLimit DECIMAL(10,2) NOT NULL,
FOREIGN KEY (CustRep)
REFERENCES SalesReps(RepNbr));
Customers
CustNbr
Company
CustRep
CreditLimit
16
Create Orders table
CREATE TABLE Orders (
OrderNbr INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,
Cust NCHAR(3) NOT NULL,
Prod NVARCHAR(20) NOT NULL,
Qty INT NOT NULL, Orders
Amt DECIMAL(10,2) NOT NULL,
Disc DECIMAL(3,1) NOT NULL, OrderNbr
FOREIGN KEY (Cust) Cust
REFERENCES Customers(CustNbr)); Prod
Qty
Amt
Disc
17
Create Employees table
CREATE TABLE Employees(
EmpNbr NCHAR(5) NOT NULL PRIMARY KEY,
Name NVARCHAR(20) NOT NULL,
Title NVARCHAR(20) NOT NULL,
Mgr NCHAR(5));
Employees
EmpNbr
Name
Title
Mgr
18
Create Parts table
CREATE TABLE Parts (
PartID NCHAR(4) NOT NULL PRIMARY KEY,
Vendor NCHAR(4) NOT NULL);
Parts
PartID
Vendor
19
Key points from lesson
• Databases are made using the CREATE DATABASE command
20
Inserting Data into a Database
21
Inserting data into a new database
• The data model acts as a guide to load data into a new
database:
n It may build well, which usually means you found the real
business rules
n It may build with some errors, which usually means you
• It’s often useful to get some sample data and browse it while
building the data model
22
Insert offices into the Offices table
INSERT INTO Offices
Offices
VALUES('1', 'Denver', 'CO', 'West', OfficeNbr
3000000, 130000, '970.586.3341');
City
INSERT INTO Offices State
VALUES('2', 'New York', 'NY', 'East',
200000, 300000, '212.942.5574');
Region
Target
INSERT INTO Offices Sales
VALUES('57', 'Dallas', 'TX', 'West', 0, 0,
'214.781.5342'); Phone
OfficeNbr City State Region Target Sales Phone
1 Denver CO West 3000000.00 130000.00 970.586.3341
2 New York NY East 200000.00 300000.00 212.942.5574
57 Dallas TX West 0.00 0.00 214.781.5342
23
Insert sales representatives into the
SalesReps table
INSERT INTO SalesReps
VALUES('53', 'Bill Smith', '1', 100000, 0);
SalesReps
RepNbr
Name
RepOffice
RepNbr Name RepOffice Quota Sales
Quota
53 Bill Smith 1 100000.00 0.00
89 Jen Jones 2 50000.00 130000.00 Sales
24
Insert customers into the Customers
table
INSERT INTO Customers
VALUES('211', 'Connor Co', '89', 50000);
28
SQL SELECT queries
29
SELECT
• SELECT statements are constructed of clauses to get records
from one or more tables or views.
• Clauses must be in order, only SELECT and FROM are
required:
SELECT attributes/columns
INTO new table
FROM table or view
WHERE specific records or a join is created
GROUP BY grouping conditions (attributes)
HAVING group-property (specific records)
ORDER BY ordering criterion ASC | DESC
30
SELECT attributes FROM SalesReps
• List the sales reps and their current sales and quotas:
SELECT *
FROM Offices;
34
SELECT regions without duplicates
• Select all Region records:
Region
SELECT Region West
FROM Offices; East
West
SELECT COUNT(*)
COUNT(*)
FROM Orders
1
WHERE Cust = '211';
36
SELECT sums and averages FROM
Orders
• Find the sum of all sales:
SELECT SUM(Amt) SUM(Amt)
FROM Orders; 535000.00
38
Wildcards in SQL
• Most database implementations offer additional regular
expressions wildcards
• MySQL has:
[list] match any single character in list, e.g. [a-f]
[^list] match any single character not in list, e.g. [^h-m]
OfficeNbr Phone
2 212.942.5574
41
INSERT INTO Offices
• Add an office:
UPDATE Offices
SET Target = 300000
WHERE OfficeNbr = '55';
45