当前位置:网站首页>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; 
边栏推荐
- Brush questions - Luogu -p1085 unhappy Jinjin
- Alibaba mqtt IOT platform "cloud product circulation" practice - the two esp32 achieve remote interoperability through the IOT platform
- [server data recovery] HP EVA server storage raid information power loss data recovery
- leetcode1 --两数之和
- redis集群的三种方式
- Brush questions - Luogu -p1035 series summation
- 百度搜索打击盗版网文站点,SEOer应该关注哪些问题?
- MySQL and Navicat installation and stepping on pits
- I2C can also be powered on by bus!
- AQS of concurrent programming
猜你喜欢

数字孪生 - 认知篇

Install oh my Zsh

Brush questions - luogu-p1089 Jinjin savings plan

Working principle of Lora to 4G and gateway repeater

Pytorch uses tensorboard to realize visual summary
![[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster

依迅总经理孙峰:公司已完成股改,准备IPO

Applet enterprise red envelope function

Tm1637 four digit LED display module Arduino driver with second dot

CDA level Ⅰ 2021 new version simulation question 1 (with answers)
随机推荐
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis
CSV文本文件导入excel的四种方法
leetcode--四数相加II
Depth estimation self-monitoring model monodepth2 paper summary and source code analysis [theoretical part]
NoSQL, relational database, row and column database comparison and analogy
职场「数字人」不吃不睡007工作制,你「卷」得过它们吗?
Applet sharing function
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster
Brush questions - Luogu -p1151 sub number integer
Wangeditor rich text editor
From input URL to web page display
leetcode1 --两数之和
AQS of concurrent programming
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
MXNet对DenseNet(稠密连接网络)的实现
redis集群的三种方式
Construction and practice of yolov7 in hands-on teaching
Namespaces and libraries
MySQL 01: Source command