1.是一种一体化语言
集数据定义、数据操纵、数据控制功能于一体。可以完成数据库中全部操作,包括定义关系模式、录入数据、查询、维护等。
2.是一种高度非过程化语言
不需一步步告诉“如何做”,只需描述“做什么”。SQL可将要求交与系统自动完成。存取路径的选择以及SQL语句的操作过程由系统自动完成。减轻了用户负担,而且有利于提高数据独立性。
3.语言非常简洁
只用9动词就能完成数据核心功能。数据定义:create,drop,alter,数据操纵:SELECT INSERT、UPDATE、DELETE;数据控制: GRANT (设置用户权限)、REVOKE(取消设置用户权限)。
4.同一种语法结构提供两种使用方式
数据结构模型主要有:
关系模型:
二维关系:row,column
数据库管理系统:DBMS
关系:Relational,RDBMS
常见的关系型数据库管理系统:
SQL:Structure Query Language,结构化查询语言
约束:constraint,向数据表提供的数据要遵守的限制
MySQL数据库管理系统数据表字段约束主要包括主键约束、外键约束、默认值约束、唯一约束、空值约束、自动增长约束等。
1.主键约束 Primary Key
主键的概念:记录的唯一标识,能够通过该标识确定唯一一条记录。如上表中唯一能够确定学生记录的只能是学号,因此学号是主键。主键是不允许为空,不允许出现重复。可以在定义数据表字段时给字段添加主键约束。语法结构为 :
//字段名称 数据类型 主键约束
id varchar(14) primary key
如上表所示,定义学生学号字段名称为id ,数据类型为可变长度字符串,长度为14 ,设置其为主键。
在定义主键时需要注意多字段组合形式主键的定义,在很多情况下我们的主键需要由多个字段所组成,如学生成绩信息表,主要字段包括学号、课程编号、成绩。针对该表主键为学号与课程编号组合。
primary key(id,cid)
//id指学号
//cid指课程编号
2、外键约束 Foreign Key
外键约束主要针对多个表之间的关系进行描述,外键的定义描述如下:
对于字段F在表A中不是主键,但在另外一个表B中表示主键,这个时候字段F对于表A可称为外键,在定义过程中需指明F在哪个表中是主键。另外需要注意,两个表中并不需要都是用F作为字段名,只要两者存储数据含义与类型一致即可。为说明外键约束,我们使用学生基本信息表与班级信息表进行说明,表结构存储数据描述如下图:
如上表所示,学生信息表与班级信息表中均存在一个班级编号字段,在学生信息表中,该字段不表示任何键,但在班级信息表中编辑编码为主键,因此相对于学生信息表,班级编号为外键。实现语法描述如下:
constraint stu_class_fk foreign key (class_id) references classinfor(class_id)
//由于存在外键关系需要首先定义班级信息表classinfor,再定义学生信息表
// 在定义学生信息表stuinfor中定义外键约束
//stu_class_fk表示外键名称
//references表示外键依赖关系
3、默认值约束 default
默认值约束主要用于实现为字段提供统一的默认值,如学生信息表中,所有的班级编号字段都取值1,为简化操作,我们可以直接为班级编号定义默认值约束,取值为1,在后期录入数据时,可直接去掉对该字段的数据录入。在定义字段时可直接指明默认值,基本语法:
字段名称 数据类型 默认值
class_id varchar(4) default “1”;
//设置学生信息表中学号字段默认值为1
4.唯一约束unique
唯一约束主要用于约束字段取值的唯一性,有些情况下需要在录入数据时保存数据的唯一性,如在上表学生信息表中,需要学生的联系电话唯一,即该字段不允许输入重复的值。示例代码如下:
字段名称 数据类型 unique
stu_tel varchar(14) unique
//设置学生电话字段名称为stu_tel
//类型为长度14字符串
//唯一约束
5.空值约束 NULL
字段允许取空值则可使用空值约束,默认情况下,字段是允许为空的。如我们创建一个学生成绩表,在录入成绩之前需要将学号,课程编号写入数据表,但是允许成绩为空时,对成绩字段设置允许为空。代码说明如下:
字段名称 数据类型 null | not null
stu_score float(4) null
//设置学生成绩字段名称为stu_score
//类型为浮点型
//设置允许为空
6.自动增长约束 auto_increment
自动增长可以理解为一种约束,也可以理解为一种数据类型。是指某一个字段值依次增长,且不重复,(序号)在这种情况下可以将其设置为自动增长类型。如我们可以将班级信息表中的班级编号设置为自动增长字段:
class_id int auto_increment primary key;
//班级编号 class_id
//数据类型 int整数
//自动增长,默认初始值1,增量1
//主键 primary key
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
关系型数据库的常见组件有:
SQL语句有三种类型:
SQL语句类型 | 对应操作 |
---|---|
DDL | CREATE:创建 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据DELETE:删除表中数据UPDATE:更新表中数据SELECT:查询表中数据 |
DCL | GRANT:授权 REVOKE:移除授权 |
[root@yanlei ~]# yum -y install mariadb*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00
extras | 2.9 kB 00:00
updates | 2.9 kB 00:00
Package 1:mariadb-embedded-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-server-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-test-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-devel-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-bench-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-embedded-devel-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-5.5.65-1.el7.x86_64 already installed and latest version
Package 1:mariadb-libs-5.5.65-1.el7.x86_64 already installed and latest version
Nothing to do
1.启动mysql
[root@yanlei ~]# systemctl enable --now mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@yanlei ~]# systemctl status mariadb
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2020-05-20 14:08:53 CST; 2s ago
Process: 2762 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
2.查看监听端口3306
[root@yanlei ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
3.设置登录mysql密码
[root@yanlei ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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.
MariaDB [(none)]> set password = password('147258');
语法:mysql [OPTIONS] [database]
常用的OPTIONS:
[root@yanlei ~]# mysql -uroot -p147258 -h127.0.0.1Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.65-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.
MariaDB [(none)]>
[root@yanlei ~]# mysql -V
mysql Ver 15.1 Distrib 5.5.65-MariaDB, for Linux (x86_64) using readline 5.1
[root@yanlei ~]# mysql -uroot -p147258 -h127.0.0.1 -e 'show databases;'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
socket类型 | 说明 |
---|---|
ip socket | 默认监听在tcp的3306端口,支持远程通信 |
unix sock | 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock) 仅支持本地通信 server地址只能是:localhost,127.0.0.1 |
//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
//创建数据库hello
MariaDB [(none)]> create database if not exists hello;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库hello
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> drop database if exists hello;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
/创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
//在数据库hello里创建表class
MariaDB [hello]> create table class (id int not null,name varchar(60) not null,age tinyint not null);
Query OK, 0 rows affected (0.00 sec)
MariaDB [hello]> show tables;
+-----------------+
| Tables_in_hello |
+-----------------+
| class |
+-----------------+
1 row in set (0.00 sec)
MariaDB [hello]> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(60) | NO | | NULL | |
| age | tinyint(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from class;
Empty set (0.00 sec)
//删除表
//语法:DROP TABLE [ IF EXISTS ] 'table_name';
//删除表class
MariaDB [hello]> show tables;
+-----------------+
| Tables_in_hello |
+-----------------+
| class |
+-----------------+
1 row in set (0.00 sec)
MariaDB [hello]> drop table class;
Query OK, 0 rows affected (0.01 sec)
MariaDB [hello]> show tables;
Empty set (0.00 sec)
MariaDB [hello]>
mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建数据库用户lei
MariaDB [(none)]> create user 'lei'@'192.168.220.135' identified by '147258';
Query OK, 0 rows affected (0.00 sec)
//删除数据库用户
//语法:DROP USER 'username'@'host';
MariaDB [(none)]> drop user [email protected];
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
mysql> SHOW CHARACTER SET; //查看支持的所有字符集
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
mysql> SHOW ENGINES; //查看当前数据库支持的所有存储引擎
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
| ARCHIVE | YES | gzip-compresses tables for a low storage footprint | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | YES | Allows to access tables on other MariaDB servers, supports transactions and more | YES | NO | YES |
| Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)
MariaDB [(none)]> SHOW ENGINES\G
*************************** 1. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Percona-XtraDB, Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: MyISAM
Support: YES
Comment: Non-transactional engine with good performance and small data footprint
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: CSV
Support: YES
Comment: Stores tables as CSV files
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: ARCHIVE
Support: YES
Comment: gzip-compresses tables for a low storage footprint
Transactions: NO
XA: NO
Savepoints: NO
*************************** 8. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: FEDERATED
Support: YES
Comment: Allows to access tables on other MariaDB servers, supports transactions and more
Transactions: YES
XA: NO
Savepoints: YES
*************************** 10. row ***************************
Engine: Aria
Support: YES
Comment: Crash-safe tables with MyISAM heritage
Transactions: NO
XA: NO
Savepoints: NO
10 rows in set (0.00 sec)
mysql> SHOW DATABASES; //查看数据库信息
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW TABLES FROM wangqingge; //不进入某数据库而列出其包含的所有表
MariaDB [(none)]> SHOW TABLES FROM hello;
+-----------------+
| Tables_in_hello |
+-----------------+
| class |
+-----------------+
1 row in set (0.00 sec)
//查看表结构
//语法:DESC [db_name.]table_name;
MariaDB [(none)]> desc hello.class;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | tinyint(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
//查看某表的创建命令
//语法:SHOW CREATE TABLE table_name;
MariaDB [(none)]> SHOW CREATE TABLE hello.class;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| class | CREATE TABLE `class` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL,
`age` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
//查看某表的状态
//语法:SHOW TABLE STATUS LIKE 'table_name'\G
MariaDB [(none)]> use hello;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hello]> show table status like 'class'\G
*************************** 1. row ***************************
Name: class
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 10485760
Auto_increment: NULL
Create_time: 2020-05-23 01:17:10
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
MariaDB [hello]>
//获取命令使用帮助
//语法:HELP keyword;
mysql> HELP CREATE TABLE; //获取创建表的帮助
MariaDB [hello]> HELP CREATE TABLE;
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
select_statement
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition:
col_name column_definition
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name,...) reference_definition
| CHECK (expr)
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。
//DML操作之增操作insert
//语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
MariaDB [hello]> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | tinyint(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [hello]> insert into class value(1,'tom',23);
Query OK, 1 row affected (0.00 sec)
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> insert into class value(2,'jee',18),(3,'yyu',24);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 18 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> desc yanlei;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(200) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
MariaDB [hello]> insert into yanlei(name) value('xiaowang');
Query OK, 1 row affected (0.00 sec)
MariaDB [hello]> select * from yanlei;
+------+----------+------+
| id | name | age |
+------+----------+------+
| NULL | xiaowang | NULL |
+------+----------+------+
1 row in set (0.00 sec)
//设置id字段自动增长
MariaDB [hello]> create table xiaoxiao(id int primary key auto_increment not null,name varchar(50),age tinyint);
Query OK, 0 rows affected (0.00 sec)
MariaDB [hello]> desc xiaoxiao;
+-------+-------------+------+-----+---------+----------------+
| 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.00 sec)
MariaDB [hello]> insert xiaoxiao value(1,'tom',21);
Query OK, 1 row affected (0.00 sec)
MariaDB [hello]> select * from xiaoxiao;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 21 |
+----+------+------+
1 row in set (0.00 sec)
MariaDB [hello]> insert xiaoxiao value('tom',21);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
MariaDB [hello]> insert xiaoxiao(name,age) value('tom',21);
Query OK, 1 row affected (0.00 sec)
MariaDB [hello]> select * from xiaoxiao;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 21 |
| 2 | tom | 21 |
+----+------+------+
2 rows in set (0.00 sec)
字段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个结果 |
//DML操作之查操作select
//语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 18 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> select id,age from class
-> ;
+----+-----+
| id | age |
+----+-----+
| 1 | 23 |
| 2 | 18 |
| 3 | 24 |
+----+-----+
3 rows in set (0.00 sec)
\\设置别名
MariaDB [hello]> select id as 编号,age from class
-> ;
-> ;
+--------+-----+
| 编号 | age |
+--------+-----+
| 1 | 23 |
| 2 | 18 |
| 3 | 24 |
+--------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from class where age=18;+----+------+-----+
| id | name | age |
+----+------+-----+
| 2 | jee | 18 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> select neme from class where age=18;
ERROR 1054 (42S22): Unknown column 'neme' in 'field list'
MariaDB [hello]> select name from class where age=18;
+------+
| name |
+------+
| jee |
+------+
1 row in set (0.00 sec)
MariaDB [hello]> select * from class where age=18 or name='tom';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 18 |
+----+------+-----+
2 rows in set (0.00 sec)
MariaDB [hello]> select * from class where age=18 and name='jee';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 2 | jee | 18 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> select * from class where age between 20 and 23;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]>
MariaDB [hello]> select * from class where age >= 20 and age <= 24;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 3 | yyu | 24 |
+----+------+-----+
2 rows in set (0.00 sec)
MariaDB [hello]> select * from class where age like '2%';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 3 | yyu | 24 |
+----+------+-----+
2 rows in set (0.00 sec)
MariaDB [hello]> select * from class where name rlike '^t[a-z][a-z]';
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> select * from xiaoxiao;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 21 |
| 2 | tom | 21 |
| 3 | lisi | NULL |
+----+------+------+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from xiaoxiao where age is not null;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 21 |
| 2 | tom | 21 |
+----+------+------+
2 rows in set (0.00 sec)
MariaDB [hello]> select * from xiaoxiao where age is null;
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | lisi | NULL |
+----+------+------+
1 row in set (0.00 sec)
MariaDB [hello]> select * from class order by age;+----+------+-----+
| id | name | age |
+----+------+-----+
| 2 | jee | 18 |
| 1 | tom | 23 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from class order by age limit 1;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 2 | jee | 18 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> select name from class order by age limit 1;
+------+
| name |
+------+
| jee |
+------+
1 row in set (0.00 sec)
MariaDB [hello]> select * from class order by age ;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 2 | jee | 18 |
| 1 | tom | 23 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from class order by age limit 1,1;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> select * from class order by age limit 1,2;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 3 | yyu | 24 |
+----+------+-----+
2 rows in set (0.00 sec)
MariaDB [hello]> select * from class order by age limit 2,1;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 3 | yyu | 24 |
+----+------+-----+
1 row in set (0.00 sec)
MariaDB [hello]> select * from class order by age desc;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 3 | yyu | 24 |
| 1 | tom | 23 |
| 2 | jee | 18 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from class order by age desc limit 1;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 3 | yyu | 24 |
+----+------+-----+
1 row in set (0.00 sec)
//DML操作之改操作update
//语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 18 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> update class set age = 25 where id = 2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 25 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
//DML操作之删操作delete
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 25 |
| 3 | yyu | 24 |
+----+------+-----+
3 rows in set (0.00 sec)
MariaDB [hello]> delete from class where id = 3; 删除某条记录
Query OK, 1 row affected (0.00 sec)
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 25 |
+----+------+-----+
2 rows in set (0.00 sec)
MariaDB [hello]> delete from yanlei; 删除整张表内容
Query OK, 1 row affected (0.00 sec)
MariaDB [hello]> show tables;
+-----------------+
| Tables_in_hello |
+-----------------+
| class |
| xiaoxiao |
| yanlei |
+-----------------+
3 rows in set (0.00 sec)
MariaDB [hello]> select * from yanlei;
Empty set (0.00 sec)
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
//语法:TRUNCATE table_name;
MariaDB [hello]> select * from class;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | tom | 23 |
| 2 | jee | 25 |
+----+------+-----+
2 rows in set (0.00 sec)
MariaDB [hello]> truncate class;
Query OK, 0 rows affected (0.00 sec)
MariaDB [hello]> select * from class;
Empty set (0.00 sec)
MariaDB [hello]> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | NO | | NULL | |
| age | tinyint(4) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
权限类型(priv_type)
权限类型 | 代表什么? |
---|---|
ALL | 所有权限 |
SELECT | 读取内容的权限 |
INSERT | 插入内容的权限 |
UPDATE | 更新内容的权限 |
DELETE | 删除内容的权限 |
指定要操作的对象db_name.table_name
表示方式 | 意义 |
---|---|
* . * | 所有库的所有表 |
db_name | 指定库的所有表 |
db_name.table_name | 指定库的指定表 |
WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。
GRANT priv_type,... ON [object_type] db_name.table_name TO ‘username'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
//授权tom用户本机上hello库xiaoxiao表插入数据
MariaDB [(none)]> create user 'tom'@'localhost' identified by '147258';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant insert on hello.xiaoxiao to 'tom'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello |
| test |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> use hello;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hello]> show tables;
+-----------------+
| Tables_in_hello |
+-----------------+
| xiaoxiao |
+-----------------+
1 row in set (0.00 sec)
MariaDB [hello]> select * from xiaoxiao;
ERROR 1142 (42000): SELECT command denied to user 'tom'@'localhost' for table 'xiaoxiao'
MariaDB [hello]> insert xiaoxiao(name,age) value('xiaozhang',15);
Query OK, 1 row affected (0.11 sec)
当前用户权限
MariaDB [(none)]> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*288A7937D7A48B2C8148F1849013DBD8AA443263' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
查看tom@localhost权限
MariaDB [(none)]> show grants for 'tom'@'localhost';
+------------------------------------------------------------------------------------------------------------+
| Grants for tom@localhost |
+------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'localhost' IDENTIFIED BY PASSWORD '*288A7937D7A48B2C8148F1849013DBD8AA443263' |
| GRANT INSERT ON `hello`.`xiaoxiao` TO 'tom'@'localhost' |
+------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
MariaDB [(none)]> revoke select on *.* from 'tom'@'localhost';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges; 刷新权限
Query OK, 0 rows affected (0.00 sec)
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:
MariaDB [(none)]> flush privileges;
1、打开mysql配置文件/etc/my.cnf在【mysqld】下面添加一行代码:skip-grant-tables。这行代码意思就是跳过跳过授权表,即是可以跳过密码验证直接进入数据库。
[root@yanlei ~]# vim /etc/my.cnf
[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
2.重启mysql数据库
[root@yanlei ~]# systemctl restart mariadb
3、mysql -uroot -p //此时直接回车,既可以进入数据库。
出现mysql>就说明已经进入到mysql数据库
[root@yanlei ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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.
MariaDB [(none)]>
4、进数据库后,use mysql //选择mysql这个库,因为mysql的root密码存放在这个数据库里
MariaDB [(none)]> use mysql;
5.show tables //查看下mysql库里有哪些表,需要操作的用户名密码都在user表里。
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
6.desc user //查看下user表有哪些字段
MariaDB [mysql]> desc user;
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
| Select_priv | enum('N','Y') | NO | | N | |
| Insert_priv | enum('N','Y') | NO | | N | |
| Update_priv | enum('N','Y') | NO | | N | |
| Delete_priv | enum('N','Y') | NO | | N | |
| Create_priv | enum('N','Y') | NO | | N | |
| Drop_priv | enum('N','Y') | NO | | N | |
| Reload_priv | enum('N','Y') | NO | | N | |
| Shutdown_priv | enum('N','Y') | NO | | N | |
| Process_priv | enum('N','Y') | NO | | N | |
| File_priv | enum('N','Y') | NO | | N | |
| Grant_priv | enum('N','Y') | NO | | N | |
| References_priv | enum('N','Y') | NO | | N | |
| Index_priv | enum('N','Y') | NO | | N | |
| Alter_priv | enum('N','Y') | NO | | N | |
| Show_db_priv | enum('N','Y') | NO | | N | |
| Super_priv | enum('N','Y') | NO | | N | |
| Create_tmp_table_priv | enum('N','Y') | NO | | N | |
| Lock_tables_priv | enum('N','Y') | NO | | N | |
| Execute_priv | enum('N','Y') | NO | | N | |
| Repl_slave_priv | enum('N','Y') | NO | | N | |
| Repl_client_priv | enum('N','Y') | NO | | N | |
| Create_view_priv | enum('N','Y') | NO | | N | |
| Show_view_priv | enum('N','Y') | NO | | N | |
| Create_routine_priv | enum('N','Y') | NO | | N | |
| Alter_routine_priv | enum('N','Y') | NO | | N | |
| Create_user_priv | enum('N','Y') | NO | | N | |
| Event_priv | enum('N','Y') | NO | | N | |
| Trigger_priv | enum('N','Y') | NO | | N | |
| Create_tablespace_priv | enum('N','Y') | NO | | N | |
| ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
| ssl_cipher | blob | NO | | NULL | |
| x509_issuer | blob | NO | | NULL | |
| x509_subject | blob | NO | | NULL | |
| max_questions | int(11) unsigned | NO | | 0 | |
| max_updates | int(11) unsigned | NO | | 0 | |
| max_connections | int(11) unsigned | NO | | 0 | |
| max_user_connections | int(11) | NO | | 0 | |
| plugin | char(64) | NO | | | |
| authentication_string | text | NO | | NULL | |
+------------------------+-----------------------------------+------+-----+---------+-------+
42 rows in set (0.00 sec)
7.update user set password=password(***) where user=“root”; //用户选root,可以随便更改成任意密码
MariaDB [mysql]> update user set password = password('123456') where user = "root";
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
8.flush privileges; //刷新下密码,使更改的生效。
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
9.exit //退出数据库
MariaDB [mysql]> exit
Bye
[root@yanlei ~]#
10.mysql -uroot -p //回车输入刚刚更改的密码,就能进去了
然后再次进入配置文件vi /etc/my.cnf 把skip-grant-tables去掉
[root@yanlei ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.65-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.
MariaDB [(none)]>
1.搭建mysql服务
[root@yanlei ~]# yum -y install mariadb*
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.65-1.el7 will be installed
---> Package mariadb-bench.x86_64 1:5.5.65-1.el7 will be installed
--> Processing Dependency: perl(GD) for package: 1:mariadb-bench-5.5.65
[root@yanlei ~]# systemctl enable --now mariadb
[root@yanlei ~]# systemctl restart mariadb
[root@yanlei ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
2.创建一个以yanlei名字的数据库,并创建一张表student,该表包含三个字段(id,name,age),
MariaDB [yanlei]> create table student(id int primary key auto_increment not null,name varchar(50) not null,age tinyint);
Query OK, 0 rows affected (0.00 sec)
MariaDB [yanlei]> show tables
-> ;
+------------------+
| Tables_in_yanlei |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
MariaDB [yanlei]> desc student
-> ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
3.查看下该新建的表有无内容(用select语句)
MariaDB [yanlei]> select * from student;
Empty set (0.00 sec)
4.往新建的student表中插入数据(用insert语句),
MariaDB [yanlei]> insert into student (id,name,age) value(1,'tom',20);
Query OK, 1 row affected (0.00 sec)
MariaDB [yanlei]> insert into student (id,name,age) value(2,'jerry',23),(3,'wangqing',25),(4,'sea',28),(5,'z
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [yanlei]> insert into student (id,name,age) values(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [yanlei]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sea | 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.00 sec)
5.修改lisi的年龄为50
MariaDB [yanlei]> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [yanlei]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sea | 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.00 sec)
6.以age字段降序排序
MariaDB [yanlei]> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 4 | sea | 28 |
| 5 | zhangshan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 6 | zhangshan | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
+----+-------------+------+
11 rows in set (0.00 sec)
7.查询student表中年龄最小的3位同学跳过前2位
MariaDB [yanlei]> select * from student order by age limit 2,1;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 10 | qiuyi | 15 |
+----+-------+------+
1 row in set (0.00 sec)
8.查询student表中年龄最大的4位同学
MariaDB [yanlei]> select * from student order by age desc limit 4;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 7 | lisi | 50 |
| 4 | sea | 28 |
| 5 | zhangshan | 26 |
| 3 | wangqing | 25 |
+----+-----------+------+
4 rows in set (0.00 sec)
9.查询student表中名字叫zhangshan的记录
MariaDB [yanlei]> select * from student where name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
+----+-----------+------+
2 rows in set (0.00 sec)
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
MariaDB [yanlei]> select * from student where age > 20 and name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
+----+-----------+------+
1 row in set (0.00 sec)
11.查询student表中年龄在23到30之间的记录
MariaDB [yanlei]> select * from student where age between 23 and 30;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sea | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
4 rows in set (0.00 sec)
12.修改wangwu的年龄为100
MariaDB [yanlei]> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [yanlei]> select * from student where name = 'wangwu';
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.00 sec)
13.删除student中名字叫zhangshan且年龄小于等于20的记录
MariaDB [yanlei]> select * from student where age <= 20 and name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 6 | zhangshan | 20 |
+----+-----------+------+
1 row in set (0.00 sec)