SQL Module #58 Flashcards

1
Q

What does SQL mean?

A

Structured Query Language

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

What command is used in SQL in order to create a table?

A

CREATE TABLE

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

What other information is needed when creating a table?

A

Table names and the data types that they will hold.

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

What are some of the most common data types you’ll see in an SQL table?

A
• CHAR
• TEXT
• VARCHAR
• DATE
• TIME
• DATETIME
• TIMESTAMP
---------------------------------------------------
Numeric Types include:
• TINYINT 1 byte (0-255)
• INT 4 bytes
• BIGINT 8 bytes
• SMALLINT 2 bytes
• DECIMAL
• FLOAT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the syntax look like for a table with 2 columns (1 integer, 1 variable string lenght)?

A

CREATE TABLE people (
age INT,
name CHAR(20)
);

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

Once the table is created, how are values added to it?

A

INSERT INTO people VALUES (37, ‘Flavio’)

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

How are multiple values specified?

A

By separating the values with a comma. Example:

37, ‘Flavio’ ), (8, ‘Roger’

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

How is data added into a table?

A

Use the INSERT INTO command, example:

INSERT INTO people VALUES ( 37, ‘Flavio’ ), (8, ‘Roger’)

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

How can you query data from SQL?

A

By using the SELECT command.

You can narrow the query by selecting a single column:

SELECT name FROM people; OR
SELECT COUNT(*) from people;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are rows in a table filtered in SQL?

A

By adding the WHERE clause:

SELECT age FROM people WHERE name=’Flavio’;

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

When data is returned from a query, how can we adjust the order of the data? For example descending to acending?

A

By using the ORDER BY clause:

SELECT * FROM people ORDER BY name DESC;

or order by name:

SELECT * FROM people ORDER BY name;

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

Are empty values (empty cells) acceptable in SQL?

A

Yes, and they are represented as NULL, example:

INSERT INTO people VALUES (null, null); in this instance both columns have an empty cell or NULL value.

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

If a an entire row has a value of null, that can be problematic. How can we solve for that?

A

Declare constraints on the table preventing null values. Essentially write a rule, that forbids it. Example: Use NOT NULL

CREATE TABLE people (
age INT NOT NULL,
name CHAR(20) NOT NULL
);

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

Sometimes data contains a lot of repeated values. Say we didn’t want that to happen, what is the solution?

A

By using the UNIQUE key constraint. Example:

CREATE TABLE people (
age INT NOT NULL,
name CHAR(20) NOT NULL UNIQUE
);

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

What would happen if you used the UNIQUE key constraint and then tried to add duplicate values to the table?

A

You’d get an error:

ERROR: duplicate key value violates unique constraint “people_name_key”
DETAIL: Key (name)=(Flavio) already exists.

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

Say you wanted to find a specific row. How are rows identified in a table?

A

By identifying the primary key for that row. Primary keys are unique ID’s assigned to rows automatically. For example, the email address for a list of users. Whatever the value is, that’s how to reference rows.

17
Q

How is data updated in an SQL table?

A

Buy invoking the UPDATE command. Here’s an example:

UPDATE people SET age=2 WHERE name=’Roger’

18
Q

When updating values in a table/DB how is the location of the update specified?

A

By using the WHERE clause. Example:

UPDATE people SET age=2 WHERE name=’Roger’

19
Q

What if the WHERE clause isn’t specified in your update?

A

ALL tables get the update. So be careful.

20
Q

What if it was necessary to alter the table structure? For example, adding a column.

A

Use the ALTER TABLE command followed by the alteration that you want to make?

ALTER TABLE people ADD COLUMN born_year INT;

21
Q

How can we remove a column from the table?

A

Again using the ALTER TABLE command but with DROP COLUMN.

ALTER TABLE people DROP COLUMN born_yea

22
Q

What is the syntax for deleting data?

A

Use the DELETE FROM command, in order to delete all of the rows from a table.

But, you can use the WHERE clause to specify the data that you want to remove. Example:

DELETE FROM people WHERE name=’Flavio’;

23
Q

How are entire tables deleted?

A

By using the DROP TABLE command. Example:

DROP TABLE people;

24
Q

What is the concept behind “Joins” ?

A

Thay allow for adding a relationship between data sets. The way that’s achieved is through relational algebra. And the syntax looks like this:

SELECT age FROM people JOIN cars ON people.name = cars.owner WHERE cars.model=’Mustang’;