MySQL brief and basic use

The basic idea of ​​the database

  1. English word database: DataBase abbreviation: DB

  2. What database?
    * For warehouse storage and management of data.

  3. Database Features:
    1. persistent storage of data. In fact, the database is a file system
    2. convenience store and manage data
    3. Use a unified approach to operational database - SQL

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

MySQL database software

  1. Installation
    * see "MySQL basis .pdf"

  2. Uninstall
    the installation directory 1. mysql to find my.ini file
    * Copy = datadir "C: / ProgramData / MySQL / MySQL Server 5.5 / the Data /"
    2. Uninstall MySQL
    3. To delete C: / MySQL files in the ProgramData directory folder.

  3. Configuration

  • MySQL service start
    1. Manually.
    2. cmd-> services.msc open window services
    3. Use Administrator cmd to open
      • net start mysql: mysql start service

      • net stop mysql: mysql shut down service

      • MySQL Login

        1. mysql -uroot -p password
        2. mysql -hip -uroot -p password link destination
        3. mysql --host = ip --user = root --password = password connection target
      • MySQL exit

        1. exit
        2. quit
      • MySQL directory structure

        1. MySQL installation directory: basedir = "D: / develop / MySQL /"
          • Profile 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
is actually defines all the rules relational database operations. Existence of each database operation is not the same place, called "dialects."

2.SQL universal grammar

  1. SQL statements can be single or multiple rows of writing, end with a semicolon.
  2. You can use spaces to indent and enhance the readability of the statement.
  3. MySQL database SQL statements are not case sensitive, use uppercase keyword suggestions.
  4. Three kinds of comments
    • Single-line comments: - Annotation content annotation content or # (mysql-specific)
    • Multi-line comments: / * comment * /

3. SQL classification

  1. DDL (Data Definition Language) data definition language
    * is used to define the database objects: databases, tables, columns and the like. Keywords: create, drop, alter, etc.
  2. DML (Data Manipulation Language) data manipulation language
    * to the data tables in the database additions and deletions. Keywords: insert, delete, update, etc.
  3. DQL (Data Query Language) data query language
    * used to query records (data) tables in the database. Keywords: select, where, etc.
  4. DCL (Data Control Language) Data Control Language (understanding)
    * is used to define the level of access and security of the database, and create a user. Keywords: GRANT, REVOKE, etc.

DDL: operation of the database, table

1. The operation of the database: CRUD

  1. C (Create): Creating
    • Create the database:
      * the Create Database database name;

    • Create a database, judgment does not exist, then create:
      * not the Create Database IF EXISTS database name;

    • Create a database and specify the character set
      * create database database name of the character set and character set;

    • Exercise:
      * Create db4 database to determine whether there is, and to develop character set is GBK
      not the SET GBK Character db4 EXISTS * IF the Create Database;

  2. R (Retrieve): inquiry
    • All queries the database name:
      * Show Databases;
    • Query a database character sets: a database query creation statement
      * show create database database name;
  3. U (Update): Modify
    • Character set to modify the database
      * alter database database name of the character set the character set names;
  4. D (Delete): Delete
    • Delete the database
      * drop database database name;
    • Judgment database exists, there is then deleted
      * drop database if exists database name;
  5. Use Database
    • Query the database name is currently in use
      * select database ();
    • Use Database
      * use the database name;

2. Operating Table

  1. C (Create): Creating
    1. Syntax:
      Create table Table (
      Column name Data type 1,
      the column 2 data type name 2,
      ...
      the column name Data type n n
      );
      * Note: The last column, no comma (,)
    2. Database type:
      1. int: integer type
        * age int,
      2. double: decimal type
        * score double (5,2)
      3. date: date, containing only the date, yyyy-MM-dd
      4. datetime: date, time of year, month, day, hour comprising yyyy-MM-dd HH: mm: ss
      5. timestamp: Time wrong type contains the minutes and seconds yyyy-MM-dd HH when the date: mm: SS
        * If in the future not to the field assignment, or assignment is null, the current system time by default, automatically assign
      6. varchar: String
        * name varchar (20): name up to 20 characters
        * zhangsan 8 characters seating two characters
    3. 创建表
      create table student(
      id int,
      name varchar(32),
      age int ,
      score double(4,1),
      birthday date,
      insert_time timestamp
      );
    4. Copy the table:
      • create table like the table name table name to be copied;
  2. R (Retrieve): inquiry
    • Query a database of all the table names
      * show tables;
    • Lookup table structure
      * desc table;
  3. U (Update): Modify
    1. Modify the table name
      * alter table table name rename to new table name;
    2. Modify the table character set
      * alter table table name character set the character set name;
    3. Add a
      * alter table name table add column data type;
    4. Column Name Type modification
      * alter table change table column names new column not new data types;
      * alter table column name table modify the new data type;
    5. Remove Columns
      * alter table table name drop column names;
  4. D (Delete): Delete
    * drop table table name;
    * drop table IF EXISTS table;
  • The client graphical tools: SQLYog

