mysql constraint class notes

Today, content #
    
    1. DQL: query
        1. Query sort
        2. Aggregate function
        3. query packet
        4. Query tab

    2. Constraints
    3. Relationship between Multi Table
    4. Pattern
    backup and restore database 5.

# DQL: Query statement
    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: Calculation 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. the IFNULL function

    3. grouped query:
        1. Syntax: group by grouping field;
        2. Note:
            Field 1. After the query packet: packet fields, aggregate functions
            2 the difference of having and 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 male and female students average score of

            the SELECT Sex, AVG (the Math) the GROUP BY the FROM Student Sex;
            
            - grouped according to gender. Queries were male and female students average number of
            
            SELECT sex, AVG (math), COUNT (id) FROM student GROUP BY sex;
            
            - grouped by 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 number of the HAVING> 2;


            
    4. paging query
        1. syntax: limit start index, the number of queries per page;
        2. formula: start index = (the current page number - 1) * Showing Article number
            - page shows three records

            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 "dialects"


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

    * non-empty constraint: not null, null value can not be
        added when the table 1. Create constraints
            the cREATE tABLE STU (
                the above mentioned id INT,
                NAME VARCHAR (20) the NOT NULL - name is non-empty
            );
        after 2. create a table, add a non-empty constraint
            the ALTER tABLE STU MODIFY NAME VARCHAR (20) the NOT NULL;

        3. delete name non-null constraint
            the ALTER tABLE STU the MODIFY NAME VARCHAR (20 is);
    
    
    * the only constraint: unique, not repeated value
        1. when creating the table, add a unique constraint
            CREATE tABLE stu (
                the INT ID,
                PHONE_NUMBER VARCHAR (20 is) UNIQUE - a uniqueness constraint
            
            );
            * Note mysql, the values in the column may define a unique constraint plurality null
        
        
        2. The only constraint delete
        
            the ALTER TABLE STU the DROP the INDEX PHONE_NUMBER;
        
        3. Create after the table, the only constraint is added
            the ALTER tABLE STU the MODIFY PHONE_NUMBER VARCHAR (20 is) uNIQUE;
    
    * primary key constraint: primary key.
        1. Note:
            1. The meaning: The only non-empty and
            2. A table can have only one primary key field
            3. The primary key is to uniquely identify records in the table

        2. When you create a table, add a primary key constraint
            the Create the Table STU (
                the above mentioned id int primary key, - adding to the primary key constraint id
                name VARCHAR (20 is)
            );

        3. Drop primary
            - Modify Error STU ID int Table ALTER;
            the ALTER TABLE STU the DROP a PRIMARY KEY;

        4. After creating the table, add the primary key
            the ALTER TABLE STU the MODIFY ID a PRIMARY KEY the INT;

        5. The automatic growth:
            1. Concept: if the column is numeric type using auto_increment may be done worth grow automatically

            2. when creating the table, add a primary key constraint, and complete the main key from growth
            Create table STU (
                id int primary key auto_increment, - to id add a primary key constraint
                name VARCHAR (20 is)
            );

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


    * foreign key constraint: foreign key, so that the table is generated in the relationship table, thus ensuring the accuracy of the data.
        1. When you create a table, you can add foreign keys
            * Syntax:
                Create table Table (
                    ....
                    foreign key column,
                    constraint name of the foreign key foreign key (foreign key column name) references the name of the primary table (the primary table column names)
                );
                Note: when the foreign key Finally, the outer key alone as a former one to add "."

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

        3. After creating the table, add foreign keys
            ALTER TABLE table name ADD CONSTRAINT foreign key name FOREIGN KEY (foreign key field name) REFERENCES main table name (the primary table column name);
        
        
        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) ON UPDATE cASCADE ON DELETE cASCADE;
            2. Category:
                1. Cascade Update: ON UPDATE CASCADE
                2. cascading deletes: ON DELETE CASCADE
    

