1、修改表名
语法:alter table old_name renname to new_name;
2、修改字段名
语法:alter table table_name
change old_name new_name new_type;
3、修改字段的数据类型
语法:alter table table_name
modify col_name new_type;
示例:alter table games
modify gno varchar(20);
4、添加和删除字段
添加字段语法:
alter table table_name
add new_col_name new_type;
删除字段语法:
alter table table_name
drop col_name;
示例:<添加>
alter table games
add aaa int;
<删除>
alter table games
drop aaa;
5、为列增补约束
(1)添加主键约束
语法:alter table table_name
add constraint con_name
primary key (col_name);
示例:
alter table users
add constraint PK_users_userQQ
primary key (usersQQ);
(2)添加外键约束
语法:alter table table_name
add constraint con_name
foreign key (F_COL) references M_TABLE(M_COL)
示例:
alter table scores
add constraint FK_SCORES_GAMES
foreign key (gno) references games(gno);
6、添加检查约束语法
语法:alter table table_name
add constraint CON_NAME
check (EXP);
示例:
alter table games
add constraint CK_GAMES_GNO
check (gno>0);
7、添加默认值
语法:alter table table_name
alter col_name set defaule value;
示例:
alter table users
alter user_sex set defaule '男';
8、添加自增列
语法:alter table table_name
modify column col_name [类型和是否为空]auto_increment
示例:
alter table games
modify column gno int not null auto_increment
primary key;(还可以加一个,让它变成主键列)
语法:alter table old_name renname to new_name;
2、修改字段名
语法:alter table table_name
change old_name new_name new_type;
3、修改字段的数据类型
语法:alter table table_name
modify col_name new_type;
示例:alter table games
modify gno varchar(20);
4、添加和删除字段
添加字段语法:
alter table table_name
add new_col_name new_type;
删除字段语法:
alter table table_name
drop col_name;
示例:<添加>
alter table games
add aaa int;
<删除>
alter table games
drop aaa;
5、为列增补约束
(1)添加主键约束
语法:alter table table_name
add constraint con_name
primary key (col_name);
示例:
alter table users
add constraint PK_users_userQQ
primary key (usersQQ);
(2)添加外键约束
语法:alter table table_name
add constraint con_name
foreign key (F_COL) references M_TABLE(M_COL)
示例:
alter table scores
add constraint FK_SCORES_GAMES
foreign key (gno) references games(gno);
6、添加检查约束语法
语法:alter table table_name
add constraint CON_NAME
check (EXP);
示例:
alter table games
add constraint CK_GAMES_GNO
check (gno>0);
7、添加默认值
语法:alter table table_name
alter col_name set defaule value;
示例:
alter table users
alter user_sex set defaule '男';
8、添加自增列
语法:alter table table_name
modify column col_name [类型和是否为空]auto_increment
示例:
alter table games
modify column gno int not null auto_increment
primary key;(还可以加一个,让它变成主键列)