Relational Database and SQL
Note: SQL in the MySQL / MariaDB dialect
- Example tables\, keys\, and relations: Products and Categories
Types of relations:
Many-to-one, or One-to-many
One-to-one
Many-to-many
SQL
- SQL: Structured Query Language
- Standard language to work with databases
- Supported by most relational database management systems (RDBMS) such as MySQL
- DDL: Data Definition Language
- Statements to create\, drop database
- Statements to create\, alter\, drop tables
- DML: Data Manipulation Language
- Statements to _select_ \, _insert_ \, _update_ \, and _delete_ records in database tables
Common MySQL data types
- String/text types:
CHAR
,VARCHAR
- Numerical types:
INT
,DECIMAL
- Date and time:
DATE
,TIME
,DATETIME
insert into products (categoryID, productCode, productName, listPrice)
values (3, 'drm', 'Tama Drum', 259.99);
To retrieve rows (records) with a condition, use WHERE:
select * from products where categoryID = 2;
To retrieve selected columns and rows, in a specified order:
select productName, listPrice
from products
where listPrice < 500
order by listPrice ASC;
Insert one row without column list:
INSERT INTO products
VALUES (DEFAULT, 1, 'tele', 'Fender Telecaster', 'NA',
'949.99', DEFAULT, NOW())
Insert multiple rows:
INSERT INTO categories
(categoryID, categoryName)
VALUES (4, 'Keyboards'),
(5, 'Brass'),
(6, 'Woodwind')
Update multiple columns:
UPDATE products
SET discountPercent = '25.00',
description = 'This guitar has great tone and smooth playability.'
WHERE productName = 'Fender Telecaster'
Delete multiple rows:
DELETE FROM products
WHERE categoryID = 3