当前位置:网站首页>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; 
边栏推荐
- Three ways of redis cluster
- What problems should SEOER pay attention to when baidu searches and attacks pirated websites?
- Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
- Vscode plug-in development
- 2271. Maximum number of white bricks covered by blanket ●●
- Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt
- [platform IO compile hifive1 revb] * * * [.pio\build\hifive1 revb\src\setupgpio.o] solution to error 1
- IM系统-消息流化一些常见问题
- The practice of depth estimation self-monitoring model monodepth2 in its own data set -- single card / multi card training, reasoning, onnx transformation and quantitative index evaluation
- Data analysis interview records 1-5
猜你喜欢

Brush questions - Luogu -p1161 turn on the light

Three ways of redis cluster

Redux usage and analysis

Acquisition data transmission mode and online monitoring system of wireless acquisition instrument for vibrating wire sensor of engineering instrument

From input URL to web page display

The practice of depth estimation self-monitoring model monodepth2 in its own data set -- single card / multi card training, reasoning, onnx transformation and quantitative index evaluation

Vscode plug-in development

飞盘局有多快乐?2022年轻人新潮运动报告

Mongodb源码部署以及配置

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
随机推荐
MXNet对DenseNet(稠密连接网络)的实现
wangeditor 富文本编辑器
einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
Brush questions - Luogu -p1047 trees outside the school gate
Hyperautomation for the enhancement of automation in industries
AQS of concurrent programming
Nodejs link MySQL error: Er_ NOT_ SUPPORTED_ AUTH_ MODEError: ER_ NOT_ SUPPORTED_ AUTH_ MODE
【学习记录】plt.show()闪退解决方法
@Classmethod decorator
pytest.mark.parametrize及mock使用
GCD details
Leetcode 205. isomorphic string (2022.07.24)
Practice of online problem feedback module (13): realize multi parameter paging query list
Write an esp32 Watchdog with Arduino
Alibaba mqtt IOT platform "cloud product circulation" practice - the two esp32 achieve remote interoperability through the IOT platform
Framework package merge script
Working principle of Lora to 4G and gateway repeater
[force deduction] 1030. Arrange matrix cells in distance order
依迅总经理孙峰:公司已完成股改,准备IPO
Three ways of redis cluster