mysql primary application and understanding

? 1 Why
    1.1 the Java code will save the data into memory
        advantages: speed
        Disadvantages: can not persist, can not save data to the hard disk
    
    1.2 java code uses IO streams to save data to the hard disk
        advantages: persisted, to save data to on the hard drive
        Cons: slow

    1.3 is necessary to speed, but also save persistence, but also safe? Database

2 what?
    Database software to store data

    Manufacturer: oracle, sql server, DB2 (IBM), mysql (the protagonist), etc.

    Download, install, start using

HOW 3?
    3.1 internal storage structure

    3.2 Operational Database
        3.2.1 increase
            1, create a name for mydb1 database.
                Database mydb1 the Create;
            2, create one using utf8 character set mydb2 database.
                the SET utf8 Character Database mydb2 the Create;
                
        3.2.2 Delete
            demand: Delete mydb1 database created earlier
                drop database mydb1;

        3.2.3 Modify
            requirements: the coded set into mydb2 GBK
                ALTER Character Database mydb2 GBK SET;

        3.2.4 Query
            1, all database queries mysql database software
                Show Databases;
            2, coding mysql database to view the
                show create database mysql;

            3 using the database query is
                SELECT Database ();
            . 4 switch database using specified mydb2
                use mydb2;
                SELECT Database ();

        3.2.5 Other

    3.3 operational data table structure
        3.3.1 increase
            demand: Create an employee table containing the employee's name, password, gender, birth date information.
            - employee employ abbreviated EMP
            Create Table EMP (
                username VARCHAR (50), - 
                password VARCHAR (50),
                Sex char (10),
                Birthday DATE
            );

            show tables; - all the query tables
            desc emp; - a lookup table structure

            Constraints: primary key, unique, not null
                create Employees Table 2: job number key from the primary shaping growth, the only not empty user name, a password is not empty, gender, birthday.

                create table emp2(
                    id int primary key auto_increment,
                    username varchar(50) unique not null,
                    password varchar(50) not null,
                    sex char(10),
                    birthday date
                );

        3.3.2 Delete
            demand: delete emp table
                drop table emp;

        3.3.3 Review
            Requirement 1: an increase in the salary column emp2 table
                alter table emp2 add salary double;

            Requirement 2: an increase in the age column emp2 table
                alter table emp2 add age int;

            Requirement 1: Modify birthday column can not be null
                Table EMP2 DATE Not Modify ALTER birthday null;
                
            Requirement 2: Modify the username column length is 60
                ALTER Modify Table EMP2 username VARCHAR (60) UNIQUE Not null;

            Requirements: Modify the column name username to name
                the Table EMP2 Change username name VARCHAR (60) UNIQUE not null the ALTER;

            Requirements: Delete the age column
                alter table emp2 drop age;

            Requirements: The emp2 table name changed to the person table
                rename table emp2 to person;

            Demand 7: emp encoding a modified UTF8
                ALTER Character Table emp SET UTF8;

        3.3.4 query
            demand: View Employees table structure
                desc emp; - a lookup table structure

                Requirement 1: View all the tables in the current database.
                    show tables;

                Requirement 2: View person table structure.
                    desc person;

                Demand 3: View encoded person table.
                    show create table person;

        3.3.5 Other

    3.4 the operation content data table
        3.4.1 increased
            1, all columns in the table to insert data
                INSERT INTO Person (ID, name, password, Sex, Birthday, the salary)
                values (1, 'zhangsan', '123', 'NaN3 ',' 1985-8-8 ', 15000);
                
                 SELECT * from Person; - within the data look-up table
            2, to simplify the insertion data to all columns in the table: All columns, column order and the table structure must be consistent
                iNSERT INTO Person
                values (null, 'Lisi', '123', 'NV', '1990-9-9', 18000);
                
            . 3, to the table is not empty column insert data
                iNSERT INTO Person (name, password, Birthday)
                values ( 'wangwu', '123', '2000-9-10');

        3.4.2 Remove
            1, delete the person table name is recorded as user lisi
                Delete from person
                WHERE name = 'lisi';
            2, delete all records in the table
                delete from person; - Progressive deleted

                truncate person; - all deleted table, and then create a new table based sql statement

        3.4.3 Modify
            1, modify all user password person table is abcdef
                Update Person SET password = 'abcdef';

            2, Edit name for this user pay zhangsan of 88888
                Update Person SET the salary = 88888 WHERE name = 'zhangsan';
            3, the id of the user 3's name and password modify zhaoliu
                Update Person 
                    SET name = 'zhaoliu', password = 'zhaoliu'
                    WHERE ID =. 3;

            4, the user id for the user to modify the name of 3 Chinese Zhao six;
                Update Person 
                    SET name = 'Zhao six'
                    WHERE id = 3;


        3.4.4 query
            demand: query all the information students.
                select * from student;

            Requirements: query all the student's name and achievements
                select name, score from student;
            
            demand: a lookup table is equal to 24 years older than the student information
                the SELECT * 
                from Student
                the WHERE Age> = 24;
            
            demand: Query age is not 25-year-old student.
                * the SELECT 
                from Student
                ! the WHERE Age = 25;

                * the SELECT 
                from Student
                ; the WHERE Age <> 25
            
            queries age> 23, and student achievement information> 80: demand
                the SELECT *
                from Student
                the WHERE Age> 23 and Score> 80
            
            between the query results 80 to 100 (included): Demand student information
                SELECT *
                from student
                WHERE Score> Score = 80 and <= 100;

                - simplify the way: the column names between the smaller and larger value;
                the SELECT *
                from Student
                the WHERE Score between 80 and 100;
            
            demand: Query students aged 18,23,25 information of
                the SELECT * 
                from Student
                the WHERE Age = 18 or Age = 23 or age = 25;


                * the SELECT 
                from Student
                the WHERE Age in (18, 23, 25);
            
            demand: Query contains information for all students think of
                the SELECT * 
                from Student
                the WHERE name like '% Si%';


            Requirement 1: The query is not the birthday student information
                the SELECT * 
                from Student
                the WHERE Birthday IS null;
            
            Requirement 2: Query age student information
                the SELECT * 
                from Student
                the WHERE Age IS not null;
            
            demand: Display of non-repetition of age
                the SELECT DISTINCT Age from Student;
            
            1, results of the sorted output
                - positive sequence alignment results
                SELECT * 
                from Student
                order by ASC score;

                - results in reverse order
                the SELECT * 
                from Student
                the Order by Score desc;

            2, age sorted in descending order (descending) output
                SELECT * 
                from Student
                Order by Age desc;

            3, for students aged sorted in descending order, according to the results of the same age descending
                the SELECT * 
                from Student
                the Order by Age desc, Score desc;
            
            demand 1, to age and score surnamed
                select age as the age, score as results
                from student;

            2 demand, omitted key as the query again
                select age the age of 2, score score 2
                from Student;

            Aggregate functions
                1, the total number of students in a class statistics?
                    select count (*) from student;

                2, student information query results greater than 80:
                    the SELECT * Student from
                    the WHERE Score> 80;

                3 students, more than 80 statistical results of how many?
                    COUNT SELECT (*) from Student
                    WHERE Score> 80;

                1, a statistics class results and    
                    select sum (score) from student;

                2, respectively, and the age statistics, scores and
                    select sum (age), sum ( score) from student;

                3, age and statistical achievements and values of
                    select sum (age) + sum ( score) from student; - 491.96

                    select sum(age+score) from student; --  407.96000000000004

                    - ifnull (column name, the default value) If this column is null, default value is used
                    select sum (ifnull (age, 0 ) + score) from student; - 491.96000000000004

                    - truncate (value reserved decimal places) to retain the specified number of decimal places
                    select truncate (sum (ifnull (age , 0) + score), 2) from student;

                Requirements: Seeking a class average age
                    - are not allowed in AVG
                    the SELECT SUM (Age), COUNT (*), SUM (Age) / COUNT (*) from Student; - 19.0000

                    select sum(age),count(age),sum(age)/count(age) from student; --  23.7500

                    select avg (age) from student; - 23.7500
                
                demand: seeking class highest and lowest scores
                    select max (score), min ( score) from student;
                
                groups:
                Demand 1: Find the total score of each student?
                    the SELECT name, SUM (score) - Note: reality column can be grouped columns and aggregate function
                    from Student
                    Group by name;

                Requirement 2: averaging more than 80 points a student? 
                    The SELECT name, AVG (Score)
                    from Student
                    - the WHERE AVG (Score)> 80 - this is the wrong
                    group by name;

                Why is it wrong? Because contrary to the order of execution of the mysql sql
                    from 1
                    the WHERE 2
                    Group 3 by
                    the HAVING 4
                    the Order 5 by
                    the SELECT 6

                    After the polymerization function can only use the packet, it can only occur in the 4,5,6
                
                    
                    Requirement 2: averaging more than 80 minutes students? 
                        SELECT name, AVG (Score)
                        from Student
                        Group name by
                        HAVING AVG (Score)> 80;
                
                ? 3 expansion needs: seeking boys and girls each course average
                    the SELECT course,, Sex, AVG (Score)
                    from Student
                    Group by course,, Sex;
                     
        3.4.5 other


        Backup
            1, re-open a new dos window.
            2, the mydb2 database export to a disk file .sql.
                mysqldump -u root -p mydb2> e: /dashuju10.sql

        Recovery
            1. Create heima database.
                Database Heima the Create;
            2, re-open a new dos window.
            3, to restore the backed up data mydb2 tables and table data into the heima.
                mysql -u root -p heima <e:  /dashuju10.sql

Guess you like

Origin blog.csdn.net/qq_43406741/article/details/92587129