Database and Table Creation Syntax Flashcards

1
Q

What values can a Boolean datatype hold?

A

True and false

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

What datatypes belong to the Character datatype?

A

char, varchar and text

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

What numeric datatypes exist?

A

smallint

integer

bigint

decimal

numeric

real

double precision

smallserial

serial

bigserial

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

What are temporal datatypes?

A

data, time, timestamp, and interval

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

What is the UUID datatype?

A

UUID stands for Universally unique identifier.

The UUID data type is considered a subtype of the STRING data type, because UUID values are displayed in their canonical textual format and, in general, behave the same as string values in the various SQL operators and expressions.

A UUID generated by the UUID() function would look like this:

UUID_VALUE1UUID_VALUE2UUID_VALUE3e762634c-3e41-11eb-b897-0862660ccbd4e7626367-3e41-11eb-b897-0862660ccbd4e7626368-3e41-11eb-b897-0862660ccbd4

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

What array datatypes are available in SQL?

A

Arrays can be used with many if not all? datatypes:

CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);

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

What is a Primary Key (PK)?

A
  • A primary key is a column or a group of columns used to identify a row uniquely in a table
  • Primary keys are also important since they allow us to easily discern what columns should be used for joining tables together
  • Primary keys are integer based and unique
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does PK stand for?

A

Primary Key

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

What is a foreign key (FK)?

A
  • A foreign key is a field or group of fields in a table that uniquely identifies a row in another table.
  • A foreign key is defined in a table that references the other table’s primary key.
  • The table that contains the foreign key is called referencing table or child table
  • A table to which the foreign key references is called a referenced table or parent table
  • A table can have multiple foreign keys depending on its relationships with other tables
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does FK stand for?

A

Foreign Key

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

What is a referenced table or parent table?

A

A table to which the foreign key references is called referenced table or parent table

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

Can a table have multiple foreign keys?

A

Yes, depending on its relationships with other tables

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

What is a referencing table or child table?

A

The table that contains the foreign key is called referencing table or child table

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

What is a child table a child of?

And what does a child table have?

A

The parent table with the Primary Key

A child table has a foreign key, whereas a parent table has the primary key referencing to the child table

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

What is a constraint?

A

SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted.

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

Are constraints specified to a column or table?

A

Constraints can be column level or table level. Column-level constraints apply to a column, and table-level constraints apply to the whole table.

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

What are the most common constraints used in SQL

A

The following constraints are commonly used in SQL:

  • NOT NULL - Ensures that a column cannot have a NULL value
  • UNIQUE - Ensures that all values in a column are different
  • PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
  • FOREIGN KEY - Prevents actions that would destroy links between tables
  • CHECK - Ensures that the values in a column satisfy a specific condition
  • DEFAULT - Sets a default value for a column if no value is specified
  • CREATE INDEX - Used to create and retrieve data from the database very quickly
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How can you create a table without any constraints or inheritance?

A

CREATE TABLE table_name (
column_name TYPE column_constraint,
column_name TYPE column_constraint,
);

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

What’s the best datatype for a Primary Key on Postgresql?

A

Serial

  • In PostgreSQL a sequence is a special king of database object that generates a sequence of integers.
  • A sequence is often used as the primary key column in a table
20
Q

What’s the most commonly used datatype for Primary Keys in MySQL and SQL server etc?

A

Integer

  • Not all SQL flavors support the serial data type so the AUTO_INCREMENT constraint would need to be specified when creating a table.
21
Q

How can you add a reference to a primary_key (foreign key (FK)) of a different table while creating a new table?

A

CREATE TABLE account_job(
user_id INTEGER REFERENCES account(user_id)
)

notice here that the data type for the user_id is not serial but INTEGER, the data type doesn’t need to be serial because we are only referencing the primary_key of another table.

22
Q

What are things that you need to keep in mind when inserting row values into a table?

A
  1. The inserted row values must match up for the table, including constraints
  2. SERIAL columns or PRIMARY KEY columns do not need to be provided a value, as they are auto incrementing
23
Q

What does INSERT INTO do?

A

INSERT INTO is used for adding a row into a table.

24
Q

What other command is used in combination with INSERT INTO?

A

INSERT INTO is used in combination with VALUES to specify values that will be inserted into the row

25
Q

Considering the following table named account:

user_id(SERIAL),username(VARCHAR),created_on(TIMESTAMP)

How would you add a row to this table?

A

