当前位置:网站首页>Basic usage of MySQL database
Basic usage of MySQL database
2022-06-10 05:59:00 【Liaoruoxingchen lty】
database (database) It's for organization 、 Storage and Management data The warehouse of .
MySQL database ( At present, it is most widely used 、 The most popular Free open source database ; Yes Community Community free edition and Enterprise Enterprise charging version ).
MySQL Databases also belong to Traditional database , Its data structure is divided into database 、 Data sheet 、 data row 、 Field this 4 Most of the components .
Use SQL Statement management database
SQL namely Structured query language , special A programming language used to access and process databases . Can let us In the form of programming , Operate the data in the database .
adopt SQL We can Create database 、 Data sheet 、 stored procedure 、 View etc. , Also can be Data increase 、 Delete 、 Change 、 check etc. .
notes :SQL The comments in It's written in :-- ( Subtract spaces )
SQL Statement Keywords are case insensitive . Such as SELECT Equivalent to select
Operation of database
View the list of existing databases
show databases;create new database
create database my_db_01Delete the created database
drop database my_db_01View the database currently in use
select database()Switch or use the new database
use The name of the database to use Operation of data table
View the list of existing data tables in the current database
show tablesLook at the structure of a table
desc Table name Create a new data table
create table Table name (
Field name data type Optional constraints ,
The second field name data type constraint condition
)demo: Create a User information sheet
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT COMMENT ' Unique identification of user information ',
username VARCHAR(45) NOT NULL COMMENT ' User name for login ',
password VARCHAR(45) NOT NULL COMMENT ' User name and password for login ',
status TINYINT(1) NOT NULL DEFAULT 0 COMMENT ' Indicates the status of the user , It's a Boolean value \n0 Indicates normal state ( The default value is 0 )\n1 Indicates that the user is disabled or marked for deletion ',
PRIMARY KEY (id))
COMMENT = ' User information sheet ';Delete the existing data table
drop table Table name Operations to modify table structure
Add table fields
alter table Table name add new field name data type Limiting conditions
-- demo: alter table users add avatar varchar(45) not nullDelete table fields
alter table Table name drop Field name
-- demo: alter table users drop avatarModify the existing field name
alter table Table name change Old fields The new fields data type Limiting conditions ;
-- demo: alter table users change username uname varchar(45) not nullModify the data types and restrictions of existing fields
alter table Table name change Old fields The new fields data type Limiting conditions ;
-- demo: alter table users change username username varchar(30)notes : Old and new field names can be the same ,change Keywords can only modify data types or constraints
The main operations of data rows ( Additions and deletions )
newly added ——insert into
insert into Table name ( Name 1, Name 2, ...) values (' value 1', ' value 2', ...)
-- demo: insert into users(username, password) value ('zs', '123456')
-- users In the table id Fields and status Field values can be omitted (id It will automatically increment ,status Have default values ) notes : stay SQL The value inserted in the statement can use ? Conduct placeholder Fill in the value later . Such as :
insert into users (username, password) value(?, ?)
Delete ——delete
delete from Table name where id=' Columns to be deleted id';
-- demo: delete from users where id=2
-- Deleted id = 2 This piece of data Be careful : When performing a delete operation, be sure to provide where The qualification of , If Without this condition, the entire table will be deleted , must do Be careful !
modify ——update
update Table name set Name =' Modified value ' where id=' Of the data to be modified id'
-- (where Followed by the specific screening criteria , If you do not write this change, it will affect the entire table )
-- demo: update users set password=123123 where id=2;
-- take id=2 The password of this piece of data is changed to 123123notes : The columns to be modified in the same row can be multiple columns , Use English commas to separate columns
Inquire about ——select
-- Query all data in the specified table
select * from The name of the table
-- demo: select * from users
-- Query the data of the specified column in the specified table
select Column name from The name of the table
-- demo: select username from users;
-- You can also query multiple columns , Such as :
-- demo: select username, password from users;where Clause
SQL Of where Clause for Define the criteria for selection , stay select、update、delete Can be used in any statement where Clause to limit the selection criteria .
- You can use one or more tables in a query statement , The tables are separated by English commas , And use where Statement to set the query conditions .
- You can where Clause to specify any condition . Such as =、>、<、>=、...
- You can use and perhaps or Specify one or more conditions .
- where Clause can also be applied to SQL Of DELETE perhaps UPDATE command .
- where Clause is similar to... In programming languages if Conditions , according to MySQL Table to read the specified data .
SQL Medium and and or Operator
and and or Can be in where In Clause Two or more Combine the conditions .
and I have to At the same time satisfy Multiple conditions , amount to JS Medium &&
or As long as you meet Any one The conditions are good , amount to JS Medium ||
SQL Medium order by Clause —— Sort
order by Examples of use :
select * from Table name order by Name
-- demo: select * from users order by idnotes : In the example demo It means that users In the table id This column is for all data Ascending Sort display ( Column name followed by asc Expressing ascending order , You can omit it , The default is ascending )
order by Name desc Examples of use :
select * from Table name order by Name desc
-- demo: select * from users order by id desc;notes : In the example demo It means that users In the table id Field to all data Descending Sort display (desc Representation of descending order )
order by Medium Multiple sort
stay order by hinder Sorting can be based on multiple fields , Multiple conditions are separated by commas , The final The result set satisfies the sorting conditions in turn Show after . Examples are as follows :
select * from users order by id, username desc;
-- The example represents a query users All the data in the table are pressed id Ascending 、username The result set is displayed in descending order sount(*) keyword
Use count(*) Sure Inquire about Specify... In the table Total number of data . Examples are as follows :
select count(*) from Table name where Qualifications
-- demo: select count(*) from users where status=0
-- The example represents a query users In the table status=0 The total number of pieces of data as keyword
Use as Keywords can Alias the list . Examples are as follows :
select Name as Alias from Table name
-- demo: select username as ' user name ', password as ' password ' from users;notes :as The effect of keywords is generally displayed in the result set after query , Alias is only used to display the results of this query , The column names and data in the original table will not be affected .
thus MySQL Medium SQL The basic use of the statement is introduced first . For more information, see Novice tutorial -MySQL course
study hard 、 Day day up ! :)
边栏推荐
- Machine learning notes - what are Bleu scores?
- matlab中不同随机数的生成
- 刃 7000P 内存频率限制 2400 的解决方法
- Struct in golang
- Difference between risc-v "access fault" and "page fault"
- Focus on web development, simple web development
- Common English abbreviations of display
- [homeassistant drive servo]
- [understanding of opportunity -20]: Guiguzi - reaction chapter - the art of movement and stillness, the combination of speaking and listening, silence is golden
- ArcGIS应用(十九)Arcgis 统计分析计算多波段图像最大值、最小值、平均值等
猜你喜欢

