python3 notes twenty-four: Mysql database operations command

One: Learn

  • Mysql operation command: start the service, stop the service, connect to the database, exit the database, view the version displays the current time, remote connection
  • Database operations command: create a database, delete the database, switch the database to see if the currently selected database
  • Table Operations Command: View current database of all the tables, create table, drop table to view the table structure, see the construction of the table statement, rename the table name, modify table structure
  • Data manipulation commands: increase data (full column insert, inserted by default, while inserting pieces of data), delete data, modify data
  • Check the data manipulation commands: query data, eliminate duplicate rows query, the query conditions, aggregate queries, the query packet, sorting query, the query pagination
  • Data associated with the command: the connector, connecting the left and right connection

 

Two: Mysql operation command

  • Start Service

1. Description: Run as administrator cmd
2. Format: net start service name
3. Example: net start MySQL
4. Example: net start mysql57

 

  • Out of service

1. Description: Run as administrator cmd
2. Format: net stop service name
3. Example: net stop MySQL
4. Example: net stop mysql57

 

  • Connect to the database

1. Note: You can use the administrator, the administrator may not be used
2. Format: mysql -u username -p
3. Example: mysql -u root -p
password (set during installation)
4. Note: the connection is unsuccessful suggesting no authority, you can execute the following command:
the ALTER the USER 'root' @ 'localhost' IDENTIFIED BY the WITH mysql_native_password '123456';
flush privileges;

 

  • Exit Database

Example 1: exit or quit

 

  • View version

1. Description: Performs the connection to the database
2. Example: select version ();

 

  • Displays the current time

1. Description: Performs the connection to the database
2. Example: select now ();

 

  • Remote Connection

1. Description: The database connection to others, non-local
2. Format: mysql -h ip address -u root -p
enter each other's mysql password

 

III: database operations command

  • Create a database

1. Description: After executing example, appear OK, the instructions for creating a successful library
2. Format: create database database name = utf8 charset;
3. Example: create database test charset = utf8;

 

  • Delete Database

1. Description: After executing example, appear OK, the instructions to remove the library successfully
2. Format: drop database database name;
3. Example: drop database test;

 

  • Switching Database

1. Description: After execution examples, there Database changed, the library described handover success
2. Format: use database name;
3. Example: use Test;

 

  • View the currently selected database

1. Description: After the execution example, the name of the currently selected database appears
2. Example: select database ();

 

Four: Table Operations Command

  • View current database of all the tables

1. Description: This command must be executed first in a database
2. Example: show tables;

  • Create a table

1. Description: created under the existing test database tables, auto_increment show from growth, primary key indicates that the main key, not null is not empty, after the execution example, appear OK, the instructions for creating successful table
2. Format: create table table name ( row and type);
3. example:
Create Table Student (
ID int Primary Key AUTO_INCREMENT,
name VARCHAR (20 is) Not null,
Age int Not null,
Gender 'bit default (. 1),
address VARCHAR (20 is),
isDelete 0' bit default
) ;

 

  • Delete table

1. Description: After executing example, appear OK, the instructions to remove the table successfully
2. Format: drop table table name;
3. Example: drop table student;

 

  • View table structure

1. Description: After the execution example, the structure of table fields appear
2. Format: desc name;
3. Example: desc student;

 

  • View Jian table statement

1. Description: After executing example, appear statement creates a table
2. Format: show create table table name;
3. Example: show create table student;

 

  • Rename the table name

1. Description: After the execution example, the OK occurs, successfully renamed description table
2. Format: rename table to the new original table name;
3. Example: rename table car to newCar;

 

  • Modify table structure - action is not recommended

1. Description: After executing example, appears OK, explain successfully modified table structure, it is recommended not arbitrarily modify table structure.
2. Format: alter table table add | change | drop listed types;
3. Example: alter table newCar add isDelete bit default 0;

 

Five: data manipulation commands

  • Increased data - full column insert

