当前位置:网站首页>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
边栏推荐
- Clean up system cache and free memory under Linux
- GaussDB(DWS)主动预防排查
- CNN convolution neural network principle explanation + image recognition application (with source code) [easy to understand]
- QT 使用FFmpeg4将argb的Qimage转换成YUV422P
- MySQL的视图练习题
- Design and practice of new generation cloud native database
- Learning notes on futuretask source code of concurrent programming series
- Unity 使用Sqlite
- Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
- 删除AWS绑定的信用卡账户
猜你喜欢

News classification based on LSTM model

详解Kubernetes网络模型

JS how to get a list of elements in a collection object

黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
![[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial](/img/b0/029cdea72483ed9bc8a0d66908983a.png)
[commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial

Basic operation of binary tree

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

高攀不起的希尔排序,直接插入排序

YOLOv5.5 调用本地摄像头

【juc学习之路第9天】屏障衍生工具
随机推荐
keras训练的H5模型转tflite
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
MQ learning notes
Four methods of JS array splicing [easy to understand]
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
flink sql-client 使用 对照并熟悉官方文档
What is the difference between PMP and NPDP?
Basic operation of binary tree
指标陷阱:IT领导者易犯的七个KPI错误
Getting started with the lockust series
I received a letter from CTO inviting me to interview machine learning engineer
13th Blue Bridge Cup group B national tournament
Recent public ancestor (LCA) online practices
Training on the device with MIT | 256Kb memory
mysql 学习笔记-优化之SQL优化
20220701
Little p weekly Vol.11
多种智能指针
Spark interview questions
【JetCache】JetCache的使用方法与步骤