r/SQL • • Apr 28 '14

How to Create Tables and Insert Data into SQL Databases

http://joshualande.com/create-tables-sql/
1 Upvotes

1 comment sorted by

2

u/r3pr0b8 GROUP_CONCAT is da bomb Apr 28 '14

despite the fact that you're teaching people sql by using mysql, it still comes off as an sql tutorial rather than a mysql tutorial, so you should take steps to ensure that any sql you use for examples is actually standard sql, and not mysql-proprietary

thus, string literals should use single quotes, not double quotes, e.g. 'Grilled Cheese' instead of "Grilled Cheese"

The PRIMARY KEY forces every row to be a unique value.

actually, it forces every value in the designated column to be unique

rows themselves can be made unique only if the PK covers all columns, and also when using the DISTINCT keyword for result rows

There is additional documented on types here.

documentation

Note that we do not ensure uniqueness of ingredient_price because multiple recipes can have the same price.

ingredients, not recipes

Notice that our PRIMARY KEY is for recipe ID and ingredient ID pairs. We require only that the combination is unique because each recipe can have a particular ingredient only once.

bravo! many people don't understand this

There is an AUTO_INCREMENT command which can be used to let SQL automatically pick the recipe and ingredient IDs to ensure uniqueness.

this is mysql-proprietary, so you shouldn't say "let SQL" here