Model lightweight pruning distillation quantification series yolov5 lossless pruning (with source code)

SZT mr- in depth understanding, configuration file, exception case, load

《深度学习入门》学习笔记

Machine learning notes - what are Bleu scores?

【软件工程导论】知识点汇总 | 适用于考试复习 | 轻松通过考试

QT configures opencv-4.5.1 and runs the program

5. inverse Polish representation, ternary expression, quaternion expression

Thesis reading (53):universal advantageous perturbations

AssertJ 的异常(Exception )断言

Web171~180 of ctfshow - SQL injection (1)
随机推荐
Simple and interesting Snake growth game -- greedy snake
MySQL数据库的基本使用
What does IP address 0.0.0.0 mean?
Thank you for the Big White Rabbit candy of Yuyi children's shoes
Idea plug-in recommendation: file tree enhancement, displaying class comments
将对象数组转换成二维数组
These six open source protocols (GPL, LGPL, BSD, MIT, APACHE)
Flink 系例 之 SessionWindow
[introduction to software engineering] summary of knowledge points | suitable for examination review | easy to pass the exam
JVM memory overflow exercise record
Flink's timewindow
NPM command Encyclopedia
SZT mr- in depth understanding, configuration file, exception case, load
C # student management system designed to cope with most examination sites involved in the final period
[homeassistant drive servo]
Learning of common functional interfaces
npm命令大全
Two Sum
APM flight control learning notes hovering loiter mode -cxm
反向域名解析是什么?