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
Considering the following table named account: user\_id(SERIAL),username(VARCHAR),created\_on(TIMESTAMP) How would you add a row to this table?
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
What's the general syntax to update data in a table?
UPDATE table SET column1 = value1, column2 = value2 WHERE condition;
27
Can you run an UPDATE command without any WHERE condition?
Yes: UPDATE table SET last\_login = CURRENT\_TIMESTAMP this will update all rows
28
Can you update the data in a specific column based on data in a different column?
Yes: UPDATE account SET last\_login = created\_on
29
What is an UPDATE JOIN?
An UPDATE JOIN is when we create the data on a table according to the data inside a different table
30
What does the RETURNING command do inside a query?
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
What does the DELETE clause do?
We can use the DELETE clause to remove rows from a table
32
What's a simple example for a DELETE clause query
DELETE FROM table WHERE row\_id = 1
33
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?
DELETE FROM tableA USING tableB WHERE tableA.id=TableB.id
34
How can you delete all rows from a table?
DELETE FROM table
35
What does the RETURNING clause do when used in conjunction with a DELETE command?
It will return the deleted rows if specified like so: RETURNING id,name,password…
36
What does an ALTER clause do?
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
How can you add columns to a table using the ALTER TABLE clause?
ALTER TABLE table\_name ADD COLUMN new\_col TYPE
38
How can you remove columns using the ALTER TABLE clause?
ALTER TABLE table\_name DROP COLUMN col\_name
39
How can you alter constraints using the ALTER TABLE clause?
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
How can you change a table name using the ALTER TABLE clause?
ALTER TABLE information RENAME TO new\_info
41
How can you change a column name using the ALTER TALBE clause?
ALTER TABLE new\_info RENAME COLUMN person TO people
42
How can you drop a column using the ALTER TABLE clause?
ALTER TABLE table\_name DROP COLUMN col\_name
43
How can you drop a column using the ALTER TABLE clause only if the column actually exists?
ALTER TABLE new\_info DROP COLUMN IF EXISTS people
44
What does the CHECK constraint do?
* 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
What does the CHECK constraint do?
* 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 ) );