1. MySQL Basics

1. Basic concepts of database

2. MySQL database software
    1. Install
    2. Uninstall
    3. Configure

3. SQL


## Basic concepts of
    database 1. English word of database: DataBase Abbreviation: DB
    2. What database?
        * A warehouse for storing and managing data.

    3. The characteristics of the database:
        1. Persistent storage of data. In fact, the database is a file system
        2. It is convenient to store and manage data
        3. The database is operated in a unified way--SQL

    
    4. Common database software
        * See "MySQL Basics.pdf"


# MySQL database software
    1. Installation
        * See "MySQL Basics.pdf"
    2. Uninstall
        1. Go to the mysql installation directory to find the my.ini file
            * Copy datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/"
        2 . Uninstall MySQL
        3. Delete the MySQL folder in the C:/ProgramData directory.
        
    3. Configure
        * MySQL service to start
            1. Manually.
            2. cmd--> services.msc to open the service window
            3. Use administrator to open cmd
                * net start mysql : start mysql service
                * net stop mysql: close mysql service
        * MySQL login
            1. mysql -uroot -p password
            2. mysql -hip -uroot -p connection target password
            3. mysql --host=ip --user=root --password=connection target password
        * MySQL exit
            1. exit
            2. quit
    
        * MySQL directory structure
            1. MySQL installation directory: basedir="D:/develop/MySQL/"
                * Configuration file my.ini
            2. MySQL data directory: datadir="C:/ProgramData/ MySQL/MySQL Server 5.5/Data/"
                * several concepts
                    * database: folder
                    * table: file
                    * data: data

# SQL

    1. What is SQL?
        Structured Query Language: Structured Query Language
        actually defines the rules for operating all relational databases. There are differences in the way each database operates, called "dialects".
        
    2. SQL general syntax
        1. SQL statements can be written on a single line or multiple lines, ending with a semicolon.
        2. Use spaces and indentation to enhance the readability of statements.
        3. The SQL statement of the MySQL database is not case-sensitive, and it is recommended to use uppercase for keywords.
        4. 3 kinds of comments
            * Single-line comment: -- comment content or # comment content (mysql-specific) 
            * Multi-line comment: /* Comment*/
        
    3. SQL classification
        1) DDL (Data Definition Language) data definition language is
            used to define the database Objects: databases, tables, columns, etc. Keywords: create, drop, alter, etc.
        2) DML (Data Manipulation Language) data manipulation language is
            used to add, delete, and modify the data in the tables in the database. Keywords: insert, delete, update, etc.
        3) DQL (Data Query Language) data query language
            Used to query the records (data) of the table in the database. Keywords: select, where, etc.
        4) DCL (Data Control Language) data control language (understanding) is
            used to define the access authority and security level of the database, and to create users. Keywords: GRANT, REVOKE, etc.

## DDL: operation database, table

    1. Operating the database: CRUD
        1. C(Create): Create
            * Create a database:
                * create database database name;
            * Create a database, judge that it does not exist, and then create it:
                * create database if not exists Database name;
            * Create a database and specify character set
                * create database database name character set character set name;

            * Exercise: Create a db4 database, determine whether it exists, and set the character set to gbk
                * create database if not exists db4 character set gbk;
        2. R(Retrieve): Query
            * Query the names of all databases:
                * show databases;
            * Query a certain Character set of a database: query the creation statement of a database
                * show create database database name;
        3. U(Update): modify
            * modify the character set of the
                database* alter database database name character set character set name;
        4. D(Delete ): delete
            * delete database
                * drop database database name;
            * judge the existence of the database, delete it
                if exists * drop database if exists database name;
        5. Use database
            * query the name of the database currently in use
                * select database();
            * use database
                * use database name;


    2. Operation table
        1. C(Create): Create
            1. Syntax:
                create table table name (
                    column name 1 data type 1,
                    column name 2 data type 2,
                    ....
                    column name n data type n
                );
                * Note: The last column, no need to add a comma (,)
                * Database type:
                    1. int: integer type
                        * age int,
                    2. double: decimal type
                        * score double(5,2)
                    3. date: date, only contains year, month and day, yyyy-MM-dd
                    4. datetime: date, including year, month, day, hour, minute, second yyyy-MM-dd HH:mm:ss
                    5. timestamp: time error type, including year, month, day, hour, minute, second yyyy-MM-dd HH:mm:ss    
                        * If this field is not assigned a value in the future, or the value is null, the current system time will be used by default to automatically assign a value

                    6. varchar: string
                        * name varchar(20): maximum name is 20 characters
                        * zhangsan 8 characters Zhang San 2 characters
                

            * Create table
                create table student(
                    id int,
                    name varchar(32),
                    age int ,
                    score double(4,1),
                    birthday date,
                    insert_time timestamp
                );
            * Copy table:
                * create table table name like copied table name;          
        2. R(Retrieve): Query
            * Query all table names in a database
                * show tables;
            * Query table structure
                * desc table name;
        3. U(Update): Modify
            1. Modify the table name
                alter table table name rename to new table name;
            2. Modify the character set of the
                table alter table table name character set character set name;
            3. Add a column
                alter table table name add column name data type;
            4. Modify column name type
                alter table table name change column name new column name new data type ;
                alter table table name modify column name new data type;
            5. delete column
                alter table table name drop column name;
        4. D(Delete): delete
            * drop table table name;
            * drop table if exists table name;

