The base table is connected Ⓑ MySQL, constraint, foreign key, a packet, paging, sorting, unique index

1. Important repeated: the data type of the key

   1) related to digital

      255 tinyint
      int 65535
      BIGINT 42 is billion
      decimal decimal, the string is actually stored, with a decimal precision.
      float decimal floating-point store a limited
      double double double Reserved limited decimals

   2) the relevant character

      char fixed length, speed query, save time up to 255 bytes
      varchar variable length up to 255 characters to save space
      text up text type: 65535 characters

   3) Time

      date,time,datetime,timestamp

   4) Binary:

      blob

   5) enum, set

    enum ( 'XL', 'XXL', 'XXXL')
    SET ( 'A', 'B', 'C', 'D') any combination #
 

2.sql operation

(1) Operation of the database

   1) Check Database

      show databases;

   2) Open the database:

      use (database name);

   3) Create a database

      create database test default charset utf8;

   4) delete the database

      drop databases test;

   5) Check all the tables under the database:

      show tables;
 

Operation (2) of the table

   1) Create a table

     create table student(
        id int auto_increment primary key,
        name varchar(12) not null,
        info varchar(22)) engine=innodb charset=utf8;

   2) look-up table

      select  * from student;

   3) Display:

      desc student; # table structure
      show create table class \ G; construction of the table information #

   4) Clear the table:

    Reserved increment id number: delete from student;
      not retained increment number: truncate table student;

   5) to completely delete the table:

    drop table student;

   6) Modify table

     alter table student auto_increment = 1;

(3) Operation of the line:

    CRUD:

    Student INTO INSERT (name, info) values ( 'Tom', '');
    # insert a plurality of values:
    INSERT INTO User (name, Age) values ( 'Tom', 22 is), ( 'Jim', 23 is), ( 'Tomas', 18 is), ( 'Jerry',. 19);
    # insert the temporary table entity table
    insert into t2 (name) select name from user;
    # 修改内容
      update student set name='',info='' where name = '';
      update user set age = 29 where age = 18;
    Condition # query
      select * from user where age between 18 and 22;
    # 综合查询
      select * from user where age > 17 and name like 't%';
  
      select name as s_name,age, 20 from user;
      select name as s_name,age, 'avg_20' from user;
         select * from user order by uid limit 3;
      select * from user order by age desc limit 3;
 

3, constraints and criteria query

(1) primary key constraint

 create table score(
  id int auto_increment not null,
  student_id int not null,
  class_id int not null,
  score_num tinyint,
  pimary key(id),

(2) foreign key constraint

  constraint fk_stu_cls foreign key (class_id) references class(cid)

(3) The only constraint

  unique uq_std_cls_id (student_id, class_id)
  )engine=innodb default charset=utf8

(4) self-energizing setting step

   1) modify the session, local variables, affects only the current drawing (login user), commonly used. The default is 1

      show session variables like 'auto_incre%'
      set session auto_increment = 2

   2) modify global, affecting all sessions.

      show globle variables like 'auto_incre%'
      set globle auto_increment = 2

(5) special foreign key:

   1) One to One

      Foreign key plus a unique index is one to one relationship.

   2) many

      Support comes

   3) many to many

      Have a two-way-many relationship is many to many relationships.
      In this case, for ease of operation, creates an intermediate table that records the relationship between two tables.
      Two intermediate storage table id table, establishing a foreign key constraint, if needed, you can add a unique index, two id added.

(6) conditional statement

   1) Page

      Take the first 10 rows of the result
        select * from student limit 10;
      starting from the first record, take the first 10
        select * from student limit 0,10;
      take the first
        select * from student limit 0 1;
      fetch second
        select * from student limit 1 1;

   2) wildcard

      Queries student table to name beginning with t students
        select * from student where name like ' t%';
      query to the name nd at the end of the students
        select * from student where name like ' % du'
      query by the beginning of t, two name-character
        select * from student where name like ' t_';

   3) Sort

      Default ascending order from small to large
        select * from student order by id;
      may be arranged in reverse order, i.e., in descending
        select * from stduent order by id desc ;

   4) Packet

      The default is a query packet,
      the results of the query must be a function of polymerization: sum, avg, count, max , min , etc., or a polymerization column, i.e. with the column group by, a one to one relationship.
        COUNT the SELECT (the above mentioned id) from the WHERE score_num Score> 60 Group by the above mentioned id the HAVING COUNT (the above mentioned id)> 1
      mysql5.7 version, the new sql_mode = only_full_group_by, can be closed, but not recommended!

4, the connection table

 (1) left and right connecting join

   1) connecting the inner

      There are hidden null values ​​in a row

      2) an outer connector

      Column left outer join left join the table on the left displays all of
      the right outer join All right join table on the right column

(2) upper and lower connecting union

   1) Remove duplicates

      union 

      2) not to heavy

      union all 

Guess you like

Origin www.cnblogs.com/funyou/p/12181507.html