mysql knowledge and operation

Account : mysql8 landing root account, the initial password in /var/log/mysqld.log

  Create an account: create user username @ 'owner' identified mysql_native_password by 'password';

  Authorization: grant permission on a scope to users;
  Grant * to * All privileges on Linux;.
  Grant the SELECT name on db.student to ADMIN;

 

  The right to withdraw: revoke privileges on a scope from the user;
  REVOKE the SELECT * from Students on Linux;.

  Privileges: select, insert, drop, update , grant, create, revoke, alter and so on
  . Scope: database table
  * for all

 

  Password change: mysql5.7 previously encrypted cryptographic functions: password ( 'password')

  set password = 'password'; - defaults to root account

  set password for the user name = 'password';

 

  View permissions: desc mysql.user;

  show grants for the user;

  select * from mysql.user where user = 'username' and host = 'owner' \ G;

 

Import, restore a database : 1, introduced into the tool (SQLyog): Import → execute SQL scripts (ctrl + shift + Q)

            2, introduced in mysql: mysql> source d: /db.sql;

            3, the outer introduction mysql: mysql -uroot <d: /db.sql

Export, backup database : 1, export tool (SQLyog): Export / backup → backup database dump to SQL (ctrl + shift + E)

                2, the outer mysql Export: ① deriving a plurality of databases: mysqldump -uroot --databases db1 ab2 db3> d: /db.sql

                         ② export data structure in a database with a plurality of tables: mysqldump -uroot -p db t1 t2> d: /m.sql

                      Description:> -t data preceded by a long table, the table structure as long as the add -d (operation)

      All database represented as -A

Data type :
  1, number: integer tinyint, smallint, int, bigint
        decimal decimal (p, s): p is the total character limit, s is the decimal limits such as: decimal (3,2-) 9.9

  2, String: char (18 ) 18 bytes
        varchar (50) 50-character
        text
        LONGTEXT

  3, the date and time: datetime Time and Date 15:59:30 2019-08-20
         DATE Date 2019-08-20


  4, binary
  5, composite type

       enum () enumeration: gender enum ( 'male', 'female', 'secret');
      the SET () collection: the SET ( 'the Java', 'PHP', 'Python');
          'the Java' 'PHP, Python' 'Python, PHP, Java' 'HTML, PHP'
  . 6, the JSON type (the NoSQL characteristic)

