ADD CONSTRAINT RangbuocUnique UNIQUE(cot1, cot2.);
To add CHECK constraints on a table we use the following syntax:
ALTER TABLE ten_bang
ADD CONSTRAINT RangbuocUnique CHECK (DIEUKIEN);
To add constraints PRIMARY KEY on a table we use the following syntax:
ALTER TABLE ten_bang
ADD CONSTRAINT Khoachinh PRIMARY KEY (cot1, cot2.);
ALTER TABLE ten_bang
DROP CONSTRAINT RangbuocUnique;
If you are using MySQL, the code is:
ALTER TABLE ten_bang
DROP INDEX RangbuocUnique;
To delete the binding PRIMARY KEY on a table we use the following syntax:
ALTER TABLE ten_bang
DROP CONSTRAINT Khoachinh;
If you are using MySQL, the code is:
ALTER TABLE ten_bang
DROP PRIMARY KEY;
Suppose the NHANVIEN table has the following records:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
Now we add GIOITINH column to NHANVIEN table .
ALTER TABLE NHANVIEN ADD GIOITINH char(1);
Now, the NHANVIEN table has been changed and the following is the result of the SELECT command:
+----+----------+-----+-----------+----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | GIOITINH | +----+----------+-----+-----------+----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | NULL | | 2 | Loan | 25 | Hanoi | 1500.00 | NULL | | 3 | Nga | 23 | Hanam | 2000.00 | NULL | | 4 | Manh | 25 | Hue | 6500.00 | NULL | | 5 | Huy | 27 | Hatinh | 8500.00 | NULL | | 6 | Cao | 22 | HCM | 4500.00 | NULL | | 7 | Lam | 24 | Hanoi | 10000.00 | NULL | +----+----------+-----+-----------+----------+----------+
Next, when you want to remove the GIOITINH column from the table, we use the following SQL statement:
ALTER TABLE NHANVIEN DROP GIOITINH;
Now, the NHANVIEN table has been changed and the following is the result of the SELECT command:
+----+----------+-----+-----------+----------+ | ID | TEN |TUOI | DIACHI | LUONG | +----+----------+-----+-----------+----------+ | 1 | Thanh | 32 | Haiphong | 2000.00 | | 2 | Loan | 25 | Hanoi | 1500.00 | | 3 | Nga | 23 | Hanam | 2000.00 | | 4 | Manh | 25 | Hue | 6500.00 | | 5 | Huy | 27 | Hatinh | 8500.00 | | 6 | Cao | 22 | HCM | 4500.00 | | 7 | Lam | 24 | Hanoi | 10000.00 | +----+----------+-----+-----------+----------+
In the next section, we will take a look at the TRUNCATE TABLE command in SQL, so keep in mind.
Previous article: Index (INDEX) in SQL
Next lesson: TRUNCATE TABLE statement in SQL