当前位置:网站首页>MySQL table operation
MySQL table operation
2022-07-25 14:06:00 【GSX_ MI】
1. Create table
grammar :
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set Character set collate Validation rules engine Storage engine ;- fifield Said column names
- datatype Indicates the type of the column
- character set Character set , If no character set is specified , Then the character set of the database shall prevail
- collate Validation rules , If no verification rule is specified , The verification rules of the database shall prevail
2. Create a table case
create table users (
id int,
name varchar(20) comment ' user name ',
password char(32) comment ' password ',
birthday date comment ' Birthday '
) character set utf8 engine MyISAM; - users.frm: Table structure
- users.MYD: Table data
- users.MYI: Table index
②comment Followed by comments

3. View table structure
(1)desc The name of the table ;

(2) show create table The name of the table \G;

4. Modify table
(1) In the actual development of the project , Often modify the structure of a table , For example, field name , Field size , Field type , The character set type of the table , Table storage engine and so on . We still have needs , Add fields , Delete fields and so on . Then we need to modify the table .
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) When we create database tables , It is not recommended to modify the established table structure ; If the table is really created incorrectly, don't change it , Recreate ﹔ Don't change the table structure easily , Changes may cause serious errors , The table structure determines how the upper business is written .
(3) Example
① stay users Add two records to the table
insert into users values(1,' Zhang San ','123456','2001-1-1'),(2,' Li Si ','88888','2002-1-1');
② stay users Table add a field , Used to save the picture path
alter table users add path varchar(100) comment ' Picture path ' after id;- after Following the column name means that the new column is after the column , without after, The default is in the last column .
- After inserting the new field , It has no effect on the data in the original table

③ modify name, Change its length to 60
alter table users modify name varchar(60);- modify It is an overlay modification

④ Delete password Column
alter table users drop password;- Be careful when deleting fields , The deleted field and its corresponding column data are gone
⑤ Change the table name to person
alter table users rename to person;- to It can be omitted

⑥ Add column name id It is amended as follows flag
alter table person change id flag int comment ' Identify a user ';- The new field needs to be fully defined ( type ).

⑦ Summary
The table structure should not be deleted easily in the working scenario , Make a backup before deleting
alter table Your watch name add/modify/drop Column + Column properties ;
alter table Your watch name rename New table name ;
alter table Your watch name change Old column name New column + Properties of the new column ;
4. Delete table
drop table student; 
边栏推荐
- Brush questions - Luogu -p1146 coin flip
- NUC980 设置SSH Xshell连接
- How happy is the frisbee bureau? 2022 youth trendy Sports Report
- 力扣(LeetCode)205. 同构字符串(2022.07.24)
- From fish eye to look around to multi task King bombing -- a review of Valeo's classic articles on visual depth estimation (from fisheyedistancenet to omnidet) (Part 2)
- Detailed explanation of nat/napt address translation (internal and external network communication) technology [Huawei ENSP]
- Interpretation of featdepth self-monitoring model for monocular depth estimation (Part 2) -- use of openmmlab framework
- swiper 一侧或两侧露出一小部分
- CDA level Ⅰ 2021 new version simulation question 2 (with answers)
- [force buckle] 645. Wrong set
猜你喜欢

Brush questions - Luogu -p1075 prime factor decomposition

百度搜索打击盗版网文站点,SEOer应该关注哪些问题?

Day1:三种语言暴刷牛客130题

word设置粘贴仅保留文本

Comprehensive sorting and summary of maskrcnn code structure process of target detection and segmentation

手把手教学Yolov7的搭建及实践

Brush questions - Luogu -p1059 clear random number

Brush questions - Luogu -p1150 Peter's smoke

Interpretation of featdepth self-monitoring model for monocular depth estimation (Part 2) -- use of openmmlab framework

Pytorch uses tensorboard to realize visual summary
随机推荐
Use of Bluetooth function of vs wireless vibrating wire acquisition instrument
IM system - some common problems of message streaming
金鱼哥RHCA回忆录:CL210管理存储--管理共享文件系统
Leetcode1 -- sum of two numbers
Applet enterprise red envelope function
Practice of online problem feedback module (13): realize multi parameter paging query list
【学习记录】plt.show()闪退解决方法
~4.1 sword finger offer 05. replace spaces
命名空间与库
Okaleido生态核心权益OKA,尽在聚变Mining模式
OKA通证权益解析,参与Okaleido生态建设的不二之选
手里有点钱可以投资哪些理财产品?
Xintang nuc980 set DHCP or static IP
Deep understanding of pytorch distributed parallel processing tool DDP -- starting from bugs in engineering practice
Working principle of Lora to 4G and gateway repeater
Namespaces and libraries
手把手教学Yolov7的搭建及实践
MySQL and Navicat installation and stepping on pits
~4.2 CCF 2021-12-1 sequence query
[directory blasting tool] information collection stage: robots.txt, Yujian, dirsearch, dirb, gobuster