[1] Create Tables, Enforcing Integrity Flashcards

1
Q

Give 5 variations of the standard Table.

A
  1. Temp tables: a tables that exist only in tempdb and last as long as a session or scope referencing it.
  2. Table variables: tables that store data only for a batch.
  3. Views: a query on a table or multiple tables.
  4. Indexed views: views that update when the base table is updated.
  5. Derived tables: CTEs. Sub-queries referenced like tables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a database schema and what is it’s primary purpose?

A

A database schema is a namespace that groups tables together.

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

What are the 4 built-in database schema and what do they store?

A
  1. dbo: default schema for saving new objects for owners/admin users.
  2. guest schema: stores info for a guest user
    INFORMATION_SCHEMA: provides ANSI access to metadata.
  3. system database schema: for system objects like tables/views
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the syntax to move a table from one schema to another?

A

ALTER SCHEMA schemaTo TRANSFER schemaCurrent.table

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

What are the two types of identifiers and how do they differ?

A

Regular: names that do not require delimiters
Delimited: names that require [] or “”

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

What does the SET QUOTED_IDENTIFIER setting do?

A

When ON, it allows for quotations to be used for delimited identifiers

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

What are the 4 types of constraints when creating/altering a table?

A
  1. DEFAULT
  2. UNIQUE
  3. PRIMARY KEY
  4. FOREIGN KEY
  5. CHECK
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Syntax a primary key on column “ID”

A

CONSTRAINT PK_tablename PRIMARY KEY(ID)

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

What are the 3 main requirements for creating a primary key out of a column?

A
  1. Column must not allow nulls
  2. Column must have no duplicates
  3. Column is the only primary key in the table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Syntax to check for primary keys on a table

A

SELECT *
from sys.key_constraints
WERE type = ‘PK’

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

What is the difference between a primary key and UNIQUE key columns?

A

A unique key column can take 1 null value. Primary key columns cannot have any nulls

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

Syntax to add uniqueness to column “ID”

A

ALTER TABLE schema.tablename

ADD CONSTRAINT UC_tablename UNIQUE (ID)

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

Syntax to check for unique keys on a table

A

SELECT *
from sys.key_constraints
WERE type = ‘UQ’

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

What is the definition of a foreign key?

A

A (combination of) column(s) in one table that serve as a link to look up data in another (lookup) table.

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

Syntax to add a foreign key to column “ID”

A

ALTER TABLE schema.tablename1
ADD CONSTRAINT FK_tablename FOREIGN KEY (ID)
REFERENCES tablename2 (ID)

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

Syntax to check for adding constraint check on a table

A

ALTER TABLE schema.table WITH CHECK
ADD CONSTRAINT CHK_name
CHECK (col1 < 5)

17
Q

Syntax to check for check constraints on a table

A

SELECT *
FROM sys.check_constraints
WHERE parent_object_id = OBJECT_ID(N’schema.table’, N’U’)

18
Q

What is the purpose of the default constraint?

A

To create an alternative value in a column that does not allow nulls.

19
Q

Syntax to check for default constraints on a table

A

SELECT *
FROM sys.default_constraints
WHERE parent_object_id = OBJECT_ID(N’schema.table’, N’U’)

20
Q

What syntax allows you to move a table from one schema to another?

A

ALTER SCHEMA TRANSFER

21
Q

What is the syntax that allows you to insert a row in an identity column?

A

SET IDENTITY_INSERT tablename ON/OFF