当前位置:网站首页>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_shiyan
When 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 root
Download 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_shiyan
Use 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
边栏推荐
- MIT|256KB 内存下的设备上训练
- [STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
- Basic operation of binary tree
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- Application of real estate management based on 3D GIS
- A debugging to understand the slot mechanism of redis cluster
- 【JetCache】JetCache的使用方法与步骤
- Recent public ancestor (LCA) online practices
- Ida dynamic debugging apk
猜你喜欢
K-means based user portrait clustering model
Recent public ancestor offline practice (tarjan)
GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
完全注解的ssm框架搭建
In the past 100 years, only 6 products have been approved, which is the "adjuvant" behind the vaccine competition
IDA动态调试apk
JS how to get a list of elements in a collection object
Mysql——》MyISAM存储引擎的索引
Indicator trap: seven KPI mistakes that it leaders are prone to make
keras训练的H5模型转tflite
随机推荐
Gaussdb (DWS) active prevention and troubleshooting
Basic knowledge of ngnix
MySQL series transaction log redo log learning notes
GaussDB(DWS)主动预防排查
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
Relationship and difference between enterprise architecture and project management
RestTemplate 远程调用工具类
Object memory layout
Recent public ancestor (LCA) online practices
QT uses ffmpeg4 to convert the qimage of ARGB to yuv422p
MySQL数据库详细学习教程
【MySQL】数据库优化方法
Getting started with the lockust series
Business visualization - make your flowchart'run'up
Mysql——》索引存储模型推演
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
高攀不起的希尔排序,直接插入排序
QT版本华睿相机的Demo程序实现
【juc学习之路第8天】Condition