当前位置:网站首页>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; 
边栏推荐
- ADB connects to Xiaomi mobile phone via Wi Fi
- Working principle of Lora to 4G and gateway repeater
- Four methods of importing CSV text files into Excel
- [force buckle] 645. Wrong set
- Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
- Construction and practice of yolov7 in hands-on teaching
- Leetcode1 -- sum of two numbers
- 「数字安全」警惕 NFT的七大骗局
- Explain the precision of floating point numbers in detail
- 【力扣】645.错误的集合
猜你喜欢

Brush questions - Luogu -p1152 happy jump

Applet H5 get mobile number scheme
![[force deduction] 1030. Arrange matrix cells in distance order](/img/6d/5301be3439cdb35500cbb4ae4a2534.png)
[force deduction] 1030. Arrange matrix cells in distance order
![[原创]九点标定工具之机械手头部相机标定](/img/de/5ea86a01f1a714462b52496e2869d6.png)
[原创]九点标定工具之机械手头部相机标定

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

Brush questions - luogu-p1089 Jinjin savings plan

Data analysis business core

Pytest.mark.parameterize and mock use

Three ways of redis cluster

【力扣】1030.距离顺序排列矩阵单元格
随机推荐
Hyperautomation for the enhancement of automation in industries
[platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1
Business data analysis of CDA level1 knowledge point summary
【配置Hifive1-revB】设备管理器中不识别端口,Can not connect to J-Link via USB的解决办法
Brush questions - Luogu -p1085 unhappy Jinjin
einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
Use of Bluetooth function of vs wireless vibrating wire acquisition instrument
Tm1638 LED digital display module Arduino drive code
Brush questions - Luogu -p1161 turn on the light
移动端网站,独立APP,网站排名策略有哪些?
LabVIEW的内部错误
手把手教学Yolov7的搭建及实践
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis
Brush questions - Luogu -p1035 series summation
Leetcode202 --- Happy number
Common problems in the use of wireless vibrating wire acquisition instrument
telnet远程登录aaa模式详解【华为eNSP】
How happy is the frisbee bureau? 2022 youth trendy Sports Report
pytest.mark.parametrize及mock使用
Acquisition data transmission mode and online monitoring system of wireless acquisition instrument for vibrating wire sensor of engineering instrument