当前位置:网站首页>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; 
边栏推荐
- word设置粘贴仅保留文本
- [configure hifive1 revb] the device manager does not recognize the port, and can not connect to j-link via USB
- einsum(): operands do not broadcast with remapped shapes [original->remapped]: [1, 144, 20, 17]->[1,
- NUC980 设置SSH Xshell连接
- Tm1638 LED digital display module Arduino drive code
- 手里有点钱可以投资哪些理财产品?
- @Wrap decorator
- Brush questions - Luogu -p1085 unhappy Jinjin
- Common methods of arrays
- Mxnet implementation of densenet (dense connection network)
猜你喜欢

Word set paste to retain only text

Brush questions - Luogu -p1151 sub number integer

Arduino code of key state machine for realizing single, double click, long press and other functions with esp32 timed interrupt

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

What are the ranking strategies for mobile websites, independent apps and websites?

word设置粘贴仅保留文本

Install oh my Zsh

Leetcode1 -- sum of two numbers

Brush questions - Luogu -p1075 prime factor decomposition
![[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
随机推荐
pytest.mark.parametrize及mock使用
伯克利博士『机器学习工程』大实话;AI副总裁『2022 ML就业市场』分析;半导体创业公司大列表;大规模视频人脸属性数据集;前沿论文 | ShowMeAI资讯日报
手里有点钱可以投资哪些理财产品?
Day1:三种语言暴刷牛客130题
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
redis集群的三种方式
Engineering monitoring multi-channel vibrating wire sensor wireless acquisition instrument external digital sensor process
NoSQL, relational database, row and column database comparison and analogy
@wrap 装饰器
Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
LabVIEW的内部错误
Explain the precision of floating point numbers in detail
Business analysis report and data visualization report of CDA level1 knowledge point summary
[configure hifive1 revb] the device manager does not recognize the port, and can not connect to j-link via USB
Experiment the Arduino code of NTP network timing alarm clock with esp32+tm1638
Common problems of wireless relay acquisition instrument
NAT/NAPT地址转换(内外网通信)技术详解【华为eNSP】
Use of Bluetooth function of vs wireless vibrating wire acquisition instrument
Internal error of LabVIEW
Brush questions - Luogu -p1161 turn on the light