Previous | Table of Contents | Next |
Today you learned how to create tables and views. Views provide an alternate way of looking at data in a table. Each column in a table requires a name, a data type, and a nullability status, which may be null, not null, or identity. Default constraints, when defined on a column, provide a value when an explicit value is not defined during an INSERT. Identity columns provide a server-maintained, unique, ascending numeric key for easy primary key creation, and a simple way to uniquely identify rows in a table.
You learned how to modify data in a table by using the INSERT, UPDATE, and DELETE commands.
INSERT... SELECT and SELECT... INTO copy rows from one table to another. The first requires the destination table to exist and is logged; the last creates the destination table and is not logged.
TRUNCATE TABLE removes all rows from a table, and resets its identity counter.
You learned how to apply declarative referential integrity through the use of three types of constraints. Primary key constraints require unique values in all rows for the key. Foreign key constraints require that the value exist in the primary key column. Unique constraints require unique values, like PK constraints, but allow inclusion of null columns in the constraint and a single null row.
Q If Ive already created a table, can I add DRI constraints to it without dropping and re-creating it? Can I do this if it has data?
A Yes. You may use the ALTER TABLE command to accomplish this. ALTER TABLE will also allow you to add null columns to a table after the table has been defined. To add a PK constraint to the authors table, use this syntax:
alter table authors add constraint PK_NC_au_id primary key (au_id)
Q Can I insert new identity values explicitly?
A If you own the table, you can set identity_insert on. This allows duplicate identity values, however, so be careful that you do not introduce duplicates. This is helpful when migrating an existing table to a new server, or if you want to move a row in the table to a different identifier value that had been skipped.
Q Can I create a table with just a single identity column, and nothing else?
A Strangely, yes, but you cannot insert data into it unless you set identity_insert on, defeating the purpose of the identity column. This is because you cannot provide an empty column list to insert.
Q Can I create a null identity column?
A No.
The Workshop provides quiz questions to help you solidify your understanding of the material covered, as well as exercises to provide you with experience in using what youve learned. Try to understand the quiz and exercise answers before continuing on to the next days lesson. Answers are provided in the appendix.
create table employer( emp_id numeric(5,0) identity(0,1) emp_address varchar )
Name | Preferred? | |
---|---|---|
OK Office Products | Larry@OK.com | Yes |
German Imports, Inc. | ||
ACME Cartoon Demolition | roadrunner@wb.com | No |
Red Rum | No | |
Elmos Used Cars | redfurry@tickled.net | Yes |
Previous | Table of Contents | Next |