Dr. Andrew Besmer
mysql -h deltona.birdnest.org -u my.besmera2 -p
Relational Model | DBMS |
---|---|
Relation | Table |
Tuple | Row |
Attribute | Column |
;
CREATE
commandCREATE DATABASE my_besmera2; -- Catalog
CREATE TABLE Product (
name VARCHAR(255)
); -- Schema
BLOB
and TEXT
are stored separately from the row
TINYINT
, SMALLINT
, MEDIUMINT
, INT
, BIGINT
Type | Bytes | Min | Max |
---|---|---|---|
TINYINT Signed |
1 | -128 | 127 |
TINYINT Unsigned |
0 | 255 | |
SMALLINT Signed |
2 | -32768 | 32767 |
Type | Bytes | Min | Max |
---|---|---|---|
SMALLINT Unsigned |
0 | 65535 | |
MEDIUMINT Signed |
3 | -8388608 | 8388607 |
MEDIUMINT Unsigned |
0 | 16777215 | |
INT Signed |
4 | -2147483648 | 2147483647 |
Type | Bytes | Min | Max |
---|---|---|---|
INT Unsigned |
0 | 4294967295 | |
BIGINT Signed |
8 | -9223372036 854775808 | 9223372036 854775807 |
BIGINT Unsigned |
0 | 1844674407 3709551615 |
SMALLINT(4)
BOOLEAN
is really TINYINT(1)
FLOAT
, DOUBLE
FLOAT
uses 4 bytes, DOUBLE
uses 8 bytesINSERT INTO DecimalTest VALUES (.1),(.7);
SELECT floor(sum(val)*10) FROM DecimalTest;
DECIMAL
when you want exact precision
M
) and scale (D
)
M
- Max digitsD
- DecimalsDECIMAL(M,D)
where M
in {1,2,3,...,254}
& D
in {0,1,2,...30}
D
must never exceed M
DECIMAL
is the same DECIMAL(10,0)
DOUBLE
against DECIMAL
Digits | Bytes |
---|---|
0 | 0 |
1–2 | 1 |
3–4 | 2 |
5–6 | 3 |
7–9 | 4 |
DATE
- 3 bytes
DATETIME
- 8 bytes
TIMESTAMP
- 4 bytes
CHAR(M)
M
is number of characters
M
in {0,1,2,...,255}
VARCHAR(M)
M
in {0,1,2,...,65535}
M
in {0,1,2,...,21844}
BINARY(M)
M
is number of bytesM
in {0,1,2,...,255}
VARBINARY(M)
M
in {0,1,2,...,65535}
BLOB | TEXT | Length Bytes | Max Size? |
---|---|---|---|
TINYBLOB |
TINYTEXT |
1 | ? |
MEDIUMBLOB |
MEDIUMTEXT |
3 | ? |
LONGBLOB |
LONGTEXT |
4 | ? |
BLOB(M) |
TEXT(M) |
Min Needed | ? |
MEDIUMBLOB NOT NULL
how big of a VARCHAR(M) NOT NULL
could we store?
VARCHAR(M) NULL
?CREATE DOMAIN SSN_TYPE AS CHAR(9);
FOREIGN KEY
, PRIMARY KEY
, UNIQUE KEY
NOT NULL
CHAR(9)
DEFAULT <value>
CHECK
clause
CHECK
is not supported in MySQLCHECK (Dnumber > 0 AND Dnumber < 21)
PRIMARY KEY
clause
NOT NULL
UNIQUE
clause
NOT NULL
Dnumber INT PRIMARY KEY -- as part of attribute options
Dname VARCHAR(15) UNIQUE -- as part of attribute options
PRIMARY KEY (Dnumber) -- as part of table options
UNIQUE KEY (Dname) -- as part of table options
FOREIGN KEY
clause
REFERENCES
ON DELETE
or ON UPDATE
SET NULL
, CASCADE
, RESTRICT
and SET DEFAULT
FOREIGN KEY (super_ssn) REFERENCES Employee(ssn) -- as part of table options
CREATE TABLE tbl_name
( col_name1 TYPE [options],
col_name2 TYPE [options],
...
col_nameN TYPE [options],
PRIMARY KEY(col_Name),
FOREIGN KEY (col2_name) REFERENCES tbl2_name(other_col_name)
); [table_options] -- Note FK can reference same table too!
CREATE TABLE EMPLOYEE
(
Fname VARCHAR(15) NOT NULL,
Minit CHAR(1),
Lname VARCHAR(15) NOT NULL,
Ssn CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(30),
Sex CHAR(1),
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT NOT NULL,
PRIMARY KEY (Ssn),
FOREIGN KEY (Super_ssn) REFERENCES EMPLOYEE(Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT(Dnumber)
);
CREATE TABLE DEPARTMENT
(
Dname VARCHAR(15) NOT NULL,
Dnumber INT NOT NULL,
Mgr_ssn CHAR(9) NOT NULL,
Mgr_start_date DATE,
PRIMARY KEY (Dnumber),
UNIQUE (Dname),
FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn)
);
ALTER TABLE tbl_name ADD FOREIGN KEY (col_name) REFERENCES other_tbl(other_col);
START TRANSACTION;
CREATE ...
CREATE ...
COMMIT;
EMPLOYEE.Dno
and DEPARTMENT.Mgr_ssn
Deviation from SQL standards: … InnoDB checks foreign key constraints immediately; the check is not deferred to transaction commit. According to the SQL standard, the default behavior should be deferred checking. That is, constraints are only checked after the entire SQL statement has been processed. Until InnoDB implements deferred constraint checking, some things will be impossible, such as deleting a record that refers to itself using a foreign key.
INSERT
statement
VALUES
only syntax you MUST specify a value for every column, in order
VALUES
INSERT INTO table_name VALUES (value1,value2,value3,...);
-- OR
INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);
Product
table with:
name
quantity
price
CREATE TABLE Product (
name VARCHAR(200) PRIMARY KEY,
quantity SMALLINT UNSIGNED NOT NULL DEFAULT 0,
price DECIMAL(6,2) NOT NULL
);
INSERT INTO Product VALUES ("M&M's", 1000, .99);
INSERT INTO Product VALUES ("Mike&Ike", 500, 1.10), ("Diet Coke", 50, 2.00);
INSERT INTO Product (name, price) VALUES ("Popcorn", 2.99);
INSERT INTO Product (price, name) VALUES (99.99, "TI 93 Calculator"), (20.00, "T-Shirt");
INSERT INTO Product VALUES ("Frozen Pizza", 20, 7.99), ("Mini Toolkit", 20, 15.00), ("Flashlight" , 75 , 5.99 );
SELECT * FROM Product;
SELECT
SELECT <projection attributes> FROM <tables> WHERE <selection conditions> ORDER BY <attribute list>;
*
indicates all.
=
- note the lack of a double equals<
, <=
, >
, >=
, and <>
AND
, OR
, NOT
NOT
laterLIKE
comparison operator
%
replaces an arbitrary number of zero or more characters_
replaces a single character+
, subtraction –
, multiplication *
, and division /
WHERE DNumber BETWEEN 1 AND 2
ORDER BY
clause
ASC
for ascending or DESC
for descendingSELECT name FROM Product WHERE price > 2 ORDER BY price DESC, name ASC;
DISTINCT
with a SELECT
SELECT salary FROM EMPLOYEE;
SELECT DISTINCT salary FROM EMPLOYEE;
CREATE VIEW ProductValues AS SELECT qty*price AS value FROM Product;
SELECT name FROM Product WHERE price > 2 ORDER BY price;
UPDATE
SyntaxUPDATE table SET col1=val1, col2=val2 WHERE col3=val3;
DELETE
SyntaxDELETE FROM table WHERE col1=val1;
JOIN
themCROSS JOIN
- Cartesian productINNER JOIN
or JOIN
NATURAL JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
1JOIN
PDFEMPLOYEE.Dnumber
SELECT Fname, EMPLOYEE.Name, Address FROM EMPLOYEE JOIN DEPARTMENT ON DEPARTMENT.Dnumber = EMPLOYEE.Dnumber WHERE DEPARTMENT.Name = 'Research' ;
-- or
-- NOT ADVISED TO DO:
SELECT Fname, EMPLOYEE.Name, Address FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT.Name = 'Research' AND DEPARTMENT.Dnumber = EMPLOYEE.Dnumber;
EMPLOYEE AS E(Fn, Mi, Ln, Ssn, Bd, Addr, Sex, Sal, Sssn, Dno)
-- Now can use Select E.Fn, E.Ln .....
-- MySQL Ex: Select C.id AS i, C.Name AS n FROM Company AS C;
CREATE TABLE
CREATE TABLE
-- DROP Existing
DROP VIEW ProductValues;
DROP TABLE Product, Company, Category;
-- Create Product Table
CREATE TABLE Product (
id SMALLINT UNSIGNED NOT NULL auto_increment,
name VARCHAR(50) NOT NULL,
qty SMALLINT NOT NULL,
price DECIMAL(7,2) NOT NULL,
company SMALLINT UNSIGNED default NULL,
category SMALLINT UNSIGNED default NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB ;
-- Create Category Table
CREATE TABLE Category (
id SMALLINT UNSIGNED NOT NULL auto_increment,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB ;
-- Add Category Foreign Key
ALTER TABLE Product ADD FOREIGN KEY (category) REFERENCES Category(id);
-- Create Company Table
CREATE TABLE Company (
id SMALLINT UNSIGNED NOT NULL auto_increment,
name VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB ;
-- Add Company Foreign Key
ALTER TABLE Product ADD FOREIGN KEY (Company) REFERENCES Company(id);
-- Fill Category Table
INSERT INTO Category (name) VALUES ("Toys"), ("Tools"), ("Health");
-- Fill Company Table
Insert INTO Company (name) VALUES ("LEGO"), ("Craftsman"), ("Stanley"), ("Crest");
-- Fill Product Table
Insert INTO Product (name, category, company, qty, price) Select "Batman Lego Set", Category.id, Company.id, 1, 9.99 from Category, Company WHERE Category.name = "Toys" AND Company.name = "LEGO";
Insert INTO Product (name, category, company, qty, price) Select "32 Piece Ratchet Set", Category.id, Company.id, 2, 29.99 from Category, Company WHERE Category.name = "Tools" AND Company.name = "Stanley";
Insert INTO Product (name, category, company, qty, price) Select "Hammer", Category.id, Company.id, 5, 5.99 from Category, Company WHERE Category.name = "Tools" AND Company.name = "Stanley";
Insert INTO Product (name, category, company, qty, price) Select "Toothpaste", Category.id, Company.id, 1, 2.99 from Category, Company WHERE Category.name = "Health" AND Company.name = "Crest";
Insert INTO Product (name, category, company, qty, price) Select "Floss", Category.id, Company.id, 3, .99 from Category, Company WHERE Category.name = "Health" AND Company.name = "Crest";
Insert INTO Product (name, category, company, qty, price) Select "Power Drill", Category.id, NULL, 5, 49.99 from Category WHERE Category.name = "Tools";
Insert INTO Product (name, category, company, qty, price) Select "120 Piece Lego Set", NULL, Company.id, 1, 12.99 from Company WHERE Company.name = "LEGO";
Insert INTO Product (name, category, company, qty, price) Select "Hot-wheels Car", NULL, NULL, 10, .99 ;
DROP TABLE Follow, Tweet, User;
CREATE TABLE User (
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL
);
CREATE TABLE Tweet (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
text VARCHAR(140) NOT NULL,
user INT UNSIGNED NOT NULL,
createdAt TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user) REFERENCES User(id) ON UPDATE CASCADE ON DELETE CASCADE
);
CREATE TABLE Follow (
follower INT UNSIGNED,
followee INT UNSIGNED,
PRIMARY KEY(follower, followee),
FOREIGN KEY (follower) REFERENCES User(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (followee) REFERENCES User(id) ON UPDATE CASCADE ON DELETE CASCADE
);
INSERT INTO User (name) VALUES ("KATY PERRY"),("Justin Bieber"),("Elon Musk"),("Taylor Swift"),("YouTube"),("Rihanna"),("Lady Gaga"),("Justin Timberlake"),("Ariana Grande");
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "If people want a role model, they can have Miley Cyrus!", id FROM User where name = "KATY PERRY";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "My personality is up and down, sassy and cheeky.", id FROM User where name = "KATY PERRY";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "Canada's the best country in the world.", id FROM User where name = "Justin Bieber";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "I want to give people a taste of the Caribbean, and show them the fun side of me.", id FROM User where name = "Rihanna";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "Love is like a brick. You can build a house, or you can sink a dead body.", id FROM User where name = "Lady Gaga";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "They can't scare me, if I scare them first.", id FROM User where name = "Lady Gaga";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "Tesla stock price is too high imo", id FROM User where name = "Elon Musk";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "I am selling almost all physical possessions. Will own no house.", id FROM User where name = "Elon Musk";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "Just be yourself, there is no one better.", id FROM User where name = "Taylor Swift";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "No matter what happens in life, be good to people. Being good to people is a wonderful legacy to leave behind.", id FROM User where name = "Taylor Swift";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "Want your video posted, send us a message!", id FROM User where name = "YouTube";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "I never know what day it is. Never, ever, ever.", id FROM User where name = "Rihanna";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "I like simple things. I like to sneak in the theatre and watch movies. I'm a movie buff.", id FROM User where name = "Justin Timberlake";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "Hello YouTubes!", id FROM User where name = "YouTube";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "I try to read all of my fan mail. A lot of them send me candy, which I'm not allowed to eat 'cause my mom says it might be poisonous.", id FROM User where name = "Justin Bieber";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "I have 20,000 girlfriends, all around the world.", id FROM User where name = "Justin Timberlake";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "had the weirdest, scariest, most bizarre dreams ever last night. wow. One of them was me trapped in a room with Kevin Jonas and an alien.", id FROM User where name = "Ariana Grande";
SELECT SLEEP(1);
INSERT INTO Tweet (text, user) SELECT "who ariane", id FROM User where name = "Ariana Grande";
Insert data into Follow
for who Elon should be following. Remember he is incredibly busy so don’t add everyone. Also recall that the twitter relationship is not reciprocal.
Write the SQL statement that would be necessary to display the twitter feed for Elon Musk. Recall that order is not guaranteed.
Add a Tweet
for someone who you had Elon Musk follow and someone who he did not follow.
Rerun your SQL statement to show the twitter feed. How many new Tweets did you see?