* Client graphical tool: SQLYog

## DML: Add, delete and modify the data in the table

    1. Add data:
        * Syntax:
            * insert into table name (column name 1, column name 2, ... column name n) values ​​(value 1, value 2, ... value n);
        * Note:
            1. Column The name and value must be in one-to-one correspondence.                 2. If the column name is not defined after the table name, insert into table name values ​​(value 1, value 2, ... value n) is added to             all
            columns by default ; 2.     Delete data:         * Syntax:             * delete from table name [where conditions]         * Note:             1. If no conditions are added, all records in the table will be deleted.             2. If you want to delete all records                 1. delete from table name; -- not recommended. 2. TRUNCATE                 TABLE table name; -- Recommended, it is more efficient to delete the table first, and then create the same table.     3. Modify data:         * Syntax:












            * update table name set column name 1 = value 1, column name 2 = value 2,... [where condition];

        * Note:
            1. If no conditions are added, all records in the table will be modified.

## DQL: Query records in a table
    * select * from table name;
    
    1. Syntax:
        select
            field list
        from
            table name list
        where
            condition list
        group by
            grouping field
        having
            condition after grouping
        order by
            sorting
        limit
            paging limit


    2. Basic query
        1. Query with multiple fields
            select field name 1, field name 2... from table name;
            * Note:
                * If you query all fields, you can use * to replace the field list.
        2. Remove duplicates:
            * distinct
        3. Calculated columns
            * Generally, four arithmetic operations can be used to calculate the values ​​of some columns. (Generally, only numerical calculations are performed)
            * ifnull(expression 1, expression 2): the operation involving null, the calculation results are all null
                * expression 1: which field needs to determine whether it is null
                * If the field is null the replacement value after.
        4. Aliases:
            * as: as can also be omitted
            

    3. Conditional query
        1. Where clause followed by condition
        2. Operators
            * > , < , <= , >= , = , <>
            * BETWEEN...AND  
            * IN(collection) 
            * LIKE: fuzzy query
                * placeholder :
                    * _: single arbitrary character
                    * %: multiple arbitrary characters
            * IS NULL  
            * and or &&
            * or or || 
            * not or !
            
                -- query age greater than 20

                SELECT * FROM student WHERE age > 20;
                
                SELECT * FROM student WHERE age >= 20;
                
                -- query age is equal to 20 years old
                SELECT * FROM student WHERE age = 20;
                
                -- query age is not equal to 20 years old
                SELECT * FROM student WHERE age ! = 20;
                SELECT * FROM student WHERE age <> 20;
                
                -- query age greater than or equal to 20 but less than or equal to 30
                
                SELECT * FROM student WHERE age >= 20 && age <=30;
                SELECT * FROM student WHERE age >= 20 AND age < =30;
                SELECT * FROM student WHERE age BETWEEN 20 AND 30;
                
                -- Query age 22 years old, 18 years old, 25 years old information
                SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
                SELECT * FROM student WHERE age IN (22,18,25);
                
                -- Query English grade is null
                SELECT * FROM student WHERE english = NULL; -- wrong. The null value cannot be used = (!=) Judgment
                
                SELECT * FROM student WHERE english IS NULL;
                
                -- query English result is not null
                SELECT * FROM student WHERE english IS NOT NULL;
    
                -- query which surname is Ma? like
                SELECT * FROM student WHERE NAME LIKE 'Ma%';
                -- Query the person whose second name is Hua
                
                SELECT * FROM student WHERE NAME LIKE "_hua%";
                
                -- Query the person whose name is 3 words
                SELECT * FROM student WHERE NAME LIKE '___';
                
                
                -- Query people whose name contains German
                SELECT * FROM student WHERE NAME LIKE '%德%';

    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325730604&siteId=291194637