Operation : (hereinafter table can be accessed if such is determined whether conditions to: drop table if exists student;)

  Import database: source file name;

  First, the establishment of the table (to use database;)
    Create Table table (
      `` column name data type constraints,
      ...
    ) = InnoDB Engine charset = UTF8;

    such as: Create Student Table IF Not EXISTS (
      SnO AUTO_INCREMENT unsigned int Primary Key,
      VARCHAR sname (30),
      sgender enum ( 'male', 'female'),
      sbrithday datetime,
      smoney decimal (8,1),
      sdept VARCHAR (50) default 'computer science',
      sinfo text
    ) Engine InnoDB charset = utf8 AUTO_INCREMENT = = 201601;

  Second, view the data table: select * from table name;
  see table structure: desc table;
  amend Table:
  - Modify the table name
  alter table table name rename to new name;
  the rename the Table table name to the new name;

  - an increase
  alter table column name table add `` data type;
  alter table column name table add `` After the data type;
  alter table column name table add `` First data type;

  - Modify the column name and type (indispensable)
  ALTER change table column names Table names new data type;

  - delete an
  alter table table name drop `` column names;

  - drop table
  drop table table name;
  drop table IF EXISTS table;

  Third, additions and deletions (positioning the back where the index information, usually with the primary key value)

    By: an insert: insert table values (separated by a comma, with the string '', the space filling null, does not fill)
    inserting a plurality of: insert table values (), (), ();
    inserting portion Field : insert table ( `column name` `column name`) values (value, value);
    iNSERT is not inserted in said specified position of as typically the primary key is of type int and the table is the primary key arrangement, to 'designated' note serial number primary key insert strip

    delete: delete from table name WHERE `` a column name = value;

    Change: update table SET `column name` = value where a field location;
    Update table SET `column name` = value 'column name `= value where a field is positioned, with a field location;

  Fourth, check: (key, If the statement is more than can wrap distinction)

    Common Calculation: select function;
    such as: In this case: now (), to the time: date_add (now (), interval 10 day), the time to: date_add (now (), interval -10 day), h: hour (now ( )), a unique string: uuid ()

    query data: field conditions select from table view;
    alias format: alias field conditions select from table view;

    ① field of view : that in order to view the contents of the column name or function calculates and displays the form of fields, write a will display a view on multiple separated by commas. (* Represents all fields in the table)     

      1, to eliminate duplicate content viewed: distinct + field name, such as:

        select distinct address ...

      2, the general function: · meter row functions: COUNT (*)
              · int function: max (), min () , avg (), sum (), round (), floor (), abs (), ceil (), sqrt ()
              · string (time) function: char_length (), length () , now (), from_unixtime ( timestamp), unix_timestamp ( '2006-11-04 12:23:00' ), concat ( link function) , CONCAT_WS (separator written in the first place),

                        repeat (repeat function), upper (), lower (), left (string, number of access), right (str, length), mid (taken)

            As a function of these fields can not be directly used as a value, to use: as s = max (), available sub-query, using the result as a select value, such as: s = (select max (sno) ...)


      3, sub-query: will first view the result as a new enclosed table! To re-select
            as to view the highest scores of student information: select * from students where grate = (select max (grate) from student);

      4, if the function: to if () Check the return value as a result
            if (condition value 1, value 2)
            if (condition value 1, if (condition value 2, ...))
             such as:
               SELECT level, count (*) number from
               (the SELECT sname name,
                sperformance results,
                IF (sperformance> = 90, 'good', if (sperformance> = 80 , ' good', if (sperformance> = 60 , ' pass', 'make-up') )) rating
                from Students
                ) by SS Group level;

      5, sorting, ranking
        5.1, add a sort field: select row_number () over (order by column name desc) ... number from
        5.2, a new ranking field:
            skip Ranking: select rank () over (order by column name asc) from table name order by column name asc;
            sort does not skip: select dense_rank () over (order by column name desc) from table name order by column name asc;
              the SELECT Rank () over (order by `grate` ASC) by `Order from Students. grate` ASC;

    ② conditions : that of fields preceding claims, constraints, thereby screening data, such as: what is the front view of the column (field), and the index which is the view line (Article ), no conditions, all the pieces of data on the whole viewed

      1, specifies which rows View:
        limit the number of rows;
        limit starting line, the number of lines;
          the SELECT * from Students limit 0,10;

      2, the query where: where `column name` below + data
            ! =, =,>, <,> =, <=: Determination value
            and: and
            or: or the
            like, not like: can use wildcards:% (expressed 0 , 1 or more, such as: '% is%', '% King') _ (represented by a character, such as: '__' 'Li _')
            in (), Not in ()
            iS, iS Not: represents `` non for determining properties (rather than determination value)! Such as: SELECT * from Students. WHERE IS Not null name;
            RegRep: a regular expression, such as: '^ [0-9a-zA-the Z] + $'
            BETWEEN .. and ..: for dates, numbers, and other data orderly , such as: SELECT * WHERE `birthday` from STU BETWEEN '1990-1-1' and '2000-1-1';
            Not BETWEEN .. and ..
      . 3: statement packet group by: the later, the same is divided into a set the HAVING
          the SELECT left (sname, 1) name, count (*) number  from Students  group by left (sname,

      4: Sorting statement order by:
          Ascending (default): order by `` asc column names
          in descending order: order by `desc column name`

    ③union: connect the two query displayed together, as long as both the same table structure

    ④ foreign key (associated with another table, cross-table view): constraint fk foreign key (column name) references the table name (column name)
      foreign key must be unique index (usually the primary key of another table)
      the establishment of an outside in the table key: FK constraint Foreign key (TID) References Teacher (ID)
        SELECT s.`id`, s.`name`, s.`grate`, Student t.`name` from S, T WHERE t.`id` Teacher s.`id` =;
        SELECT s.`id`, s.`name`, s.`grate`, Student t.`name` from S, T Teacher Student left from the Join Teacher S = ON t.`id` .`id`;

  Five replicate table (backup):
    Copy table structure: create table new table like table;
    replicate data table: insert new table select * from table;

    Copying only the data table: create table new table as select * from table;
    may be partially copy: create table as select new table column names, column names from table name;

 

Description :
0, users try to use like root @%, linux@'192.168.10.11 'the format
column names with `` wrap is preferably
1, a is called a field (Field), the column name which name is called
2, the establishment of constraint field are: ①auto_increment primary key (primary key: a unique index)
② Note: comment, default: default, no negative sign: unsigned
.. 3, table name with -> available database table <- achieve cross-database table
4, cell contents can not consequently do not fill in if they want to express 'no data' fill null, if you want to represent 'empty data', fill in 0 or ''
5, drop delete: databases, tables, accounts, delete data delete
6, view multiple table: the SELECT * from STU1, STU2, stu3;
7, timely refresh permissions to give permission: flush privileges
8, universal: the role is a set of permissions

Guess you like

Origin www.cnblogs.com/shuangfeike/p/11443395.html