Database and Table Creation Syntax Flashcards
What values can a Boolean datatype hold?
True and false
What datatypes belong to the Character datatype?
char, varchar and text
What numeric datatypes exist?
smallint
integer
bigint
decimal
numeric
real
double precision
smallserial
serial
bigserial
What are temporal datatypes?
data, time, timestamp, and interval
What is the UUID datatype?
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
What array datatypes are available in SQL?
Arrays can be used with many if not all? datatypes:
CREATE TABLE sal_emp (
name text,
pay_by_quarter integer[],
schedule text[][]
);
What is a Primary Key (PK)?
- 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
What does PK stand for?
Primary Key
What is a foreign key (FK)?
- 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
What does FK stand for?
Foreign Key
What is a referenced table or parent table?
A table to which the foreign key references is called referenced table or parent table
Can a table have multiple foreign keys?
Yes, depending on its relationships with other tables
What is a referencing table or child table?
The table that contains the foreign key is called referencing table or child table
What is a child table a child of?
And what does a child table have?
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
What is a constraint?
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.
Are constraints specified to a column or table?
Constraints can be column level or table level. Column-level constraints apply to a column, and table-level constraints apply to the whole table.
What are the most common constraints used in SQL
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 can you create a table without any constraints or inheritance?
CREATE TABLE table_name (
column_name TYPE column_constraint,
column_name TYPE column_constraint,
);
What’s the best datatype for a Primary Key on Postgresql?
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
What’s the most commonly used datatype for Primary Keys in MySQL and SQL server etc?
Integer
- Not all SQL flavors support the serial data type so the AUTO_INCREMENT constraint would need to be specified when creating a table.
How can you add a reference to a primary_key (foreign key (FK)) of a different table while creating a new table?
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.
What are things that you need to keep in mind when inserting row values into a table?
- The inserted row values must match up for the table, including constraints
- SERIAL columns or PRIMARY KEY columns do not need to be provided a value, as they are auto incrementing
What does INSERT INTO do?
INSERT INTO is used for adding a row into a table.
What other command is used in combination with INSERT INTO?
INSERT INTO is used in combination with VALUES to specify values that will be inserted into the row