当前位置:网站首页>Learn MySQL from scratch - database and data table operations
Learn MySQL from scratch - database and data table operations
2022-07-01 22:34:00 【Linux server development】

Preface
Today we will learn the core content , Learn and practice how to modify database tables and contents in tables , Delete , Rename and so on .( I want to see how many kids love learning on weekends , Where are you ,O(∩_∩)O ha-ha ~)
1、 Catalog
Database operation : Delete database 、 modify the database .
Data table operation : rename table 、 Delete table 、 Add a column of data at the specified position 、 Delete a column 、 Rename a column 、 Change data type 、 Delete a row of records .
2、 step
This section will be operated through practice , Lead you to be familiar with the modification and deletion of the database .
Before starting this experiment , You need to download the relevant code first .
This code can create two new databases , Respectively called test and mysql_shiyan , And in mysql_shiyan Built in database 4 Tables (department,employee,project,table_1), Then insert data into it .
The statement of creating database and data table can be downloaded here .
https://github.com/rongweihe/MoreThanCode/tree/master/mysql_shiyanWhen the download is complete , Enter the command to turn on MySQL Service and use root The user login :
# open MySQL service
sudo service mysql start
# Use root The user login
mysql -u rootDownload the SQL5 Under the table of contents , Yes 1 File MySQL_05_01.sql .
Be careful : If the previous mysql_shiyan There is still , You can use it first drop database mysql_shiyan Delete . Load the data in the file you downloaded , If you download in home Under the table of contents , You need to in MySQL Enter the command in the console , Complete experimental preparation :
source /home/MySQL_05_01.sql;3、 Database operation
Use command SHOW DATABASES; You can see that just now MySQL_05_01.sql Two databases generated by the file :

Now let's run the command to delete the file named test The database of :DROP DATABASE test; Now use the command again SHOW DATABASES; You can find ,test The database has been successfully deleted :

About modifying the database : at present MySQL There is no method to modify the database name , Because this has led to a series of safety problems . In the old version RENAME DATABASE You can modify the database name , In subsequent experiments , Officials should find out the danger of this order , So at the moment MySQL 5.1.23 This command has been removed in version and later .
Someone on the Internet said that if it is MySQL The storage engine is MyISAM Words , Just modify DATA The name of the folder with the database name under the directory can be modified , I haven't tried this , You can try to see if you can modify .
in fact , Database names rarely have to be modified , If you have to , It is safer to rebuild a new library , Then transfer the data from the old library to the new library , And don't delete the old database for the time being , In case of data loss .
【 Article Welfare 】 In addition, Xiaobian also sorted out some C++ Back-end development interview questions , Teaching video , Back end learning roadmap for free , You can add what you need : Click to join the learning exchange group ~ Group file sharing
Xiaobian strongly recommends C++ Back end development free learning address :C/C++Linux Server development senior architect /C++ Background development architect

3.1 rename table
There are many forms of statements to rename a table , following 3 The effect of the two formats is the same :
RENAME TABLE Original name TO New name ;
ALTER TABLE Original name RENAME New name ;
ALTER TABLE Original name RENAME TO New name ;Access to database mysql_shiyan :
use mysql_shiyanUse the command to try to modify table_1 The name is table_2 :
RENAME TABLE table_1 TO table_2;
3.2 Delete table
Statement to delete a table , Similar to the statement used just now to delete the database , This is the format :
DROP TABLE Table name ;For example, we put table_2 Table delete ;
DROP TABLE table_2;
3.3 Add a row
Modification of table structure , It will actually affect the structure of the data table , Sometimes small mistakes can have irreparable consequences , So operate carefully . In the actual production environment , Be sure to get the permission of your superiors , Otherwise, do not modify it easily .
Let's see , Add a column to the table in the form of :
ALTER TABLE Table name ADD COLUMN Name data type constraint ;
ALTER TABLE Table name ADD Name data type constraint ;Now? employee Table has id、name、age、salary、phone、in_dpt this 6 Columns , We try to join height ( height ) A column and specify DEFAULT constraint :
ALTER TABLE employee ADD height INT(4) DEFAULT 170;
You can find : Newly added columns , It is placed on the far right of this table by default . If you want to insert the added column in the specified position , It needs to be used at the end of the statement AFTER key word (“AFTER Column 1” Indicates that the new column is placed in “ Column 1” Behind ).
remind : Statement INT(4) Not the number of bytes representing an integer , Instead, it represents the display width of the value , If the fill character is set to 0, be 170 Is shown as 0170
For example, we add a new column weight( weight ) Put in age( Age ) Behind :
ALTER TABLE employee ADD weight INT(4) DEFAULT 120 AFTER age;
The above effect is to add the new column after a certain position , If you want to put it in the first column , Then use FIRST key word , If statement :
ALTER TABLE employee ADD test INT(10) DEFAULT 11 FIRST;The effect is as follows :

