当前位置:网站首页>Mysql表的操作
Mysql表的操作
2022-07-25 13:58:00 【GSX_MI】
1.创建表
语法:
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;- fifield 表示列名
- datatype 表示列的类型
- character set 字符集,如果没有指定字符集,则以所在数据库的字符集为准
- collate 校验规则,如果没有指定校验规则,则以所在数据库的校验规则为准
2.创建表案例
create table users (
id int,
name varchar(20) comment '用户名',
password char(32) comment '密码',
birthday date comment '生日'
) character set utf8 engine MyISAM; - users.frm:表结构
- users.MYD:表数据
- users.MYI:表索引
②comment后面跟的是注释

3.查看表结构
(1)desc 表名称;

(2) show create table 表名称 \G;

4. 修改表
(1)在项目实际开发中,经常修改某个表的结构,比如字段名字,字段大小,字段类型,表的字符集类型,表的存储引擎等等。我们还有需求,添加字段,删除字段等等。这时我们就需要修改表。
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename DROP (column);
(2)我们在创建数据库表的时候,不建议修改建立好的表结构;如果表真的创建错了就不要改了,重新创建﹔表结构不要轻易更改,更改了可能出现严重的错误,表结构决定了上层业务怎么写。
(3)示例
①在users表添加二条记录
insert into users values(1,'张三','123456','2001-1-1'),(2,'李四','88888','2002-1-1');
②在users表添加一个字段,用于保存图片路径
alter table users add path varchar(100) comment '图片路径' after id;- after后面跟列名称就表示新列在该列后面,如果没有after,默认在最后一列。
- 插入新字段后,对原来表中的数据没有影响

③修改name,将其长度改成60
alter table users modify name varchar(60);- modify是覆盖式的修改

④删除password列
alter table users drop password;- 删除字段一定要小心,删除字段及其对应的列数据都没了
⑤修改表名为person
alter table users rename to person;- to 可以省略

⑥将列名称id修改为flag
alter table person change id flag int comment '标识一个用户';- 新字段需要完整定义(类型)。

⑦ 小结
表结构在工作场景中不要轻易删除,删除前做个备份
alter table 你的表名字 add/modify/drop 列+列属性;
alter table 你的表名字 rename 新的表名字;
alter table 你的表名字 change 旧列名称 新列+新列的属性;
4.删除表
drop table student; 
边栏推荐
- Leetcode1 -- sum of two numbers
- Word set paste to retain only text
- Engineering monitoring multi-channel vibrating wire sensor wireless acquisition instrument external digital sensor process
- Day1:三种语言暴刷牛客130题
- MySQL and Navicat installation and stepping on pits
- Working principle of Lora to 4G and gateway repeater
- mysql 01: source命令
- 飞沃科技IPO过会:年营收11.3亿 湖南文旅与沅澧投资是股东
- Brush questions - Luogu -p1046 Tao Tao picking apples
- 手里有点钱可以投资哪些理财产品?
猜你喜欢

Applet H5 get mobile number scheme

Dr. Berkeley's "machine learning engineering" big truth; AI vice president '2022 ml job market' analysis; Large list of semiconductor start-ups; Large scale video face attribute data set; Cutting edge

leetcode202---快乐数

Brush questions - Luogu -p1047 trees outside the school gate

Alibaba mqtt IOT platform "cloud product circulation" practice - the two esp32 achieve remote interoperability through the IOT platform

How happy is the frisbee bureau? 2022 youth trendy Sports Report

Three ways of redis cluster

Brush questions - Luogu -p1085 unhappy Jinjin

Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?

It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
随机推荐
「数字安全」警惕 NFT的七大骗局
@wrap 装饰器
Dr. Berkeley's "machine learning engineering" big truth; AI vice president '2022 ml job market' analysis; Large list of semiconductor start-ups; Large scale video face attribute data set; Cutting edge
Brush questions - Luogu -p1046 Tao Tao picking apples
Leetcode1 -- sum of two numbers
G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
Setting of parameter configuration tool for wireless vibrating wire collector
MySQL and Navicat installation and stepping on pits
leetcode1 --两数之和
新唐NUC980设置DHCP或者静态IP
Working principle of Lora to 4G and gateway repeater
Brush questions - Luogu -p1047 trees outside the school gate
[server data recovery] HP EVA server storage raid information power loss data recovery
Use of Bluetooth function of vs wireless vibrating wire acquisition instrument
移动端网站,独立APP,网站排名策略有哪些?
It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
From input URL to web page display
[原创]九点标定工具之机械手头部相机标定
Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation
What you must know about data engineering in mlops