Collectable MySQL Basic summary

Starting and stopping services

  • Stop the database service, run as administrator in DOS
    net stop mysql
  • Open the database service, run as administrator in DOS
    net start mysql

Related to the operation of the database

  • Connect to the database
    mysql -uroot -p enter the password
  • Change your password, fill in your own password into the last single quote
    alter user 'root'@'localhost' identified by 'root';
  • Create a database
    create database 数据库名;
  • Create a database, then create a judgment does not exist
    create database if not exists 数据库名;
  • Displays all the database, which is the default four
    show databases;
  • Use Database
    use 数据库名;
  • Delete Database
    drop database 数据库名;
  • Delete the database, to determine the presence delete
    drop database if exists 数据库名;

Database tables related operations

  • Create a database table
	create table 表名 (
	 	  列名1 数据类型1,
	      列命2 数据类型2,
	      ...
	      列名n 数据类型n,
	      primary key 列名(主键)
	 );
  • View all tables
    show tables;
  • View the database table structure
    desc 表名;
  • Delete table
    drop table 表名;
  • Modify the database table name
    alter table 表名 rename to 新表名;
  • Add Column
    alter table 表名 add 列名 数据类型;
  • Remove Columns
    alter table 表名 drop 列名;

Related operation data in the table

  • adding data
  • insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n);Life and column values ​​to correspond.
  • insert into 表名 values(值1,值2,...值n);The value here to include all the columns in the table.
  • delete data
  • delete from 表名 where 条件;Delete the data meet the criteria.
  • delete from 表名;If conditions do not write, delete all data table by default.
  • change the data
  • update 表名 set 列名1 = 值1, 列名2 = 值2,... where 条件;Here is to modify the data conditions are met.
  • update 表名 set 列名1 = 值1, 列名2 = 值2,...;If it is without conditions, all data modifications.
  • Query data
  • select 列命 from 表名, Query data in a column.
  • select * from 表名Query data for all columns.
  • select distinct 列命 from 表名, Query data deduplication.
  • Conditional statements (where)
    for the convenience of presentation, the query results are out of here with a * instead.-
  • select * from 表名 where 条件1 and 条件2Query data satisfy the conditions 1 and conditions 2.
  • select * from 表名 where 条件1 or 条件2Query data satisfies Condition 1 or Condition 2.
  • select * from 表名 where not 条件1The query does not meet the condition data 1.
    Here Insert Picture Description
  • select * from 表名 where 列名 is null;, Query data in a column is empty.
  • select * from 表名 where 列名 is not null;Query a column non-null data.
  • select * from 表名 where 列名 between 值1 and 值2;, Query data between a column value of 1 and the value 2.
  • select * from 表名 where 列名 like 'hello%';Query all data beginning with hello, like using a combination of%, which represents 0% to any number of characters.
  • select * from 表名 where 列名 like 'hello_';Queries all begin with hello and back with only one character of data, like the use of combination _, _ on behalf of a character.
  • select * from 表名 where 列名in (值1,值2,...);, Data query a column in a certain area.

Sorting and paging

Sort order by key word, default is ascending, descending order if you want, just add desc, order by its syntax is followed by the field to be sorted.

  • select* from 表名 order by 列名;, Sorted in ascending order by the column.
  • select* from 表名 order by 列名 desc;, Descending order through the column.
  • select* from 表名 limit offset,pagesize;, Query index starting offset (the first data index is 0), Per pagesize elements.
  • select* from user limit 0,10;Query the user data table, the display starts from the first user, the display page 10.
  • select* from user limit 10,10;, Check out the list of user data, from the beginning to show the first 10 users per page 10.
  • The first page 0,10
  • The second page 10, 10
  • The third page 20, 10
  • Page n (n-1) * 10,10

United-table query

Is associated keyword query table join, if it is necessary to determine the conditions on the Join (judgment condition added later on), which generally occur in pairs of two, here to explain to connect the two tables given first two table, students are student achievement tables and result tables.
Here Insert Picture Description
Here Insert Picture Description
First, we explain the connection within the (inner join, also known as join), is the most commonly used as a contingency table query, the so-called inner join, that is, when we query the student's name and achievements, we need to use student achievement student tables and result table, while inner join result found is that students have a table in the student but also the corresponding transcript of the student's performance, satisfy this condition will be checked out.
Here Insert Picture Description
Presentation inquiry following the student's name and achievements, we will see the names and corresponding results are, no students do not score, no results appear not students.
Here Insert Picture Description
An outer connector includes an outer left and right outer connector, also referred to as left and right connections. The so-called left join (left join), which is connected to the foundation, including, again left the table all the information is printed out.
Here Insert Picture Description
Or to check students' names and grades, for example, the connection will not result left the names printed out (when the premise of the former student table, which is on the left), presentation below.
Here Insert Picture Description
Right connection (right join) connected with left almost, that is connected on the basis of inside information to the right of the table printed out.
Here Insert Picture Description
Or to check students' names and grades, for example, the right connection will not name the results printed out (when provided in the table after the result, that is, on the right), demonstrate below.
Here Insert Picture Description

Published 464 original articles · won praise 317 · views 80000 +

Guess you like

Origin blog.csdn.net/HeZhiYing_/article/details/105323746