1. Description: primary key column is automatically increased, but at full row insertion required mass, usually 0 , after successful insertion subject to actual data, the performance of the example, the OK appears, the data is successfully inserted explained
2. Format: insert into table values (...);
3. example: INSERT into Student values ( 0 , "tom", 19,1, "Beijing", 0);

 

  • Increased data - inserted by default

1. Description: After the execution example, the OK appears, the data is successfully inserted explained
2. Format: insert into table (column 1, column 2, ...) values (value 1, value 2, ...);
3. Example : insert into student (name, age , address) values ( "lilei", 19, " Shanghai");

 

  • Increasing the data - while inserting a plurality of data

1. Description: After the execution example, the OK appears, the data is successfully inserted explained
2. Format: insert into table values (...), (...), ...;
3. Example: insert into student values (0 , "test01", 18,0, "Beijing", 0), (0, "test02", 22,1, " Hainan", 0), (0, "test03", 20,0, " Shijiazhuang", 0 );

 

  • delete data

1. Description: After the execution example, the OK appears, the instructions to remove the data successfully
2. Format: delete from table where conditions;
3. Example: delete from where ID = Student. 5;
4. Note: there are no conditions is to delete all, caution

 

  • change the data

1. Description: After the execution example, the OK appears, modify the data successfully explained
2. Format: update table set value = 1. Column 1 Column 2 = value 2, ... WHERE condition;
3. Example: update student set age = WHERE ID = 2 16;
4. Note: not all of the columns that are modified, caution


Six : data manipulation commands check

  • Query data - basic grammar

1. Description: All data lookup table;
  from behind the key is the table name, represents the data from this table;
  later select to write the column names in the table, if it is shown in the table * represents the focus of all the columns in the result;
  in column name select the latter part, may be used as a column name aliases, the alias in the result displayed;
  if you want to query a plurality of columns, separated by commas
2. format: select * from table;
3. example: select from Student *;
4. example: SELECT name, from Student Age;
5. The exemplary: select name as n from student;

 

  • Query data - Eliminate duplicate rows

1. Description: behind the front column select distinct use eliminates duplicate rows
2. Format: select distinct from table column names;
3. Example: select distinct gender from student;

 

  • Data Query - condition inquiry

1. Description: All data query table
2. Format: select * from table where conditions;
3. exemplary comparison operators
  equal to: select * from student where id = 1;
  greater than: select * from student where id> 2;
  less than: select * from student where id < 4;
  than or equal to: select * from student where id> = 2;
  less: select * from student where id < = 4;
  does not mean:! select * from student where id = 1;
  is not equal to: SELECT * WHERE from Student ID <>. 1;
4. exemplary logical operators
  and: SELECT * WHERE ID from Student>. 1 and Gender = 0;
  or: SELECT * WHERE ID from Student> or ID 2 <. 6;
  not : SELECT * WHERE from Student ID = Not. 4;
5. The exemplary fuzzy matching
  like: SELECT * from Student WHERE name like '% Test';
    SELECT * from Student WHERE name like 'with test_';
  or: SELECT * WHERE ID from Student> or ID 2 <. 6;
  Not: SELECT * WHERE from Student ID = Not. 4;
6. The range query
  in: indicates a non-contiguous range, select * from student where id in ( 1, 2,4);
  BETWEEN ... and ...: indicates a non-contiguous range, SELECT * WHERE from Student ID BETWEEN 2 and. 4;
7. the empty Analyzing
  NOTE: null and "" are different
  Analyzing empty : is null, select * from student where address is null;
  Analyzing non-empty: IS Not null, SELECT * WHERE address from Student IS Not null;
8. The priority
  descending order: parentheses, not, comparison operators, logic Operators
  and higher than or priority, and if desired to occur simultaneously or operator, requires a combination of parentheses () is used

 

  • Query data - aggregation

1. Description: In order to quickly obtain statistical data, aggregate functions provided 5

