当前位置:网站首页>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_01

  Delete the created database

drop database my_db_01

  View 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 tables

  Look 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 null

  Delete table fields

alter table  Table name  drop  Field name 
-- demo: alter table users drop avatar

Modify 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 null

Modify 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  123123

notes : 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 selectupdatedelete 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 id

notes : 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 !        :)

原网站

版权声明
本文为[Liaoruoxingchen lty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100546215236.html