本文实例讲述了Mysql数据表的基本操作之表结构操作,字段操作。分享给大家供大家参考,具体如下: 本节介绍: 表结构操作 创建数据表、 查看数据表和查看字段、 修改数据表结构 删除数据表 字段操作
本文实例讲述了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数据库计有所帮助。
--结束END--
本文标题: mysql数据表的基本操作之表结构操作,字段操作实例分析
本文链接: https://www.lsjlt.com/news/9528.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0