|
A. 更改表以添加新列 下例添加一個允許空值的列,而且沒有通過 DEFAULT 定義提供值。各行的新列中的值將為 NULL。 CREATE TABLE doc_exa ( column_a INT) GO ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL GO EXEC sp_help doc_exa GO DROP TABLE doc_exa GO B. 更改表以除去列 下例修改表以刪除一列。 CREATE TABLE doc_exb ( column_a INT, column_b VARCHAR(20) NULL) GO ALTER TABLE doc_exb DROP COLUMN column_b GO EXEC sp_help doc_exb GO DROP TABLE doc_exb GO C. 更改表以添加具有約束的列 下例向表中添加具有 UNIQUE 約束的新列。 CREATE TABLE doc_exc ( column_a INT) GO ALTER TABLE doc_exc ADD column_b VARCHAR(20) NULL CONSTRAINT exb_unique UNIQUE GO EXEC sp_help doc_exc GO DROP TABLE doc_exc GO D. 更改表以添加未驗證的約束 下例向表中的現(xiàn)有列上添加約束。該列中存在一個違反約束的值;因此,利用 WITH NOCHECK 來防止對現(xiàn)有行驗證約束,從而允許該約束的添加。 CREATE TABLE doc_exd ( column_a INT) GO INSERT INTO doc_exd valueS (-1) GO ALTER TABLE doc_exd WITH NOCHECK ADD CONSTRAINT exd_check CHECK (column_a > 1) GO EXEC sp_help doc_exd GO DROP TABLE doc_exd GO E. 更改表以添加多個帶有約束的列 下例向表中添加多個帶有約束的新列。第一個新列具有 IDENTITY 屬性;表中每一行的標識列都將具有遞增的新值。 CREATE TABLE doc_exe ( column_a INT CONSTRAINT column_a_un UNIQUE) GO ALTER TABLE doc_exe ADD /* Add a PRIMARY KEY identity column. */ column_b INT IDENTITY CONSTRAINT column_b_pk PRIMARY KEY, /* Add a column referencing another column in the same table. */ column_c INT NULL CONSTRAINT column_c_fk REFERENCES doc_exe(column_a), /* Add a column with a constraint to enforce that */ /* nonnull data is in a valid phone number format. */ column_d VARCHAR(16) NULL CONSTRAINT column_d_chk CHECK (column_d IS NULL OR column_d LIKE "[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]" OR column_d LIKE "([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]"), /* Add a nonnull column with a default. */ column_e DECIMAL(3,3) CONSTRAINT column_e_default DEFAULT .081 GO EXEC sp_help doc_exe GO DROP TABLE doc_exe GO F. 添加具有默認值的可為空的列 下例添加可為空的、具有 DEFAULT 定義的列,并使用 WITH valueS 為表中的各現(xiàn)有行提供值。如果沒有使用 WITH valueS,那么每一行的新列中都將具有 NULL 值。 ALTER TABLE MyTable ADD AddDate smalldatetime NULL CONSTRAINT AddDateDflt DEFAULT getdate() WITH valueS G. 禁用并重新啟用一個約束 下例禁用用于限制可接受的薪水數(shù)據(jù)的約束。WITH NOCHECK CONSTRAINT 與 ALTER TABLE 一起使用,以禁用該約束并使正常情況下會引起約束違規(guī)的插入操作得以執(zhí)行。WITH CHECK CONSTRAINT 重新啟用該約束。 CREATE TABLE cnst_example (id INT NOT NULL, name VARCHAR(10) NOT NULL, salary MONEY NOT NULL CONSTRAINT salary_cap CHECK (salary < 100000) ) -- Valid inserts INSERT INTO cnst_example valueS (1,"Joe Brown",65000) INSERT INTO cnst_example valueS (2,"Mary Smith",75000) -- This insert violates the constraint. INSERT INTO cnst_example valueS (3,"Pat Jones",105000) -- Disable the constraint and try again. ALTER TABLE cnst_example NOCHECK CONSTRAINT salary_cap INSERT INTO cnst_example valueS (3,"Pat Jones",105000) -- Reenable the constraint and try another insert, will fail. ALTER TABLE cnst_example CHECK CONSTRAINT salary_cap INSERT INTO cnst_example valueS (4,"Eric James",110000) H. 禁用并重新啟用觸發(fā)器 下例使用 ALTER TABLE 的 DISABLE TRIGGER 選項來禁用觸發(fā)器,以使正常情況下會違反觸發(fā)器條件的插入操作得以執(zhí)行。然后下例使用 ENABLE TRIGGER 重新啟用觸發(fā)器。 CREATE TABLE trig_example (id INT, name VARCHAR(10), salary MONEY) go -- Create the trigger. CREATE TRIGGER trig1 ON trig_example FOR INSERT as IF (SELECT COUNT(*) FROM INSERTED WHERE salary > 100000) > 0 BEGIN print "TRIG1 Error: you attempted to insert a salary > $ 100,000" ROLLBACK TRANSACTION END GO -- Attempt an insert that violates the trigger. INSERT INTO trig_example valueS (1,"Pat Smith",100001) GO -- Disable the trigger. ALTER TABLE trig_example DISABLE TRIGGER trig1 GO -- Attempt an insert that would normally violate the trigger INSERT INTO trig_example valueS (2,"Chuck Jones",100001) GO -- Re-enable the trigger. ALTER TABLE trig_example ENABLE TRIGGER trig1 GO -- Attempt an insert that violates the trigger. INSERT INTO trig_example valueS (3,"Mary Booth",100001) GO |
|
|
來自: 素行 > 《數(shù)據(jù)庫》