ALTER TABLE table_name ADD [COLUMN] attr_name attr_type ; |
CREATE TABLE test1 ( ssn CHAR(9) NOT NULL, dno INTEGER );
ALTER TABLE test1 ADD salary DEC(8,2); SELECT * FROM test1;
|
|
|
|
Reason:
|
|
|
SQL syntax:
(Official SQL syntax)
ALTER TABLE table_name DROP [COLUMN] attr_name RESTRICTED ;
|
The attribute attr_name is dropped from relation table_name only if the attribute attr_name is NOT a part of a foreign key in some other relation
|
|
SQL syntax:
(Official SQL syntax)
ALTER TABLE table_name DROP [COLUMN] attr_name CASCADE ;
|
The attribute attr_name is dropped from relation table_name AND all attributes that references the deleted attribute in a foreign key constraint in other relations
|
|
[CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) [ON DELETE reference_option] [ON UPDATE reference_option] reference_option: RESTRICT | CASCADE | SET NULL | NO ACTION |
Example: cascaded drop in MySQL
CREATE TABLE parent ( name char(10),
CONSTRAINT C1 PRIMARY KEY (name)
) ;
CREATE TABLE child ( name CHAR(10),
p_name CHAR(10),
CONSTRAINT C2 FOREIGN KEY (p_name) REFERENCES parent(name)
ON DELETE CASCADE
) ;
insert into parent values ('John');
insert into parent values ('Mary');
insert into child values ('John Jr', 'John');
insert into child values ('Jake', 'John');
insert into child values ('Jack', 'John');
insert into child values ('Mona', 'Mary');
insert into child values ('Molly', 'Mary');
delete from parent where name='John'; // Will delete all children from John too !!
|