mysql数据表的基本操作之表结构操作,字段操作实例分析
数据库  /  管理员 发布于 6年前   320
本文实例讲述了mysql数据表的基本操作之表结构操作,字段操作。分享给大家供大家参考,具体如下:
首发时间:2018-02-18 21:31
create table [if not exists] 表名(字段名字 数据类型,字段名字 数据类型)[表选项];
-- 建表之前必须指定数据库,可以使用use来指定后续的操作是基于哪个数据库的 ,也可以使用数据库名作为前缀来指定数据表创建在哪个数据库。
-- 使用数据库名作为前缀来指定数据表创建在哪个数据库。create table if not exists mydatabase.student(name varchar(20),sex varchar(20),number varchar(20),age int)charset utf8;
-- 使用use来指定后续操作基于哪个数据库use mydatabase;create table if not exists class(name varchar(20),room varchar(20))charset utf8;-- 演示不带表选项的创建表use mydatabase;create table if not exists class(name varchar(20),room varchar(20));
查看数据表可以查看已有数据表、数据表的字段信息
-- 查看所有表show tables;-- 查看部分表show tables like '模糊匹配';-- 查看表的创建语句show create table 数据表名;-- 旋转查看结构show create table 数据表名\G;-- 查看表结构:查看表中的字段信息:Desc/desc 表名;describe 表名;show columns from 表名;
show tables;
show tables like 'my%';
show create table student;
show create table student\G;
desc student;describe student;show columns from student;
图例:
Desc/describe /show columns from 表名;
修改表只能修改表名和表选项。
-- 修改表名:rename table 老表名 to 新表名;--修改表选项:Alter table 表名 表选项 [=] 值;
rename table student to my_student;rename table class to my_class;-- Alter table my_student charset gbk;Alter table my_collation_bin collate =utf8_bin;
Drop table 表名1,表名2...;
drop table demo;drop table demodata;
新增字段是在表存在的基础上新增字段
Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置];
Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置];Alter table demo add column id int first;Alter table demo add id int;Alter table demo add class int after age;Alter table demo add number int not null after age;
修改字段一般都是修改字段数据类型或者字段属性
Alter table 表名 modify 字段名 数据类型 [属性] [位置];
Alter table my_student modify number char(10) after id;Alter table demo modify number int null ;--alter table student modify name varchar(20) not null;--alter table student modify name varchar(20) not null primary key;
Alter table 表名 change 旧字段 新字段 数据类型 [属性] [位置];
alter table demo change class room varchar(10);Alter table my_student change sex gender varchar(10);
Alter table 表名 drop 字段名;
Alter table my_student drop age;alter table demo drop room;
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL查询技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总》
希望本文所述对大家MySQL数据库计有所帮助。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号