INSERT INTO account(username, created_on)
VALUES (‘Jose’, CURRENT_TIMESTAMP)

Notice:

  • Remember that CURRENT_TIMESTAMP can automatically insert the current Time into the TIMESTAMP column
  • Notice that the user_id doesn’t need to be selected at all, it can be omitted from INSERT INTO as well as VALUES as it will be auto-created and auto-incremented by the database server automatically
26
Q

What’s the general syntax to update data in a table?

A

UPDATE table
SET column1 = value1,
column2 = value2
WHERE
condition;

27
Q

Can you run an UPDATE command without any WHERE condition?

A

Yes:

UPDATE table
SET last_login = CURRENT_TIMESTAMP

this will update all rows

28
Q

Can you update the data in a specific column based on data in a different column?

A

Yes:

UPDATE account
SET last_login = created_on

29
Q

What is an UPDATE JOIN?

A

An UPDATE JOIN is when we create the data on a table according to the data inside a different table

30
Q

What does the RETURNING command do inside a query?

A

The RETURNING clause allows you to retrieve values of columns (and expressions based on columns) that were modified by an insert, delete or update. Without RETURNING, you would have to run a SELECT statement after the DML statement is completed, in order to obtain the values of the changed columns. So, RETURNING helps avoid another roundtrip to the database, another context switch in a PL/SQL block.

The RETURNING clause can return multiple rows of data, in which case you will use the RETURNING BULK COLLECT INTO form.

You can also call aggregate functions in the RETURNING clause to obtain sums, counts, and so on of columns in multiple rows changed by the DML statement.

Finally, you can also use RETURNING with EXECUTE IMMEDIATE (for dynamically constructed and executed SQL statements).

31
Q

What does the DELETE clause do?

A

We can use the DELETE clause to remove rows from a table

32
Q

What’s a simple example for a DELETE clause query

A

DELETE FROM table
WHERE row_id = 1

33
Q

How can you delete rows based on their presence in other tables?

If for example 2 tables are linked to each other using PK and FK how could you access the other table and delete data off of one table according to ids that match?

A

DELETE FROM tableA
USING tableB
WHERE tableA.id=TableB.id

34
Q

How can you delete all rows from a table?

A

DELETE FROM table

35
Q

What does the RETURNING clause do when used in conjunction with a DELETE command?

A

It will return the deleted rows if specified like so:

RETURNING id,name,password…

36
Q

What does an ALTER clause do?

A

An ALTER clause allows for changes to an existing table structure, such as:

  • Adding, dropping, or renaming columns
  • Changing a columns data type
  • Set DEFAULT values for a column
  • Add CHECK constraints
  • Rename table
37
Q

How can you add columns to a table using the ALTER TABLE clause?

A

ALTER TABLE table_name
ADD COLUMN new_col TYPE

38
Q

How can you remove columns using the ALTER TABLE clause?

A

ALTER TABLE table_name
DROP COLUMN col_name

39
Q

How can you alter constraints using the ALTER TABLE clause?

A

ALTER TABLE table_name
ALTER COLUMN col_name

And then you can do multiple things, to name a few:

  • To set a default value constraint: SET DEFAULT value
  • Drop a default constraint: DROP DEFAULT
  • Remove or add NULL constraint: SET NOT NULL or DROP NOT NULL
  • Add general constraint: ADD CONSTRAINT constaint_name
40
Q

How can you change a table name using the ALTER TABLE clause?

A

ALTER TABLE information
RENAME TO new_info

41
Q

How can you change a column name using the ALTER TALBE clause?

A

ALTER TABLE new_info
RENAME COLUMN person TO people

42
Q

How can you drop a column using the ALTER TABLE clause?

A

ALTER TABLE table_name
DROP COLUMN col_name

43
Q

How can you drop a column using the ALTER TABLE clause only if the column actually exists?

A

ALTER TABLE new_info
DROP COLUMN IF EXISTS people

44
Q

What does the CHECK constraint do?

A
  • The CHECK constraint allows us to create more customized constraints that adhere to a certain condition
  • Such as making sure all inserted integer values fall below a certain threshold
44
Q

What does the CHECK constraint do?

A
  • The CHECK constraint allows us to create more customized constraints that adhere to a certain condition
  • Such as making sure all inserted integer values fall below a certain threshold

Example:

CREATE TABLE example(
ex_id SERIAL PRIMARY KEY,
age SMALLINT CHECK ( age > 21 ),
parent_age SMALLINT CHECK ( parent_age > age )
);