3.4 Delete a column
Deleting a column in the table is very similar to the statement format of adding a column just used , Just put the key words ADD Change it to DROP , The statement does not need to be followed by a data type 、 Constraint or location information . Specific sentence format :
ALTER TABLE Table name DROP COLUMN Name ;
or :ALTER TABLE Table name DROP Name ;Let's take what we just added test Delete :
ALTER TABLE employee DROP test;
3.5 Rename a column
To be exact , Rename this statement, which is to modify a column (CHANGE) :
ALTER TABLE Table name CHANGE The original name New column names data type constraint ;Be careful : After this rename statement “ data type ” Don't omit , Otherwise, renaming fails .
When the original column name is the same as the new column name , Specify a new data type or constraint , It can be used to modify data types or constraints . It should be noted that , Changing the data type may result in data loss , Therefore, it also needs to be used carefully .
We use this statement to “height” Rename a column to Hanyu Pinyin “shengao” , The effect is as follows :
ALTER TABLE employee CHANGE height shengao INT(4) DEFAULT 170;
3.6 Change the data type
To change the data type of a column , In addition to using the CHANGE Out of statement , You can also use this MODIFY sentence :
ALTER TABLE Table name MODIFY Name New data types ;Here we need a very special reminder , You must be careful when modifying data types , Because this may cause your data loss .
It is best to create an online database before , Consider the type of data , You can create a temporary database to operate the data and see if it meets the expectations , Confirm that there is no problem in creating an online database . Before trying to modify the data type , Be sure to carefully consider the possible consequences of the operation .
3.7 Modify a value in the table
Most of the time, what we need to modify is not the whole database or the whole table , It's one or more data in the table , This requires us to use the following command to achieve precise modification :
UPDATE Table name SET Column 1= value 1, Column 2= value 2 WHERE Conditions ;for instance , Recently, what about Xiao Wei , Due to outstanding work performance , The company gave him a raise , Then we should xiaowei Of salary Change it to 20000, You can write like this :
UPDATE employee SET age=25 ,salary=20000 WHERE name='xiaowei';
Be careful : Be sure to have WHERE Conditions , Otherwise, there will be unwanted consequences
3.8 Delete a row of records
Delete a row of data in the table , You have to add WHERE Conditions , Otherwise, the whole column of data will be deleted . Delete statements :
DELETE FROM Table name WHERE Conditions ;We tried to xiaohong Data deletion :
DELETE FROM employee WHERE name='xiaohong';
There is no more in the database xiaohong 了 .
4、 summary
In this experiment, we learned about the database 、 Database table 、 Modification and deletion methods of database table items and records .
Reference material

Recommend a zero sound education C/C++ Free open courses developed in the background , Personally, I think the teacher spoke well , Share with you :C/C++ Background development senior architect , The content includes Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK, Streaming media ,CDN,P2P,K8S,Docker,TCP/IP, coroutines ,DPDK Etc , Learn now
original text : Learn from scratch MySQL -- Database and data table operations
边栏推荐
- Tops, the unit of computing power of the processor, can be carried out 1 trillion times per second
- 【单体】流辰信息I-BPSv3服务器推荐配置
- PHP reflective XSS, reflective XSS test and repair
- Business visualization - make your flowchart'run'up
- 配置筛选机
- Introduction and download of the latest version of airserver2022
- 从零开始学 MySQL —数据库和数据表操作
- 按照功能对Boost库进行分类
- Internet of things RFID, etc
- Basic knowledge of ngnix
猜你喜欢

二叉树的基本操作

In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition

linux下清理系统缓存并释放内存

Mysql——》Innodb存储引擎的索引

Mask wearing detection method based on yolov5

Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo

详解JMM

园区全光技术选型-中篇

Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment

互联网的智算架构设计
随机推荐
并发编程系列之FutureTask源码学习笔记
【图像分割】2021-SegFormer NeurIPS
为什么数字化转型战略必须包括持续测试?
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
地图其他篇总目录
flink sql 命令行 连接 yarn
awoo‘s Favorite Problem(优先队列)
微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
MySQL系列之事务日志Redo log学习笔记
Learning notes on futuretask source code of concurrent programming series
Unity uses SQLite
Simple interactive operation of electron learning (III)
Matlab traverses images, string arrays and other basic operations
记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
高攀不起的希尔排序,直接插入排序
MySQL之MHA高可用配置及故障切换
MySQL empties table data
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
News classification based on LSTM model