MySQL -------> basics (b)

DQL: query

1. Sort query
  * Syntax: order by clause
    * order by sort field 1 Sort 1, Sort field 2 Sort 2 ...

  * Sort by:
    * ASC: ascending order, default.
    * DESC: descending order.

  * Note:
    * If there are a plurality of sorting criteria, the current condition value when the same side, only the second determination condition.

2. Polymerization functions: a column data as a whole, longitudinally calculations.
  1. COUNT: count the number
    1 generally selected non-null columns: primary key
    2. COUNT (*)
  2. max: maximum value calculating
  3. min: minimum value calculating
  4. sum: calculating and
  5. avg: calculating an average value

  * Note: the polymerization calculation function, excluding the null value.
    Solution:
      1. Select columns do not contain a non-null calculating
      2. IFNULL function

3. grouped query:
  1. Syntax: group by grouping field;
  2. Note:
    1. The field of inquiry after grouping: the grouping field, aggregate functions
    difference and having the 2. where?
      1. where before the packets are defined, if the condition is not satisfied, is not involved in the packet.

       HAVING defined in the following packet, if the result is not satisfied, it will not be out of the query
      determined not with 2. where the aggregation function, the function can be polymerized HAVING.

    - grouped by gender. Queries were average male and female students

    Sex the SELECT, the AVG (Math) the FROM Student the GROUP BY Sex;

    - groups according to sex. Queries were male and female students of average, the number of

    the SELECT Sex, AVG (the Math), COUNT (the above mentioned id) the GROUP BY the FROM Student Sex;

    - grouped according to gender. Queries were male and female students of average, the number of requirements: Score 70 points lower than people who do not participate in grouping
    the SELECT Sex, AVG (the Math), COUNT (the above mentioned id) the FROM the WHERE the Math Student> 70 the GROUP BY Sex;

    - by gender grouping. Queries were male and female students of average, the number of requirements: Score 70 points lower than people who do not participate in packet after packet. The number is greater than the two individuals
    the SELECT Sex, AVG (the Math), COUNT (the above mentioned id) FROM student WHERE the Math> 70 the GROUP BY Sex the HAVING COUNT (the above mentioned id)> 2;

    the SELECT Sex, AVG (the Math), COUNT (the above mentioned id) number FROM student WHERE math> 70 GROUP BY sex HAVING number> 2;

4. paging query
  1. Syntax: limit start index, the number of queries per page;
  2. Formula: Start Index = (the current page number - 1) * the number of displayed per page
    - 3 records per page

    SELECT * FROM student LIMIT 0,3; - p. 1

    the SELECT * Student the LIMIT the FROM 3,3; - Page 2

    the SELECT * Student the LIMIT the FROM 6,3; - Page 3

  3. limit is a MySQL "dialect"

constraint

* Concept: the data are defined in the table, to ensure data accuracy , effectiveness and integrity .
* Classification:
  1 primary key constraint: Primary Key
  2. non-empty constraint: Not null
  3. The only constraint: UNIQUE
  4. foreign key constraint: foreign key

 

* Primary key constraint: primary key.
  1. Note:
    1. Definition: The only non-empty and
    2. A table can have only one primary key field
    3. The primary key is unique identification recorded in the table

  2. When creating the table, add the primary key constraint
    Create Table STU (
      id int Primary Key, - to add a primary key constraint id
      name VARCHAR (20 is)
    );

  3. Delete the primary key
    - the Table STU the Modify the above mentioned id error int the ALTER;
    the ALTER TABLE DROP PRIMARY KEY STU;

  4. After creating the table, add the primary key
    ALTER TABLE stu MODIFY id INT PRIMARY KEY ;

  5. Automatic Growth:
    1. Concept: if the column is numeric type, may be accomplished using auto_increment worth autogrowth

    2. When creating the table, add a primary key constraint, and completes the auto increment primary key
      Create Table STU (
        id int Primary Key AUTO_INCREMENT, - to add a primary key constraint id
        name VARCHAR (20 is)
      );

    3. Remove the automatic growth of
      the ALTER TABLE MODIFY the above mentioned id INT STU;
    4. Add automatic growth
      ALTER TABLE stu MODIFY id INT AUTO_INCREMENT;

 

* Non-empty constraint: not null, null value can not be
  added constraint table 1. Create
    the CREATE TABLE STU (
      ID the INT,
      NAME VARCHAR (20 is) the NOT NULL - non-null name
    );
  2. After creating the table, addition of non null constraints
    ALTER TABLE stu MODIFY NAME VARCHAR (20 ) NOT NULL;

  3. Delete the name of non-empty constraint
    ALTER TABLE stu MODIFY NAME VARCHAR (20 );

 

* The only constraint: unique, not repeated value
  1. When creating the table, add a unique constraint
    the CREATE TABLE STU (
      ID the INT,
      PHONE_NUMBER VARCHAR (20 is) UNIQUE - a uniqueness constraint

    );
    * Note mysql, the only constraint defined column This value may have a plurality of null

  2. Drop the unique constraints
    the ALTER TABLE DROP INDEX STU PHONE_NUMBER;
  3. After you create a table, add a unique constraint
    ALTER TABLE stu MODIFY phone_number VARCHAR (20 ) UNIQUE;

 

* Foreign key constraint: foreign key, so that the relationship between the table and the table generation, so as to ensure the correctness of the data.
  1. When you create a table, you can add foreign key
    * Syntax:
      the Create the Table table name (
        ....
        foreign key columns of
        constraint name the foreign key foreign key (foreign key column name) references the name of the primary table (the primary table column names)
      );

  2. Remove the foreign key
    ALTER TABLE table DROP FOREIGN KEY foreign key name;

  3. After creating the table, add a foreign key
    ALTER TABLE table name ADD CONSTRAINT foreign key name FOREIGN KEY (foreign key field name) REFERENCES name master table (the primary table column names);

  4. cascade operation
    1. Add cascade operation
      Syntax: ALTER TABLE table name ADD CONSTRAINT foreign key name
          FOREIGN KEY (foreign key field name) REFERENCES name master table (the primary table column names) the ON the ON the UPDATE CASCADE the DELETE CASCADE;
      2. Classification :
        1. cascade update: ON uPDATE cASCADE
        2. cascading deletes: ON dELETE cASCADE

Guess you like

Origin www.cnblogs.com/LIAN8/p/11762518.html