2. Format: select a function from table where the polymerization conditions;

3.count (*), represents the total number of rows, can be written in parentheses * and column names, select count (*) from student;

4.max (column), find the maximum value of this column indicates, select max (id) from student where gender = 0;

5.min (column), this column indicates minimization, select min (id) from student where gender = 0;

6.sum (column), and this column represents find, select sum (age) from student where gender = 0;

7.avg (column), this column indicates averaging, select avg (age) from student where gender = 0;

 

  • Query data - packets

1. Description: field of the packet according to, this field indicates the same data is put into a collection.
    After the packet data can query the same column, the column for the data can not be displayed difference in the result
    can statistics data packets, do aggregation operation

2. Format: select row 1, column 2, aggregate function from table group by the column 1, row 2, row 3, ...;

3.示例:select gender,count(*) from student group by gender;

4. Format: select row 1, column 2, aggregate function from table group by the column 1, row 2, row 3, ... having column 1, column 2, ... aggregate functions;

5.示例:select gender,count(*) from student group by gender having gender=1;

The difference between the having 6.where:
  WHERE from behind is specified table filter, filtering of the original data belonging
  having group by results is screened

  • Query data - Sort

1. Description: The data sorted by column 1, if a certain value of the same column, the column is sorted by 2, and so on
    by default in ascending order of sorting, asc ascending, descending desc
2. Format: select * from table order by columns 1 asc | desc, ASC column 2 | desc, ...;
3. example: select * from student where isDelete = 0 order by Age desc;
4. example: select * from student where isDelete = 0 order by age asc, id desc ;

  • Data Query - Page

1. Description: start index starts from 0, gender = 1 for girls as boys 0
2. Format: select * from table Start limit, COUNT;
3. Example: select * from student limit 1,3; Rule # 2 from 3 starts out query
3. example: select * from student where gender = 1 limit 1,3; # 2 is started from the query article 3

 

Seven: data associated command

  • Table association

1.建表语句 1:n
--班级表 1
create table class(
id int auto_increment primary key,
name varchar(20) not null,
strNum int not null
);

--学生表 n
create table students(
id int auto_increment primary key,
name varchar(20) not null,
gender int default 1,
classid int not null,
foreign key(classid) references class(id)
);

2. Insert Data

Class Data: insert into class values (0, "python01", 55), (0, "python02", 50), (0, "python03", 60), (0, "python04", 80);
student data: Students. INTO values INSERT (0, "Tom", 1,1);
             INSERT INTO Students. values (0, "Lilei", 1,2);

3. classification query
first: the connection: table A inner join table B - - Description: table a and table B match line will appear in the result set
example: select students.name, class.name from class inner join students on class.id = students.classid;

second: left connecting: table a left join table B - Description: table a and table B match line will appear in the result set, plus unique data in table a, are not filled with null data corresponding to
the example: select students.name, class.name from class left join students on class.id = students.classid;

third: Right connection: tABLE a right join table B - Description: table a and table B match line will appear in the result set, plus unique data in table B not filled with null data corresponding to
the example: select students.name, class.name from class right join students on class.id = students.ClassID;

4. the connections, connecting the left and right connecting distinction

En:
Table A inner join table B on, a combination of two records in the table, associated field returns records matching, i.e. return to the intersection of two tables (shaded) part

 

Left connection:
Table A left join table B on, the left outer join is short, its full name is left outer connector, an external connections
Left (outside) connection, the left table (Table A) records will be fully represented, while the right table (Table B) will only show records that match the search criteria. Right table (Table B) is less than the recorded place are NULL

 

The right connection

TABLE A right join table B on, is short for right outer join, right outer Its full name is connected to an external connection
The right (outside) connection, the right table (Table B) will record all of that out, and left the table (Table A) will only show records that match the search criteria. Left table (Table A) is less than the recorded place are NULL

Guess you like

Origin www.cnblogs.com/miaomiaokaixin/p/11512002.html