Creating Tables Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

CREATE TABLE syntax

A
CREATE TABLE table_name
(
column_name1 data_type1,
column_name2 data_type2
)

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you designate if a column allows nulls or not?

A
  1. If you don’t specify, it is assumed nulls are allowed.
  2. If you want to exclude nulls put NOT NULL after the data_type for the column in the CREATE TABLE statement
  3. 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Can a primary key column have a null value in it?

A

No

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you designate a column as the primary key?

A

Put Primary Key after the data_type for the column in the CREATE TABLE statement

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens if you try to insert a null value in a column that does not allow nulls?

A

You will receive an error

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Baseic INSERT INTO syntax

A
INSERT INTO table_name
VALUES
(
column1_value,
column2_value,
column3_value,
column4_value
)

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Better INSERT INTO syntax

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What should you use for a value if you want to insert a null value in a column that allows nulls?

A

NULL

For example:
INSERT INTO table_name
VALUES
(
column1_value,
column2_value,
NULL,
column4_value
)

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How long does a temporary table last?

A

Only during your current session in the database

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why use a temporary table?

A
  1. They are faster than creating a real table
  2. Allows you to simplify complex queries

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CREATE TEMPORARY TABLE syntax to get data from an existing table into your temporary table

A
CREATE TEMPORARY TABLE Sandals AS
(
SELECT *
FROM shoes
WHERE shoe_type='sandals'
)

SQL for Data Science Week 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

UPDATE TABLE syntax

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

DELETE TABLE syntax

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly