• mysql基础


    1. 关系型数据库介绍

    数据结构模型

    数据结构模型主要有:

    • 层次关系

    • 网状结构

    • 关系模型

    关系模型

    二维关系 :

    row  row:表中的每一行,又称为一条记录(record)

    column  column:表中的每一列,称为属性,字段(field)

    数据库管理系统:DBMS

    关系:Relational  RDBMS

    RDBMS专业名词

    常见的关系型数据库管理系统:

    • MySQL:MySQL,MariaDB,Percona-Server

    • PostgreSQL:简称为pgsql

    • Oracle

    • MSSQL

    SQL:Structure Query Language,结构化查询语言

    约束:constraint,向数据表提供的数据要遵守的限制

    • 主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。 一个表只能存在一个

    • 惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL) 一个表可以存在多个

    • 外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据

    • 检查性约束:字段值在一定范围内(例:年龄1-150岁之间)

    索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储


    2.mysql的程序组成

    客户端

    • mysql:CLI交互式客户端程序

    • mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令

    • mysqldump:mysql备份工具

    • mysqladmin

    服务器端

    mysqld

     



    3.SQL语句有3种类型:

       

    DDL(Data Defination Language) : 数据定义语言

       

    DML(Data Manipulation Language) : 数据操作语言

       

    DCL(Data Control Language) :数据控制语言

    SQL语句类型 对应操作
    DDL

    CREATE:创建

    DROP:删除

    ALTER:修改

    DML

    INSERT:向表中插入数据

    DELETE:删除表中的数据

    UPDATE:更新表中的信息

    SELECT:查询表中的信息

    DCL

    GRANT:授权

    RECVOKE:移除授权

     mysql工具使用

    语法:mysql [OPTIONS] [database]
    常用的OPTIONS:
        -uUSERNAME      指定用户名,默认为root
        -hHOST          指定服务器主机,默认为localhost,推荐使用ip地址
        -pPASSWORD      指定用户的密码
        -P              指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
        -V              查看当前使用的mysql版本
        -e              不登录mysql执行sql语句后退出,常用于脚本

    4.安装Mariadb

    MariaDB数据库管理系统是MySQL的一个分支完全兼容MySQL

    [root@localhost ~]# yum -y install mariadb*
    ....................................
      tzdata-java-2019c-1.el8.noarch                                                      unixODBC-2.3.7-1.el8.x86_64                                                          
    
    Complete

    设置为开机自启动并且现在启动Mariadb查看端口号

    [root@localhost ~]# systemctl  enable --now mariadb
    Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
    Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
    Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
    [root@localhost ~]# ss -tanlp
    State            Recv-Q           Send-Q                     Local Address:Port                     Peer Address:Port                                                      
    LISTEN           0                80                               0.0.0.0:3306                          0.0.0.0:*              users:(("mysqld",pid=4827,fd=21))          
    LISTEN           0                128                              0.0.0.0:22                            0.0.0.0:*              users:(("sshd",pid=979,fd=5))              
    LISTEN           0                128                                 [::]:22                               [::]:*              users:(("sshd",pid=979,fd=7))  

    使用命令 mysql_secure_installation安全初始化

    root@localhost ~]#  mysql_secure_installation
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    In order to log into MariaDB to secure it, we'll need the current
    password for the root user.  If you've just installed MariaDB, and
    ..............................................

     

    修改密码
    MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' ;

     5.用户操作

    mysql用户帐号由两部分组成,如'USERNAME'@'HOST',表示此USERNAME只能从此HOST上远程登录

    HOST其值可为:

                        IP地址如:192.168.122.1

                        通配符:%(允许从任何地方登录主机)

                                      _  (匹配任意单个字符)   

    数据库用户的创建

    数据库用户创建语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
    
    MariaDB [(none)]> CREATE USER 'yonghu'@'192.168.248.130' IDENTIFIED BY '123!';
    Query OK, 0 rows affected (0.001 sec)
    
    使用新创建的用户和密码登录
    [root@localhost ~]# mysql -uyonghu -p123! -h 192.168.248.130
    Welcome to the MariaDB monitor.  Commands end with ; or g.
    Your MariaDB connection id is 10
    Server version: 10.3.17-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
    
    删除数据库用户
    删除数据库用户语法: DROP USER 'username'@'host';
    MariaDB [(none)]> DROP USER 'yonghu'@'192.168.248.130';
    Query OK, 0 rows affected (0.001 se

     


    DDL操作

    数据库操作

    创建数据库语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
    
    MariaDB [(none)]> CREATE   DATABASE  IF NOT EXISTS  test ;
    Query OK, 1 row affected (0.001 sec)
    
    MariaDB [(none)]> show databases ;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    
    
    删除数据库语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
    
    MariaDB [mysql]> DROP DATABASE IF EXISTS test ;
    Query OK, 0 rows affected (0.001 sec)
    
    MariaDB [mysql]> SHOW DATABASES ;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    +--------------------+
    3 rows in set (0.000 sec)

    表操作

    创建表语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
    mariaDB [test]> CREATE TABLE biao(id int auto_increment not null,name 
    varchar(50),age tinyint,primary key(id));
    Query OK, 0 rows affected (0.003 sec)
    
    MariaDB [test]> DESC biao; 查看表结构
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(50) | YES  |     | NULL    |                |
    | age   | tinyint(4)  | YES  |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    3 rows in set (0.001 sec)
    
    修改表结构语法:ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name;
    MariaDB [test]>  ALTER TABLE biao ADD class varchar(10);
    Query OK, 0 rows affected (0.002 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    MariaDB [test]> DESC biao;
    +-------+-------------+------+-----+---------+----------------+
    | Field | Type        | Null | Key | Default | Extra          |
    +-------+-------------+------+-----+---------+----------------+
    | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(50) | YES  |     | NULL    |                |
    | age   | tinyint(4)  | YES  |     | NULL    |                |
    | class | varchar(10) | YES  |     | NULL    |                |
    +-------+-------------+------+-----+---------+----------------+
    4 rows in set (0.001 sec)
    删除表语法:DROP TABLE [ IF EXISTS ] 'table_name';
    MariaDB [test]> DROP  TABLE IF EXISTS  biao
        -> ;
    Query OK, 0 rows affected (0.004 sec)
    MariaDB [test]> SHOW TABLES ;
    Empty set (0.001 sec)

     


    DML操作

    DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。

    INSERT语句

    语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...;
    MariaDB [test]> INSERT INTO biao (id,name,age,class) VALUE (1,'huahua',20,'freshman');
    Query OK, 1 row affected (0.001 sec)
    
    MariaDB [test]> INSERT INTO biao(name,age,class) VALUE('chaochao',20,'freshman'),('lisi',19,'freshman'),('meijianbiao',50,'freshman');
    Query OK, 3 rows affected (0.002 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    MariaDB [test]> SELECT * FROM  biao;
    +----+-------------+------+----------+
    | id | name        | age  | class    |
    +----+-------------+------+----------+
    |  1 | huahua      |   20 | freshman |
    |  2 | chaochao    |   20 | freshman |
    |  3 | lisi        |   19 | freshman |
    |  4 | meijianbiao |   50 | freshman |
    +----+-------------+------+----------+
    4 rows in set (0.001 sec)

    SELECT语句

    字段column表示:

    表示符 代表什么
    * 所有字段
    as

    字段别名,如col1 AS alias1

    条件判断语句WHERE:

    操作类型 常用操作符
    操作符 >,<,>=,<=,=,!=
    BETWEEN column# AND column#
    LIKE:模糊匹配
    RLIKE:基于正则表达式进行模式匹配
    IS NOT NULL:非空
    IS NULL:空
    条件逻辑操作 AND
    OR
    NOT

    ORDER BY:排序,默认为升序(ASC):

    ORDER BY语句 意思
    ORDER BY ‘column_name' 根据column_name进行升序排序
    ORDER BY 'column_name' DESC 根据column_name进行降序排序
    ORDER BY ’column_name' LIMIT 2 根据column_name进行升序排序
    并只取前2个结果
    ORDER BY ‘column_name' LIMIT 1,2 根据column_name进行升序排序
    并且略过第1个结果取后面的2个结果
    SELECT语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
    
    MariaDB [test]> SELECT  * FROM biao;
    +----+-------------+------+----------+
    | id | name        | age  | class    |
    +----+-------------+------+----------+
    |  1 | huahua      |   20 | freshman |
    |  2 | chaochao    |   20 | freshman |
    |  3 | lisi        |   19 | freshman |
    |  4 | meijianbiao |   50 | freshman |
    +----+-------------+------+----------+
    4 rows in set (0.000 sec)
    
    MariaDB [test]> SElECT id FROM biao  ORDER BY id;
    +----+
    | id |
    +----+
    |  1 |
    |  2 |
    |  3 |
    |  4 |
    +----
    MariaDB [test]> SELECT id FROM biao  ORDER BY  id  DESC ;
    +----+
    | id |
    +----+
    |  4 |
    |  3 |
    |  2 |
    |  1 |
    +----+
    4 rows in set (0.000 sec)
    
    MariaDB [test]> SELECT name FROM  biao  WHERE name  like 'm%';
    +-------------+
    | name        |
    +-------------+
    | meijianbiao |
    +-------------+
    1 row in set (0.000 sec)
    MariaDB [test]> SELECT age FROM biao WHERE age  >=20 ;
    +------+
    | age  |
    +------+
    |   20 |
    |   20 |
    |   50 |
    +------+
    3 rows in set (0.000 sec)
    MariaDB [test]> SELECT * FROM biao WHERE age between 20 and 50 ;
    +----+-------------+------+----------+
    | id | name        | age  | class    |
    +----+-------------+------+----------+
    |  1 | huahua      |   20 | freshman |
    |  2 | chaochao    |   20 | freshman |
    |  4 | meijianbiao |   50 | freshman |
    +----+-------------+------+----------+
    
    MariaDB [test]> SELECT * FROM biao WHERE age  >=20 or age  <=50 ;
    +----+-------------+------+----------+
    | id | name        | age  | class    |
    +----+-------------+------+----------+
    |  1 | huahua      |   20 | freshman |
    |  2 | chaochao    |   20 | freshman |
    |  3 | lisi        |   19 | freshman |
    |  4 | meijianbiao |   50 | freshman |
    +----+-------------+------+----------+
    4 rows in set (0.001 sec)
    MariaDB [test]> SELECT * FROM biao WHERE age is not null ;
    +----+-------------+------+----------+
    | id | name        | age  | class    |
    +----+-------------+------+----------+
    |  1 | huahua      |   20 | freshman |
    |  2 | chaochao    |   20 | freshman |
    |  3 | lisi        |   19 | freshman |
    |  4 | meijianbiao |   50 | freshman |
    +----+-------------+------+----------+
    4 rows in set (0.000 sec)
    
    MariaDB [test]> SELECT * FROM biao WHERE age is null ;
    Empty set (0.000 sec)

    update语句

    语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
    MariaDB [test]> UPDATE biao set age =  25  WHERE  name ='meijianbiao' ;
    Query OK, 1 row affected (0.001 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    MariaDB [test]> SELECT  *  FROM biao;
    +----+-------------+------+----------+
    | id | name        | age  | class    |
    +----+-------------+------+----------+
    |  1 | huahua      |   20 | freshman |
    |  2 | chaochao    |   20 | freshman |
    |  3 | lisi        |   19 | freshman |
    |  4 | meijianbiao |   25 | freshman |
    +----+-------------+------+----------+
    4 rows in set (0.000 sec)
    MariaDB [test]> UPDATE biao set name = 'haha',age = 30  WHERE id = 4 ;
    Query OK, 1 row affected (0.001 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    MariaDB [test]> SELECT  *  FROM biao;
    +----+----------+------+----------+
    | id | name     | age  | class    |
    +----+----------+------+----------+
    |  1 | huahua   |   20 | freshman |
    |  2 | chaochao |   20 | freshman |
    |  3 | lisi     |   19 | freshman |
    |  4 | haha     |   30 | freshman |
    +----+----------+------+----------+
    4 rows in set (0.000 sec)

     delete语句

    语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
    MariaDB [test]> SELECT  *  FROM biao;
    +----+----------+------+----------+
    | id | name     | age  | class    |
    +----+----------+------+----------+
    |  1 | huahua   |   20 | freshman |
    |  2 | chaochao |   20 | freshman |
    |  3 | lisi     |   19 | freshman |
    |  4 | haha     |   30 | freshman |
    +----+----------+------+----------+
    4 rows in set (0.000 sec)
    
    MariaDB [test]> DELETE FROM biao WHERE id = 4;  删除记录
    Query OK, 1 row affected (0.001 sec)
    
    MariaDB [test]> SELECT  *  FROM biao;
    +----+----------+------+----------+
    | id | name     | age  | class    |
    +----+----------+------+----------+
    |  1 | huahua   |   20 | freshman |
    |  2 | chaochao |   20 | freshman |
    |  3 | lisi     |   19 | freshman |
    +----+----------+------+----------+
    3 rows in set (0.000 sec)
    
    MariaDB [test]> DELETE FROM biao; 删除表中所有数据
    Query OK, 3 rows affected (0.001 sec)
    
    MariaDB [test]> SELECT  *  FROM biao;
    Empty set (0.000 sec)

    删除用户

    ariaDB [(none)]> select user,host from mysql.user; 查看数据库中所有用户
    +--------+-----------------------+
    | user   | host                  |
    +--------+-----------------------+
    | root   | 127.0.0.1             |
    | hhh    | 192.168.248.1         |
    | root   | 192.168.248.1         |
    | hhh    | 192.168.248.130       |
    | yonghu | 192.168.248.130       |
    | root   | ::1                   |
    | hhh    | localhost             |
    | root   | localhost             |
    | root   | localhost.localdomain |
    +--------+-----------------------+
    9 rows in set (0.000 sec)
    
    MariaDB [(none)]> DROP user 'hhh'@192.168.248.1;
    Query OK, 0 rows affected (0.000 sec)
    
    MariaDB [(none)]> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.001 sec)
    
    MariaDB [(none)]> select user,host from mysql.user;
    +--------+-----------------------+
    | user   | host                  |
    +--------+-----------------------+
    | root   | 127.0.0.1             |
    | root   | 192.168.248.1         |
    | hhh    | 192.168.248.130       |
    | yonghu | 192.168.248.130       |
    | root   | ::1                   |
    | hhh    | localhost             |
    | root   | localhost             |
    | root   | localhost.localdomain |
    +--------+-----------------------+
    8 rows in set (0.000 sec)

     

    truncate语句

    delete与truncate的区别

    delete:DELETE删除表内容时仅删除内容,但会保留表结构,DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项,可以通过回滚事务日志恢复数据,非常占用空间。

    truncate:删除表中所有数据,且无法恢复表结构、约束和索引等保持不变,新添加的行计数值重置为初始值,执行速度比DELETE快,且使用的系统和事务日志资源少,通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放,对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据,不能用于加入了索引视图的表。

    语法:TRUNCATE table_name;
    MariaDB [test]> select * from biao;
    +----+------+------+-------+
    | id | name | age  | class |
    +----+------+------+-------+
    |  1 | haha |    1 | NULL  |
    |  2 | ww   |    3 | NULL  |
    |  3 | xx   |    9 | NULL  |
    +----+------+------+-------+
    3 rows in set (0.000 sec)
    
    MariaDB [test]> truncate biao;
    Query OK, 0 rows affected (0.004 sec)

    DCL操作

    创建授权grant

    用法: grant 权限 on 数据库对象 to 用户

    MariaDB [test]> GRANT DELETE on test.biao TO 'hhh'@'%' IDENTIFIED BY '1234!';  用户hhh在所有位置远程访问test数据库有DELETE权限
    Query OK, 0 rows affected (0.000 sec)
    MariaDB [test]> GRANT ALL  on *.* TO 'xxx'@'%' IDENTIFIED BY '1234!'; 用户xxx在所有位置上远程登录访问数据库拥有所有权限
    Query OK, 0 rows affected (0.000 sec)

    查看用户权限SHOW GRANTS

    MariaDB [test]> SHOW GRANTS;  产看当前登录用户的权限
    +----------------------------------------------------------------------------------------------------------------------------------------+
    | Grants for root@localhost                                                                                                              |
    +----------------------------------------------------------------------------------------------------------------------------------------+
    | GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' WITH GRANT OPTION |
    | GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION                                                                          |
    +----------------------------------------------------------------------------------------------------------------------------------------+
    2 rows in set (0.000 sec)

    查看指定用户权限

    MariaDB [test]> SHOW GRANTS FOR xxx;
    +-------------------------------------------------------------------------------------------------------------+
    | Grants for xxx@%                                                                                            |
    +-------------------------------------------------------------------------------------------------------------+
    | GRANT ALL PRIVILEGES ON *.* TO 'xxx'@'%' IDENTIFIED BY PASSWORD '*9BE02DCF8FEE75750C9262B0875091F94C620EF2' |
    +-------------------------------------------------------------------------------------------------------------+
    1 row in set (0.000 sec)
    
    MariaDB [test]> SHOW GRANTS FOR hhh;
    +----------------------------------------------------------------------------------------------------+
    | Grants for hhh@%                                                                                   |
    +----------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'hhh'@'%' IDENTIFIED BY PASSWORD '*9BE02DCF8FEE75750C9262B0875091F94C620EF2' |
    | GRANT DELETE ON `test`.`biao` TO 'hhh'@'%'                                                         |
    +----------------------------------------------------------------------------------------------------+
    2 rows in set (0.000 sec)

     取消授权REVOKE

    语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
    
    MariaDB [(none)]> REVOKE  ALL ON *.* FROM 'xxx'@'%';
    Query OK, 0 rows affected (0.001 sec)
    
    MariaDB [(none)]> FLUSH PRIVILEGES; flush告诉服务器重新加载授权表。
    Query OK, 0 rows affected (0.001 sec)
    
    MariaDB [(none)]> show grants for xxx;
    +----------------------------------------------------------------------------------------------------+
    | Grants for xxx@%                                                                                   |
    +----------------------------------------------------------------------------------------------------+
    | GRANT USAGE ON *.* TO 'xxx'@'%' IDENTIFIED BY PASSWORD '*9BE02DCF8FEE75750C9262B0875091F94C620EF2' |
    +----------------------------------------------------------------------------------------------------+

    练习

    1.搭建mysql服务
    2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
    
    mysql> desc student;
    +-------+--------------+------+-----+---------+----------------+
    | Field | Type         | Null | Key | Default | Extra          |
    +-------+--------------+------+-----+---------+----------------+
    | id    | int(11)      | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(100) | NO   |     | NULL    |                |
    | age   | tinyint(4)   | YES  |     | NULL    |                |
    +-------+--------------+------+-----+---------+----------------+
    3 rows in set (0.01 sec)
    
    3.查看下该新建的表有无内容(用select语句)
    4.往新建的student表中插入数据(用insert语句),结果应如下所示:
    
    +----+-------------+------+
    | id | name        | age  |
    +----+-------------+------+
    |  1 | tom         |   20 |
    |  2 | jerry       |   23 |
    |  3 | wangqing    |   25 |
    |  4 | sean        |   28 |
    |  5 | zhangshan   |   26 |
    |  6 | zhangshan   |   20 |
    |  7 | lisi        | NULL |
    |  8 | chenshuo    |   10 |
    |  9 | wangwu      |    3 |
    | 10 | qiuyi       |   15 |
    | 11 | qiuxiaotian |   20 |
    +----+-------------+------+
    
    5.修改lisi的年龄为50
    6.以age字段降序排序
    7.查询student表中年龄最小的3位同学跳过前2位
    8.查询student表中年龄最大的4位同学
    9.查询student表中名字叫zhangshan的记录
    10.查询student表中名字叫zhangshan且年龄大于20岁的记录
    11.查询student表中年龄在23到30之间的记录
    12.修改wangwu的年龄为100
    13.删除student中名字叫zhangshan且年龄小于等于20的记录

    2).

    MariaDB [(none)]> create database tc;
    Query OK, 1 row affected (0.001 sec)
    
    MariaDB [(none)]> use tc;
    Database changed
    MariaDB [tc]> create table student(id int not null auto_increment,name varchar(100) not null,age tinyint,primary key(id));
    Query OK, 0 rows affected (0.009 sec)
    
    MariaDB [tc]> desc student;
    +-------+--------------+------+-----+---------+----------------+
    | Field | Type         | Null | Key | Default | Extra          |
    +-------+--------------+------+-----+---------+----------------+
    | id    | int(11)      | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(100) | NO   |     | NULL    |                |
    | age   | tinyint(4)   | YES  |     | NULL    |                |
    +-------+--------------+------+-----+---------+----------------+
    3 rows in set (0.005 sec)

    3).

    MariaDB [tc]> select * from student;
    Empty set (0.000 sec)

    4).

    MariaDB [tc]> insert student(name,age) values('tom',20),('jerry',23),('wangqing',25),('sean',28),('zhangshan',26),('zhangshan',20),('lisi',null),('chenshuo',10),('wangwu',3),('qiuyi',15),('qiuxiaotian',20);
    Query OK, 11 rows affected (0.001 sec)
    Records: 11  Duplicates: 0  Warnings: 0
    
    MariaDB [tc]> select * from student;
    +----+-------------+------+
    | id | name        | age  |
    +----+-------------+------+
    |  1 | tom         |   20 |
    |  2 | jerry       |   23 |
    |  3 | wangqing    |   25 |
    |  4 | sean        |   28 |
    |  5 | zhangshan   |   26 |
    |  6 | zhangshan   |   20 |
    |  7 | lisi        | NULL |
    |  8 | chenshuo    |   10 |
    |  9 | wangwu      |    3 |
    | 10 | qiuyi       |   15 |
    | 11 | qiuxiaotian |   20 |
    +----+-------------+------+
    11 rows in set (0.001 sec

    5).

    MariaDB [tc]> update  student  set age = 50 where id =7;
    Query OK, 1 row affected (0.001 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    MariaDB [tc]> select * from student;
    +----+-------------+------+
    | id | name        | age  |
    +----+-------------+------+
    |  1 | tom         |   20 |
    |  2 | jerry       |   23 |
    |  3 | wangqing    |   25 |
    |  4 | sean        |   28 |
    |  5 | zhangshan   |   26 |
    |  6 | zhangshan   |   20 |
    |  7 | lisi        |   50 |
    |  8 | chenshuo    |   10 |
    |  9 | wangwu      |    3 |
    | 10 | qiuyi       |   15 |
    | 11 | qiuxiaotian |   20 |
    +----+-------------+------+
    11 rows in set (0.001 sec)

    6).

    MariaDB [tc]> select age from student order by age desc;
    +------+
    | age  |
    +------+
    |   50 |
    |   28 |
    |   26 |
    |   25 |
    |   23 |
    |   20 |
    |   20 |
    |   20 |
    |   15 |
    |   10 |
    |    3 |
    +------+
    11 rows in set (0.000 sec)

    7).

    MariaDB [tc]> select age from student order by age limit 2;
    +------+
    | age  |
    +------+
    |    3 |
    |   10 |
    +------+
    2 rows in set (0.000 sec)

    8).

    MariaDB [tc]> select age from student order by age desc  limit 4;
    +------+
    | age  |
    +------+
    |   50 |
    |   28 |
    |   26 |
    |   25 |
    +------+
    4 rows in set (0.000 sec)

    9).

    MariaDB [tc]> select * from student where name = 'zhangshan';
    +----+-----------+------+
    | id | name      | age  |
    +----+-----------+------+
    |  5 | zhangshan |   26 |
    |  6 | zhangshan |   20 |
    +----+-----------+------+
    2 rows in set (0.001 sec)

    10).

    MariaDB [tc]> select * from student where  name = 'zhangshan' and  age > 20 ;
    +----+-----------+------+
    | id | name      | age  |
    +----+-----------+------+
    |  5 | zhangshan |   26 |
    +----+-----------+------+
    1 row in set (0.001 sec)

    11).

    MariaDB [tc]> select  age  from student   where  age  between 23 and 30;
    +------+
    | age  |
    +------+
    |   23 |
    |   25 |
    |   28 |
    |   26 |
    +------+
    4 rows in set (0.001 sec)

    12).

    MariaDB [tc]> update student set age = 100 where name = 'wangwu';
    Query OK, 1 row affected (0.001 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    MariaDB [tc]> select * from student;
    +----+-------------+------+
    | id | name        | age  |
    +----+-------------+------+
    |  1 | tom         |   20 |
    |  2 | jerry       |   23 |
    |  3 | wangqing    |   25 |
    |  4 | sean        |   28 |
    |  5 | zhangshan   |   26 |
    |  6 | zhangshan   |   20 |
    |  7 | lisi        |   50 |
    |  8 | chenshuo    |   10 |
    |  9 | wangwu      |  100 |
    | 10 | qiuyi       |   15 |
    | 11 | qiuxiaotian |   20 |
    +----+-------------+------+
    11 rows in set (0.000 sec

    13).

    MariaDB [tc]> delete from student where name = 'zhangshan' and age <=20 ;
    Query OK, 1 row affected (0.002 sec)
    
    MariaDB [tc]> select * from student;
    +----+-------------+------+
    | id | name        | age  |
    +----+-------------+------+
    |  1 | tom         |   20 |
    |  2 | jerry       |   23 |
    |  3 | wangqing    |   25 |
    |  4 | sean        |   28 |
    |  5 | zhangshan   |   26 |
    |  7 | lisi        |   50 |
    |  8 | chenshuo    |   10 |
    |  9 | wangwu      |  100 |
    | 10 | qiuyi       |   15 |
    | 11 | qiuxiaotian |   20 |
    +----+-------------+------+
    10 rows in set (0.000 sec)
  • 相关阅读:
    mac下编写命令脚本
    mac环境mongodb安装小坑
    JS
    设计模式:装饰器
    proxy 数据帧听
    react hook 简单实现
    报错:java.lang.NumberFormatException: null
    git回滚到指定版本
    1109. 航班预订统计 力扣(中等) 差分数组 不会但神奇
    528. 按权重随机选择 力扣(中等) 前缀和rand()
  • 原文地址:https://www.cnblogs.com/diqiyao/p/14186039.html
Copyright © 2020-2023  润新知