首页 / MYSQL / MySQL基础之表的管理
MySQL基础之表的管理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了MySQL基础之表的管理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2817字,纯文字阅读大概需要5分钟。
内容图文
![MySQL基础之表的管理](/upload/InfoBanner/zyjiaocheng/512/da40d6f7b38f474086f013cf308c57ce.jpg)
添加和删除字段操作
添加字段
alter table tbl_name add 字段名称 字段属性 [完整性约束条件] [first|after 字段名称之后];
删除字段
alter table tbl_name drop 字段名称;
测试
create table if not exists user1(
id int unsigned auto_increment key
);
--添加用户名字段 username varchar(20)
alter table user1 add username varchar(20);
--添加密码字段 password varchar(20) 到id字段之后
alter table user1 add password varchar(20) not null default '123456' after id;
--删除密码字段
alter table user1 drop password;
--多次添加或删除用逗号分隔
create table if not exists test(
id int unsigned auto_increment key,
price float(8,2) unsigned not null default 0
);
alter table test
add num int unsigned not null default 100,
add test varchar(50) not null first,
add test1 char(23) not null after price,
drop price;
添加和删除默认值操作
添加默认值
alter table tbl_name alter 字段名称 set default 默认值;
删除默认值
alter table tbl_name alter 字段名称 drop default;
测试
create table user2(
id int unsigned auto_increment key,
username varchar(20) not null,
age tinyint unsigned not null default 18,
email varchar(50) not null
);
--给email字段添加默认值 huowuyan@163.com
alter table user2 alter email set default 'huowuyan@163.com';
--给age删除默认值
alter table user2 alter age drop default;
modify和change关键字的使用
修改字段类型、字段属性
alter table tbl_name modify 字段名称 字段类型 [字段属性] [first|after 字段名称];
修改字段名称、字段类型、字段属性
alter table tbl_name change 原字段名称 新字段名称 字段类型 [字段属性] [first|after 字段名称];
测试
create table user3(
id int unsigned auto_increment key,
username varchar(5) not null unique,
password char(32) not null,
email varchar(10) not null
);
--将用户名字段的类型改为20
alter table user3 modify username varchar(20) not null unique;
--将username 名称改为user
alter table user3 change username user varchar(20) not null;
主键和唯一索引操作
添加主键和删除主键
alter table tbl_name add primary key(字段名称);
alter table tbl_name drop primary key;
添加唯一和删除唯一
alter table tbl_name add unique key|index [index_name] (字段名称);
alter table tbl_name drop index index_name;
--当表中没有主键时,unique就相当于主键
测试
create table user4(
id int unsigned,
username varchar(20) not null,
email varchar(30) not null,
phone varchar(20) not null unique
);
alter table user4 add primary key(id);
alter table user4 drop primary key;
alter table user4 add unique key uni_email (email);
alter table user4 drop index uni_email;
重命名表的操作
alter table tbl_name rename [to|as] new_tbl_name; --第一种方式
rename table tbl_name to new_tbl_name; --第二种方式
MySQL基础之表的管理
标签:sql 没有 类型 alter 添加用户 管理 incr 基础 测试
本文系统来源:https://www.cnblogs.com/huowuyan/p/11442460.html
内容总结
以上是互联网集市为您收集整理的MySQL基础之表的管理全部内容,希望文章能够帮你解决MySQL基础之表的管理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。