design database ## of

    the relationship between 1. Multi-Table
        1. Classification:
            1. One to One (understand):
                * such as: human identity certificate
                * analysis: a person is only an ID card, an ID card corresponds to only one person
            2. to-many (many):
                * such as: departments and employees
                * analyze: a department has more staff, a staff only corresponds to a division of
            3-many:
                * such as: students and courses
                * analysis: students can choose a lot of courses, courses may also be a choice of many students
        2. To achieve the relationship:
            1-to-many (many):
                * such as: departments and employees
                * implementation: the establishment of a foreign key in a multi-party, pointing to a party primary key.
            2. to-many:
                * Such as: students and courses
                * Ways: many relationships need the help implement the third intermediate table. Intermediate table comprising at least two fields, the two fields as a foreign key in the third table, respectively, to the primary key table two
            3-one (Learn):
                * eg: people ID
                * implementation: a pair of achieve a relationship, you can add foreign key refers unique primary key in any other one.

        3. Case
            - create tours classification tab_category
            - cid tours classification primary key, automatic growth
            - cname Tourism category name is not empty, the only, the string 100
            the CREATE TABLE tab_category (
                cid INT AUTO_INCREMENT PRIMARY KEY,
                CNAME VARCHAR (100 ) the NOT NULL uNIQUE
            );
            
            - create tours table tab_route
            / *
            RID tour primary key, automatic increase
            rname tours name is not empty, the only, the string 100
            . price price
            rdate Added time, date, type
            cid foreign key, Category
            * /
            the CREATE TABLE tab_route (
                RID the INT a PRIMARY KEY the AUTO_INCREMENT,
                RNAME VARCHAR (100) the NOT NULL UNIQUE,
                . price DOUBLE,
                the rdate DATE,
                cid the INT,
                a FOREIGN KEY (cid) the REFERENCES tab_category (cid)
            );
            
            / * create a user table tab_user
            uid user primary keys, auto increment
            username user name length 100, a unique, non-empty
            password password length 30, a non-empty
            name real name length 100
            birthday birthday
            sex gender, fixed-length string 1
            Telephone phone number, string 11
            Email mailbox, string length 100
            * /
            The CREATE TABLE tab_user (
                UID the INT a PRIMARY KEY the AUTO_INCREMENT,
                username VARCHAR (100) UNIQUE the NOT NULL,
                PASSWORD VARCHAR (30) the NOT NULL,
                NAME VARCHAR (100),
                Birthday DATE,
                Sex CHAR (. 1) the DEFAULT 'M',
                Telephone VARCHAR (. 11),
                In Email VARCHAR (100)
            );
            
            / *
            Create a collection table tab_favorite
            rid tours id, foreign key
            date collection time
            uid user id, foreign keys
            rid and uid can not be repeated, provided composite primary key, the same user can not collection same line two
            * /
            the CREATE TABLE tab_favorite (
                rid INT, - Line ID
                DATE DATETIME,
                UID the INT, - User ID
                - Create composite primary key
                PRIMARY KEY (rid, uid), - the primary key
                a FOREIGN KEY (RID) the REFERENCES tab_route (RID),
                a FOREIGN KEY (UID) tab_user the REFERENCES (uid)
            );

        
    2. database design paradigm
        * concept: Some specifications when designing a database, you need to follow. To follow the paradigm behind the requirements, it must comply with all the requirements of paradigm front

            when designing relational databases, to comply with different regulatory requirements, design a reasonable relational databases, these different regulatory requirements to be called a different paradigm, presenting various paradigms subnormal, the smaller the higher the paradigm database redundancy.
            Currently there are six relational database paradigm: First Normal Form (1NF), second normal form (2NF), Third Normal Form (3NF), Bath - Cape Cod Paradigm (BCNF), fourth normal form (4NF) and fifth paradigm ( 5NF, also known as the perfect paradigm).

        * Classification:
            1. The first paradigm (1NF): each column is indivisible atomic data items
            2. The second paradigm (2NF): on the basis of 1NF, non-code must be entirely dependent on the property code (to eliminate non-primary property 1NF functional dependency based on a portion of the primary key)
                * Several concepts:
                    1. The functional dependency: a -> B, if the a attribute (set) value, to determine the value of the unique properties of B. A called B depends on
                        , for example: school -> name. (Student number, course name) -> fraction
                    2. Fully functional dependency: A -> B, if A is a group attribute, B attribute worthy of determining the need to rely on all attribute values A set of attributes.
                        E.g. :( student number, course name) -> fractional
                    3. Some functional dependency: A -> B, if A is a group attribute, B attribute worthy of determining the need to rely on only some of the group attribute value to A .
                        E.g. :( student number, course name) -> Name
                    4. dependent transfer function: A -> B, B - > C by the A value if the attribute (group), to determine the value of the unique properties of B. B value can be determined by the attribute (group) C value unique attribute, called transfer function depends on C a
                        , for example: school -> department name, department name -> Head
                    5. code: If a tables, one attribute or group, are totally dependent on all other attributes, the called code attribute (group) for the table
                        For example: The code table :( student number, course name)
                        * Main properties: Code attribute group all attributes
                        * non-primary attributes: Attribute except through the code attribute group
                        
            3. The third paradigm (3NF): on the basis of 2NF on any non-primary property is not dependent on other non-primary attributes (elimination of transitive dependencies in 2NF basis)


backup and restore of the database ##

    . command:
        * syntax:
            * backup: mysqldump -u username -p password database name > saved path
            * restore:
                1. Log database
                2. create database
                3. database
                4. executable file. source file path
    2. Graphical tools:

Guess you like

Origin www.cnblogs.com/lsswudi/p/11488427.html