DML: deletions in the data table changes

  1. Adding Data:
    * Syntax:
    * INSERT INTO table name (column names 1, 2 column names, column names ... n) values (value 1, value 2, ... n-value);
    * Note:
    1. The column names and values correspond to .
    2. If the table name, column name is not defined, the default value is added to all columns
    insert into table values (value 1, value 2, ... value n-);
    3. In addition to the digital type, other types require the use of quotation marks (single and double can) cause to
  2. To delete data:
    * Syntax:
    * the Delete from table [where condition]
    * Note :
    1. If without conditions, then delete all records in the table.
    2. If you want to delete all records
    * delete from table name; - not recommended. How many records how many times will delete operations performed
    * TRUNCATE TABLE table name; - recommended to use, more efficient to delete the table, and then create a different table.
  3. Modify data:
    * Syntax:
    * Update table column names set value 1 = 1, 2 column name = value 2, ... [WHERE condition];
    * NOTE :
    1. If conditions without any, will be all in all records in the table modify.

DQL: record lookup table

  • select * from 表名;
  1. Syntax:
    Selec field list
    from table listing
    the conditions where the list
    group by grouping field
    conditions after having packet
    order by sorting
    limit defined tab

  2. Underlying query
    multiple fields of inquiry 1.
    select field name 1, field name 2 ... from table name;
    * Note:
    * If the query all the fields, you can use * instead of the field list.
    2. deduplication:
    * DISTINCT
    3. computed columns
    * using the four arithmetic operations can be generally a series of calculated value. (Usually only for numerical calculation)
    * IFNULL (Expression 1, Expression 2): null involved in operation, the results are null
    1. Expression 1: which field determines whether the required null
    2. If the field replace the value is null.
    4. aliases:
    * AS: AS can be omitted

  3. Conditions inquiry

    1. where clause followed by the condition

    2. Operator
      *>, <, <=,> =, =, <>
      * the BETWEEN ... the AND
      * the IN (set)
      * the LIKE: Fuzzy queries
      * placeholders:
      * _: any single character
      *%: any number of characters
      * NULL IS
      * and or &&
      * or or ||
      * or not

    3. Exercise
      * Query older than 20 years old

       	SELECT * FROM student WHERE age > 20;
       	
       	SELECT * FROM student WHERE age >= 20;
       	
       	-- 查询年龄等于20岁
       	SELECT * FROM student WHERE age = 20;
       	
       	-- 查询年龄不等于20岁
       	SELECT * FROM student WHERE age != 20;
       	SELECT * FROM student WHERE age <> 20;
       	
       	-- 查询年龄大于等于20 小于等于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;
       	
       	-- 查询年龄22岁,18岁,25岁的信息
       	SELECT * FROM student WHERE age = 22 OR age = 18 OR age = 25
       	SELECT * FROM student WHERE age IN (22,18,25);
       	
       	-- 查询英语成绩为null
       	SELECT * FROM student WHERE english = NULL; -- 不对的。null值不能使用 = (!=) 判断
       	
       	SELECT * FROM student WHERE english IS NULL;
       	
       	-- 查询英语成绩不为null
       	SELECT * FROM student WHERE english  IS NOT NULL;
      
       	-- 查询姓马的有哪些? like
       	SELECT * FROM student WHERE NAME LIKE '马%';
       	-- 查询姓名第二个字是化的人
       	
       	SELECT * FROM student WHERE NAME LIKE "_化%";
       	
       	-- 查询姓名是3个字的人
       	SELECT * FROM student WHERE NAME LIKE '___';
       	
       	
       	-- 查询姓名中包含德的人
       	SELECT * FROM student WHERE NAME LIKE '%德%';
      
Released three original articles · won praise 0 · Views 26

Guess you like

Origin blog.csdn.net/loulan_lu/article/details/105059360