200 likes | 309 Views
Modifying a table structure (mysql). Alter Table Drop, add and modify columns Add, drop, and modify constraints Enable or disable constraints. Recreate Incomplete Test Table. Eexcute these commands: create table hf_test( id int ); drop table hf_test; create table hf_test( id int );.
E N D
Modifying a table structure (mysql) • Alter Table • Drop, add and modify columns • Add, drop, and modify constraints • Enable or disable constraints
Recreate Incomplete Test Table Eexcute these commands: create table hf_test( id int ); drop table hf_test; create table hf_test( id int );
To Add A Column • Provide definition for new column • Syntax: ALTER TABLE tablename ADD (column definition ); • Note: you can add several columns at a time (comma separated)
Add name column Try this: alter table hf_test add ( name varchar(20) );
To Delete a Column • Syntax: ALTER TABLE table_name DROP COLUMN column_name; • Try: ALTER TABLE hf_test DROP COLUMN name;
To Modify Existing Columns Syntax: ALTER TABLE tablenameMODIFY revised column definition; // no parenthesis
Changing Column Size • Increase of column size is OK • To decrease column size: • Data may be truncated if needed ( save data first ) • To change data type of column: • Ok, but data will be converted to new data type loss of information possible
Make name smaller Try this: alter table hf_test modify name varchar(10); Note: data may be truncated but query is executed
Change Data Type ALTER TABLE hf_test MODIFY name date; Note: default values for name (0000-00-00) will replace existing column values
Let’s go back ALTER TABLE hf_test MODIFY name varchar(20); ALTER TABLE hf_test ADD (enrolled date);
Add default Constraint alter table hf_test modify enrolled date default ‘2010-10-10’; To test: insert into hf_test (id, name) values ( 4, ‘Green' ); select * from hf_test;
More Constraint Modifications ALTER TABLE hf_testMODIFY enrolled date NOT NULL; • Null values converted to some default value (for example, empty string if varchar data type) DESCRIBE hf_test;
Removing NOT NULL alter table hf_test modify name date null; describe hf_test;
Adding a Primary Key Constraint Syntax: ALTER TABLE tablename ADD CONSTRAINT table_pk PRIMARY KEY (columnname); • Column must exist • Values should not be null (one null value possibly ok) • Values must be unique
Add Primary Key Try this: ALTER TABLE hf_test ADD CONSTRAINT hf_test_pk PRIMARY KEY ( id ); DESCRIBE hf_test;
Adding a Foreign Key Constraint Syntax: ALTER TABLE tablenameADD CONSTRAINT table_fk FOREIGN KEY ( column ) REFERENCES table2 (column); • Note: All values in current table must exist in parent table
Adding Foreign Key Constraints CREATE TABLE hf_test2 ( id int ); ALTER TABLE hf_test2 ADD CONSTRAINT hf_test2_fk FOREIGN KEY ( id ) REFERENCES hf_test ( id );
Drop Constraints ALTER TABLE tablename DROP PRIMARY KEY; ALTER TABLE hf_test DROP PRIMARY KEY;
Drop Constraints • ALTER TABLE tablename DROP FOREIGN KEY constraint_name; • ALTER TABLE hf_test2 DROP FOREIGN KEY hf_test2_fk;
Rename Tables • RENAME TABLE table1 TO table2; RENAME TABLE hf_test2 TO hf_test3;