当前位置:网站首页>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
边栏推荐
猜你喜欢

【MySQL】索引的分类

配置筛选机

Application of real estate management based on 3D GIS
![[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

Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud

Clean up system cache and free memory under Linux

互联网的智算架构设计

MySQL之MHA高可用配置及故障切换

ICML2022 | 基于元语义正则化的介入性对比学习

Mysql——》MyISAM存储引擎的索引
随机推荐
Mysql——》MyISAM存储引擎的索引
信标委云原生专题组组长,任重道远!
Burpsuite simple packet capturing tutorial [easy to understand]
Tops, the unit of computing power of the processor, can be carried out 1 trillion times per second
91.(cesium篇)cesium火箭发射模拟
CSDN购买的课程从哪里可以进入
MQ learning notes
【juc学习之路第9天】屏障衍生工具
Four methods of JS array splicing [easy to understand]
Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
Several ways of writing main function in C
Simple interactive operation of electron learning (III)
News classification based on LSTM model
Icml2022 | interventional contrastive learning based on meta semantic regularization
"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition
MySQL learning notes - SQL optimization of optimization
Design and practice of new generation cloud native database
Classify boost libraries by function
IDA动态调试apk
The correct way to set the bypass route