Network security Day23-mariadb database data management and backup

1. Manage libraries in the database

  1. Enter the specified database:use 数据库名字
  2. Addition, deletion, modification and query of the library
    1. Create database:create database 数据库名字
    2. Specify characters and create a database:CREATE DATABASE oldgirl CHARACTER SET utf8;
    3. Delete database:drop database 库名
    4. Change the character set of the database:alter database oldgirl character set utf8mb4;
    5. View the mysql library:show databases

2. Manage tables in the library

  1. Addition, deletion, modification and query of tables
    1. create table syntax
      create table <表名> (
      	 <字段名1> <类型1> ,<字段名n> <类型n>);
      
      1. Field name description: The content can be numbers, letters, underscores, and numbers cannot be used at the beginning
      2. Description of field type (also called option constraint)
        1. Data type (Type)
          1. Integer: number-integer
            1. Tiny integer (tinyint): 1byte=8bit=2^8=256
            2. Large integer (int): 4byte=2^32=(4 billion+)
            3. Super large integer (bigint): 8byte=2^64 (4 billion*4 billion=160 billion)
          2. Characters: all symbols are characters, including integers
            1. Variable-length character type (varchar): Create a string with a specified space size but do not allocate initial space for it, only allocate an upper limit for it
            2. Fixed-length character type (char): Create a specified space size and allocate initial space for it
        2. Whether it is empty (Null): YES or NO
        3. In addition, there are many constraints that will not be listed here.
    2. drop table:drop table 表名
    3. Change table name:rename table 原名字 to 新名字
    4. Check out the tables in the library:show tables
    5. View the table creation syntax:show create table stu1\G
    6. View table structure:desc 表名
      CREATE TABLE `stu1` (                    #<== CREATE TABLE是创建表的固定关键字,stu1为表名。
        `id` int(4) NOT NULL,                  #<==学号列,数字类型,长度为4,不为空值。
        `name` char(20) NOT NULL,              #<==名字列,定长字符类型,长度20,不为空值。
        `age` tinyint(2) NOT NULL DEFAULT '0', #<==年龄列,很小的数字类型,长度为2,不为空,默认为0值。
        `dept` varchar(16) DEFAULT NULL        #<==系别列,变长字符类型,长度16,默认为空。
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8	 #<==引擎和字符集,引擎默认为InnoDB,字符集,继承库utf8。
      
  2. Create table example
    1. Create and enter the library:create database oldboy; use oldboy;
    2. build table
      create table stu1( 
      	id int(10) not null, 
      	name varchar(20) not null, 
      	age tinyint(2)  NOT NULL default '0', 
      	dept varchar(16)  default NULL 
      );
      
    3. View table:show tables;
    4. View table structure: desc stu1;
    5. View the statement to create the table: show create table stu1\G
    6. Modify the table name:rename table
    7. drop table:drop table <表名>

3. Manage fields (columns) in tables

  1. add field
    1. The last line is added:alter table stu1 add 字段名 varchar(类型大小) not null(是否为空) comment 'address'(注释);
    2. Specified position increase:alter table stu1 add 字段名 varchar(类型大小) after 某个字段的名字;
    3. Add in the first line:alter table stu1 add number varchar(11) first;
    4. Before and after comparison
      +-------+-------------+------+-----+---------+-------+
      | Field | Type        | Null | Key | Default | Extra |
      +-------+-------------+------+-----+---------+-------+
      | id    | int(10)     | NO   |     | NULL    |       |
      | name  | varchar(20) | NO   |     | NULL    |       |
      | age   | tinyint(2)  | NO   |     | 0       |       |
      | dept  | varchar(16) | YES  |     | NULL    |       |
      +-------+-------------+------+-----+---------+-------+
      4 rows in set (0.01 sec)
      
      +--------+--------------+------+-----+---------+-------+
      | Field  | Type         | Null | Key | Default | Extra |
      +--------+--------------+------+-----+---------+-------+
      | number | varchar(11)  | YES  |     | NULL    |       |
      | id     | int(10)      | NO   |     | NULL    |       |
      | name   | varchar(20)  | NO   |     | NULL    |       |
      | qq     | varchar(25)  | YES  |     | NULL    |       |
      | age    | tinyint(2)   | NO   |     | 0       |       |
      | dept   | varchar(16)  | YES  |     | NULL    |       |
      | addr   | varchar(256) | NO   |     | NULL    |       |
      +--------+--------------+------+-----+---------+-------+
      7 rows in set (0.00 sec)
      
  2. Delete field:alter table 表名 drop 字段名字;
  3. Change field:alter table 表名 change name sname varchar(128);
  4. View the contents of the table:select * for 表名

4. Manage the data (rows) in the table

  1. rebuild a table
    create table stu1(
    id int(10) PRIMARY KEY not null AUTO_INCREMENT,
    name varchar(20) not null,
    age tinyint(2)  NOT NULL default '0',
    dept varchar(16)  default NULL);
    
  2. Add data to the table
    1. Syntax: insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )](Square brackets indicate options)
    2. View table structure
      MariaDB [oldboy]> desc stu1;
      +-------+-------------+------+-----+---------+----------------+
      | Field | Type        | Null | Key | Default | Extra          |
      +-------+-------------+------+-----+---------+----------------+
      | id    | int(10)     | NO   | PRI | NULL    | auto_increment |
      | name  | varchar(20) | NO   |     | NULL    |                |
      | age   | tinyint(2)  | NO   |     | 0       |                |
      | dept  | varchar(16) | YES  |     | NULL    |                |
      +-------+-------------+------+-----+---------+----------------+
      
    3. insert data
      1. Method 1 (specified column): insert into stu1(id,name,age,dept) values(1,'oldboy',35,'net sec');(Note: character columns should be enclosed in quotation marks, and numeric columns should not be enclosed in quotation marks)
      2. Method 2 (omit columns):insert into stu1 values(2,'oldgirl',25,'linux');
      3. Method 3: (insert multiple rows at the same time):insert into stu1 values(3,'littlegirl',5,'net sec'),(4,'littleboy',2,'Linux');
  3. Delete data in the table
    1. Order:delete from 表名 where 表达式
    2. example:delete from stu1 where id=6;
  4. modify the data in the table
    1. grammar:update 表名 set 字段=新值 where 条件
    2. example:update stu1 set name='zhangsan' where id=6;
  5. query the data in the table
    1. Basic grammar:select <字段1,字段2,...> from <表名> [WHERE 条件]
    2. example:SELECT * FROM city WHERE countrycode='CHN';

5. Database data backup and recovery

  1. Backup principle: Export data in the form of SQL statements
  2. Backup syntax:mysqldump -uroot -poldboy123 -B oldboy >/opt/oldboy.sql
  3. backup all libraries
    1. mysqldump -uroot --poldboy123 -A -B >路径
      1. -A all
      2. -B library (no B is the backup table)
  4. Backup multiple libraries
    5.mysqldump -uroot --poldboy123 -B 库名 >路径
  5. compressed backup
    1. mysqldump -uroot --poldboy123 -B 库名 |gzip>路径
  6. Check backed up data:grep -Ev "^$|^-|^/" /opt/oldboy.sql
  7. restore recovery: source /opt/oldboy.sql;ormysql -uroot -poldboy123 </opt/oldboy.sql
  8. View content without logging in (view data non-interactively):mysql -uroot -poldboy123 -e "select * from oldboy.stu1;"

Guess you like

Origin blog.csdn.net/m0_73293867/article/details/131947774