MySQL SQL statements modify the field name, field length

1. Modify the length field

grammar:

ALTER TABLE table MODIFY COLUMN Field Name Data Type (length after modification)

example:

The length of the field from 10 to 20

 

ALTER TABLE attence MODIFY COLUMN id INT(20)

2. Modify the name of the field

grammar:

alter table <table name> Change <field name> <field in the new name> <field type>.

example:

The field attence_name changed name

ALTER TABLE attence CHANGE attence_name NAME  VARCHAR(20)

3. New field

grammar:

New defaults to an empty field
ALTER TABLE table name ADD COLUMN field name field type DEFAULT NULL; 
new field is not empty
ALTER TABLE table name ADD COLUMN field name field type NOT NULL;

例子:
ALTER TABLE attence ADD COLUMN attence_name VARCHAR(20) DEFAULT NULL; 

ALTER TABLE attence ADD COLUMN age VARCHAR(20) NOT NULL;

 

4. Delete field

grammar:

ALTER TABLE table DROP COLUMN field name;

example:

ALTER TABLE attence DROP COLUMN age;

 

 

5. Add Batch field

The method
can use the transaction

grammar:

begin; // start transaction
alter table add the table name field name field type (length);
alter table add the table name field name field type (length);
alter table add the table name field name field type (length);
alter table table add field name field type (length);
the commit;    

example: 

begin;                                           //事务开始
alter table em_day_data add f_day_house7 int(11);
alter table em_day_data add f_day_house8 int(11);
alter table em_day_data add f_day_house9 int(11);
alter table em_day_data add f_day_house10 int(11);
commit;     

Method Two

alter table table add (Field Type 1 (length), type 2 field (length), 3 field type (length));

alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));

 

 

6. Bulk edit field names

grammar:

alter table table change before and after the changes to field names modify the field name int (11) not null,
change and modified before the field name modify the field name int (11) not null,
change and modified before the field name modify the field name int (11) not null ,
after the field name change before the amendment modify the field name int (11) not null,
the field name change before the amendment modify the field name int (11) not null

example:

alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
change f_day_house12 f_day_hour12 int(11) not null,
change f_day_house13 f_day_hour13 int(11) not null,
change f_day_house14 f_day_hour14 int(11) not null,
change f_day_house15 f_day_hour15 int(11) not null,
change f_day_house16 f_day_hour16 int(11) not null,
change f_day_house17 f_day_hour17 int(11) not null

grammar:

ALTER TABLE table MODIFY COLUMN Field Name Data Type (length after modification)

example:

The length of the field from 10 to 20

 

ALTER TABLE attence MODIFY COLUMN id INT(20)

2. Modify the name of the field

grammar:

alter table <table name> Change <field name> <field in the new name> <field type>.

example:

The field attence_name changed name

ALTER TABLE attence CHANGE attence_name NAME  VARCHAR(20)

3. New field

grammar:

New defaults to an empty field
ALTER TABLE table name ADD COLUMN field name field type DEFAULT NULL; 
new field is not empty
ALTER TABLE table name ADD COLUMN field name field type NOT NULL;

例子:
ALTER TABLE attence ADD COLUMN attence_name VARCHAR(20) DEFAULT NULL; 

ALTER TABLE attence ADD COLUMN age VARCHAR(20) NOT NULL;

 

4. Delete field

grammar:

ALTER TABLE table DROP COLUMN field name;

example:

ALTER TABLE attence DROP COLUMN age;

 

 

5. Add Batch field

The method
can use the transaction

grammar:

begin; // start transaction
alter table add the table name field name field type (length);
alter table add the table name field name field type (length);
alter table add the table name field name field type (length);
alter table table add field name field type (length);
the commit;    

example: 

begin;                                           //事务开始
alter table em_day_data add f_day_house7 int(11);
alter table em_day_data add f_day_house8 int(11);
alter table em_day_data add f_day_house9 int(11);
alter table em_day_data add f_day_house10 int(11);
commit;     

方法二

alter table 表名 add (字段1 类型(长度),字段2 类型(长度),字段3 类型(长度));

alter table em_day_data add (f_day_house11 int(11),f_day_house12 int(11),f_day_house13 int(11));

 

 

6.批量修改字段名称

语法:

alter table 表 change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null,
change 修改前字段名  修改后字段名称 int(11) not null

例子:

alter table em_day_data change f_day_house11 f_day_hour11 int(11) not null,
change f_day_house12 f_day_hour12 int(11) not null,
change f_day_house13 f_day_hour13 int(11) not null,
change f_day_house14 f_day_hour14 int(11) not null,
change f_day_house15 f_day_hour15 int(11) not null,
change f_day_house16 f_day_hour16 int(11) not null,
change f_day_house17 f_day_hour17 int(11) not null

Guess you like

Origin www.cnblogs.com/YCcc/p/11389401.html