Creating Tables Flashcards
CREATE TABLE syntax
CREATE TABLE table_name ( column_name1 data_type1, column_name2 data_type2 )
SQL for Data Science Week 1
How do you designate if a column allows nulls or not?
- If you don’t specify, it is assumed nulls are allowed.
- If you want to exclude nulls put NOT NULL after the data_type for the column in the CREATE TABLE statement
- If you want to explicitly include nulls, put NULL after the data_type for the column in the CREATE TABLE statement
SQL for Data Science Week 1
Can a primary key column have a null value in it?
No
SQL for Data Science Week 1
How do you designate a column as the primary key?
Put Primary Key after the data_type for the column in the CREATE TABLE statement
SQL for Data Science Week 1
What happens if you try to insert a null value in a column that does not allow nulls?
You will receive an error
SQL for Data Science Week 1
Baseic INSERT INTO syntax
INSERT INTO table_name VALUES ( column1_value, column2_value, column3_value, column4_value )
SQL for Data Science Week 1
Better INSERT INTO syntax
INSERT INTO table_name ( column1_name, column2_name, column3_name, column4_name ) VALUES ( column1_value, column2_value, column3_value, column4_value )
This is better because it specifies which columns the values are going into and reduces the liklihood values will go into the incorrect columns
SQL for Data Science Week 1
What should you use for a value if you want to insert a null value in a column that allows nulls?
NULL
For example: INSERT INTO table_name VALUES ( column1_value, column2_value, NULL, column4_value )
SQL for Data Science Week 1
How long does a temporary table last?
Only during your current session in the database
SQL for Data Science Week 1
Why use a temporary table?
- They are faster than creating a real table
- Allows you to simplify complex queries
SQL for Data Science Week 1
CREATE TEMPORARY TABLE syntax to get data from an existing table into your temporary table
CREATE TEMPORARY TABLE Sandals AS ( SELECT * FROM shoes WHERE shoe_type='sandals' )
SQL for Data Science Week 1
UPDATE TABLE syntax
UPDATE table_name SET column_1 = new_value_1, column_2 = new_value_2 --Optional WHERE where_condition (ex. employeeid=3)
If you don’t include a WHERE clause, every value in the columns indicated will be updated.
SQL for Data Science Week 1
DELETE TABLE syntax
DELETE FROM table_name --Optional WHERE where_condition
If you omit the WHERE condition, all rows in the table will be deleted
SQL for Data Science Week 1