您当前的位置:学无止境 > mysql数据库常用操作网站首页学无止境
mysql数据库常用操作
发布时间:2020-01-14 10:21:33编辑:破冰查看次数:4365
--给某一张表添加一个列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;--更改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;
//添加utf8编码库,
DROP database test;
//删除一个索引alter table 表名 drop index 索引列的名字;
//查看表的字段信息:desc 表名;//查看表的所有信息:
show create table 表名;//查看表的所有约束:
show index from 表名;
//添加主键约束:
alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);
alter table user add primary key(id);
//添加唯一约束:(在建表的时候最后加上unique也可以,例如:username varchar(20) not null unique)
alter table 表名 add unique 索引名 (唯一键字段);
alter table user add unique(username);
//添加外键约束:
alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign ey 从表(外键字段) references 主表(主键字段);//删除主键约束:(如果主键自增,先修改字段信息去掉自增,例如:alter table user modify id int unsigned not null;)
alter table 表名 drop primary key;
alter table user drop primary key
//删除外键约束:
alter table 表名 drop foreign key 外键(区分大小写);
//删除唯一约束(username该列上有一个唯一约束,app_user为表名)drop index username on app_user;关键字词:mysql,约束,索引,默认值,null
评论:
