- SQL is not case sensitive
- End each SQL statement with a semicolon “;”
- Use “*” to select an entire record
- White space doesn’t matter
SELECTstatement is like creating a new table- Field value needs to be in quotes
- Use parentheses to force order of operations
- Use two hyphens “--” to comment
-
CREATE DATABASE db_name- creates a database -
CREATE TABLE table_name (field_name DATA_TYPE CONSTRAINTS)- creates a table in the database -
Data types
1. `DATE`
2. `TIME`
3. `TIMESTAMP`
4. `VARCHAR/CHARACTER`
5. `BOOLEAN`
6. `INTEGER`
7. `DOUBLE PRECISION (FLOAT)`
8. `XML`
- Constraints
1. `PRIMARY KEY`
2. `FOREIGN KEY`
3. `UNIQUE`
4. `DEFAULT`
5. `AUTOINCREMENT`
6. `NOT NULL`
-
INSERT INTO table_name(field1, field2) VALUES ('value1', 'value2')- insert data into a table for one record -
INSERT INTO table_name(field1) VALUES (record1), (record2), (record3)- insert data into a table for multiple records
-
SELECT field1, field2- select fields to return (or*to return entire record) -
FROM table_name- select the table -
INNER JOIN table1, table2- specify which tables to pull data from -
ON table1.field_a = table2.field_b- specify which records to return from the join operation (JOIN+ONcreates a temporary table that includes data from table1 and table2 for only those records where the value in table1.field_a is equal to the value in table2.field_b) -
WHERE table.field_a = "value_x"- only returns records whose value for field_a is equal to value_x (WHEREacts like filter)-
WHERE table.field_a IN ("value_x", "value_y", "value_z")- only return records whose value for field_a is one of values in the parentheses (NOT IN- does the opposite) -
WHERE table.field_a = value_x AND table.field_b = value_y- only returns records where both conditions are met -
... field_a = value_x OR field_b = value_y -
... field_a BETWEEN value_x AND value_y -
... field_a LIKE "spa%"- return only records whose value in field_a starts with "spa" -
<,<=,>,>=,!= -
BETWEEN -
IS- equal to a value (or empty forIS NULL) -
IS NOT- is not equal to a value (or is not empty forIS NOT NULL)
-
-
ORDER BY field_a ASC/DESC- format how results are displayed - arrange records returned by values in field_a according to ascending order (DESCfor descending order)
-
SELECT DISTINCT field_a FROM table- return only distinct values for field_a -
SELECT COUNT(*) FROM table- return the number of records in the table -
SELECT * FROM table LIMIT 5- return only the first 5 records from the table
-
ALTER TABLE table_name RENAME TO new_table_name- rename a table -
ALTER TABLE table_name ADD COLUMN new_field_name DATA_TYPE CONSTRAINTS- add a new field to a table (include data type and field constraints) -
UPDATE table SET field_a = value_x WHERE field_b = value_y- add new data to one or more fields in a table
-
DELETE FROM table WHERE field = value- delete data from a table -
DROP TABLE table_name- delete a whole table