当前位置:网站首页>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; 
边栏推荐
- Typora无法打开提示安装新版本解决办法
- Brush questions - Luogu -p1075 prime factor decomposition
- 职场「数字人」不吃不睡007工作制,你「卷」得过它们吗?
- Mysql表的操作
- Nuc980 set up SSH xshell connection
- Problems and extensions of the monocular depth estimation model featdepth in practice
- Brush questions - Luogu -p1035 series summation
- Brush questions - Luogu -p1150 Peter's smoke
- swiper 一侧或两侧露出一小部分
- How happy is the frisbee bureau? 2022 youth trendy Sports Report
猜你喜欢

Redux usage and analysis

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

Brush questions - Luogu -p1046 Tao Tao picking apples

Digital Twins - cognition

Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?

Esp32 connects to Alibaba cloud mqtt IOT platform

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

Common problems in the use of wireless vibrating wire acquisition instrument

Feiwo technology IPO meeting: annual revenue of 1.13 billion Hunan Cultural Tourism and Yuanli investment are shareholders

Pytest.mark.parameterize and mock use
随机推荐
科隆新能源IPO被终止:拟募资6亿 先进制造与战新基金是股东
Interpretation of featdepth self-monitoring model for monocular depth estimation (Part I) -- paper understanding and core source code analysis
Business analysis report and data visualization report of CDA level1 knowledge point summary
Namespaces and libraries
Day1: 130 questions in three languages
It is predicted that 2021 will accelerate the achievement of super automation beyond RPA
Xintang nuc980 set DHCP or static IP
Day1:三种语言暴刷牛客130题
如何设计一个高并发系统?
NoSQL, relational database, row and column database comparison and analogy
高版本MySQL服务端安装不上怎么办,忘记密码(MySQL8.0.29)?
Dr. Berkeley's "machine learning engineering" big truth; AI vice president '2022 ml job market' analysis; Large list of semiconductor start-ups; Large scale video face attribute data set; Cutting edge
Working principle of Lora to 4G and gateway repeater
CDA level Ⅰ 2021 new version simulation question 2 (with answers)
在线问题反馈模块实战(十三):实现多参数分页查询列表
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
opencv视频跟踪「建议收藏」
Workplace "digital people" don't eat or sleep 007 work system, can you "roll" them?
sieve of eratosthenes
Pytest.mark.parameterize and mock use