现在的位置: 首页 > 数据库 > 正文

mysql的alter操作大全

2019年05月22日 数据库 ⁄ 共 1058字 ⁄ 字号 评论关闭
--给某一张表添加一个列
ALTER TABLE `users` ADD `username`
TEXT 
NOT NULL;
--例如
alter table app_user add starLevel INT(11) NULL default 6;
 
--建表时
给某列添加默认值
create table tablename
(columnname datatype 
default defaultvalue);
 
--已建表修改
alter table tablename alter column columnname set default defaultvalue;
 
--给user表的username添加唯一约束
Alter table user add unique(username);
 
--更改app_activity表中digest的字段,允许为空
ALTER TABLE app_activity MODIFY digest VARCHAR(255) null;
 
--删除某一字段
ALTER TABLE mytable DROP 字段
名;
 
--修改列的类型
alter table 表名称
change 字段名称 字段名称 字段类型 [是否允许非空];
 
--更改表名
rename table 旧表名 to 新表名;
 
//添加utf8编码库,删除一个数据库
CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
DROP database test;
 
//删除一个索引
alter table 表名 drop index 索引列的名字;
 
//查看表的字段信息:
desc 表名;
//查看表的所有信息:
show create table 表名;
//添加主键约束:
alter table 表名 add constraint  primary key (主键字段);
//添加外键约束:
alter table 从表 add constraint  foreign key(外键字段) references (主键字段);
//删除主键约束:
alter table 表名 drop primary key;
//删除外键约束:
alter table 表名 drop foreign key 外键(区分大小写);
 
//删除唯一约束(username该列上有一个唯一约束,app_user为表名)
drop index username on app_user;
【上篇】
【下篇】

抱歉!评论已关闭.