Data Integrity Enforcement, Constraints and Indexes Flashcards
Should you use rules or declarative constraints to enforce data integrity?
- Declarative constraints
- Rules are not as well suited for enforcing data integrity and will be deprecated in a future version of SQL Server
SQL Server 70-461 08-02
How are table constraints like views, tables, stored procedures, etc?
They are also considered database objects in SQL Server
SQL Server 70-461 08-02
What are the 5 different types of constraints?
- Primary key constraint
- Unique constraint
- Foreign key constraint
- Check constraint
- Default constraint
SQL Server 70-461 08-02
What does a UNIQUE CONSTRAINT allow you to do?
Enforce uniqueness of values in a column
SQL Server 70-461 08-02
What is automatically created when you create a UNIQUE CONSTRAINT?
- A unique index with the same name as constraint.
- The unique index is used to enforce the uniqueness of the column or combination of columns
SQL Server 70-461 08-02
Can you have NULLS in a column you want to have a unique constraint?
Yes, but only one of the rows can be null
SQL Server 70-461 08-02
Can PK constraints and Unique constraints be created on computed columns?
Yes
SQL Server 70-461 08-02
What is the max # of columns that can be the key columns of an index?
16
SQL Server 70-461 08-02
What is the max size in bytes that the key column(s) of an index can have?
900 bytes
SQL Server 70-461 08-02
How does a check constraint work?
- You create an expression similar to a filter expression in a WHERE clause so SQL Server knows how to identify valid values and only let them in.
- You can reference other columns in the same row and use built in functions.
SQL Server 70-461 08-02
What do you need to take into account when making a CHECK constraint column that allows nulls?
Make sure that the CHECK CONSTRAINT expression you create will allow nulls to get through
SQL Server 70-461 08-02
Can you customize an error message for if the CHECK CONSTRAINT fails?
No. You would have to use something like a trigger to do that
SQL Server 70-461 08-02
Can you reference the previous value of a column in the CHECK constraint expression?
No. You would need to use a trigger for something like that.
Example: Unit price cannot be increased bby more than 25%
SQL Server 70-461 08-02
What does a DEFAULT CONSTRAINT do?
Say a record is being inserted. For the insertion, column1 is null. DEFAULT CONSTRAINT basically says if the value is NULL instead assign this other value.
SQL Server 70-461 08-02
What happens if you don’t supply a name for a DEFAULT CONSTRAINT?
SQL Server will give it a machine generated name.
SQL Server 70-461 08-02
Are all types of constraints objects?
- Yes. Therefore their names must be unique across the entire database.
- No two tables can have default constraints names the same thing
SQL Server 70-461 08-02
What is the best way to enforce data integrity?
Create or declare constraints on base tables
SQL Server 70-461 08-02
How are constraints applied to a table?
Use the CREATE TABLE or ALTER TABLE statements
SQL Server 70-461 08-02
What is declarative data integrity?
When you embed methods of data integrity inside the definition of the table itself.
SQL Server 70-461 08-02
How should you name a constraint?
- Since a constraint is an object it must have a unique name when compared to other db objects.
- Since it is an object but its scope is an individual table, you may want to use a naming convention that indicates type of constraint, table it applies to and key columns, if necessary.
SQL Server 70-461 08-02
What is the standard form of the statement to add a PK?
CONSTRAINT pk.categories PRIMARY KEY(categoryid)
pk.categories is the name you supply for PK constraints object
categorid is the column name that the PK is on
SQL Server 70-461 08-02
What is the standard form of the statement to add a PK to an existing table?
ALTER TABLE Production.Categories
ADD Constraint PK_Categories
PRIMARY KEY (categoryid)
SQL Server 70-461 08-02
What conditions must be met to create a PK on a column?
- The column cannot allow nulls
- If you are creating a PK on a column(s) in an already existing table, the values in that column must be unique
- There can only be one PK constraint on a table
If any of the conditions are not met, an attempt to create a PK will fail
SQL Server 70-461 08-02
What is automatically created when you create a PK?
A unique index with the same name as the PK constraint
SQL Server 70-461 08-02