语法:mysql [OPTIONS] [database]
常用的OPTIONS:
-uUSERNAME //指定用户名,默认为root
-hHOST //指定服务器主机,默认为localhost
-pPASSWORD //指定用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本
[root@139 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.38, for Linux (x86_64) using EditLine wrapper
[root@139 ~]# mysql -uroot -p123456 -hlocalhost
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
[root@139 ~]# mysql -uroot -p123456 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
MySQL中定义数据字段的类型对数据库的优化是非常重要的。MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。
数值型
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
---|---|---|---|---|
tinyint | 1byte | -128~127 | 0~255 | 小整数值 |
smallint | 2bytes | -32768~32767 | 0~65535 | 大整数值 |
mediumint | 3bytes | -8388608~8388607 | 0~16777215 | 大整数值 |
int | 4bytes | -2147483648~2147483646 | 0~4294967295 | 大整数值 |
bigint | 8bytes | -9223372036854775808~9223372036854775807 | 0~18446744073709551615 | 极大整数值 |
float | 4bytes | -3.402823644E+38~-1.175494351E-38 | 0,1.175494351E-38~3.402823644E+38 | 单精度 浮点数值 |
double | 8bytes | -1.7976931348623157E+308~-2.2250738585072014E-308 | 0,2.2250738585072014E-308~1.7976931348623157E+308 | 双精度 浮点数值 |
decimal | 对DECIMAL(M,D),如果M>D,为M+2否则为D+2 | 依赖于M和D的值 | 依赖于M和D的值 | 小数值 |
字符串型
类型 | 大小 | 用途 |
---|---|---|
char | 0~255bytes | 定长字符串 |
varchar | 0~65535bytes | 可变字符串 |
tinyblob | 0~255bytes | 不超过255字符串的二进制字符串 |
tinytext | 0~255bytes | 短文本字符串 |
blob | 0~65535bytes | 二进制形式的长文本数据 |
text | 0~65535bytes | 长文本数据 |
binary(N) | 0~N bytes | 允许长0~N个字节的变长字节字符集 |
mediumblob | 0~16777215 bytes | 二进制形式的中等长度文本数据 |
mediumtext | 0~16777215 bytes | 中等长度文本数据 |
longblob | 0~4294967295 bytes | 二进制形式的极大文本数据 |
longtext | 0~4294967295 bytes | 极大文本数据 |
日期和时间型
类型 | 大小 | 范围 | 格式 | 用途 |
---|---|---|---|---|
year | 1 byte | 1901~2155 | YYYY | 年份值 |
time | 3 bytes | -838:59:59~838:59:59 | HH:MM:SS | 时间值 |
date | 4 bytes | 1000-01-01~9999-12-31 | YYYY-MM-DD | 日期值 |
datetime | 8 bytes | 1000-01-01 00:00:00~9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期 |
timestamp | 4 bytes | 1970-01-01 00:00:01~2038-01-19 03:14:07 | YYYY-MM-DD HH:MM:SS | 混合日期 |
创建数据库
语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
删除数据库
语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
mysql> drop database test;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
创建表
语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
mysql> use test;
Database changed
mysql> create table test1(id int not null,name varchar(20) not null,age tinyint(3),class varchar(10));
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test1 |
+----------------+
1 row in set (0.00 sec)
删除表
语法:DROP TABLE [ IF EXISTS ] 'table_name';
mysql> drop table test1;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
用户创建
语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
mysql> create user 'keven'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
测试
[root@139 ~]# mysql -ukeven -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
删除数据库用户
语法:DROP USER 'username'@'host';
mysql> drop user 'keven'@'localhost';
Query OK, 0 rows affected (0.00 sec)
查看支持的所有字符集
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 |
...
查看当前数据库支持的所有存储引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
查看数据库信息
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.01 sec)
不进入数据库列出其所有表
mysql> show tables from test;
+----------------+
| Tables_in_test |
+----------------+
| test1 |
+----------------+
1 row in set (0.00 sec)
查看表结构
语法:DESC [db_name.]table_name;
mysql> desc test.test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) | YES | | NULL | |
| class | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
查看表的创建命令
语法:SHOW CREATE TABLE table_name;
mysql> show create table test1;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test1 | CREATE TABLE `test1` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`age` tinyint(3) DEFAULT NULL,
`class` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
查看表的状态
语法:SHOW TABLE STATUS LIKE 'table_name'\G
mysql> show table status like 'test1'\G
*************************** 1. row ***************************
Name: test1
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2022-07-25 19:07:16
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
语法:HELP keyword;
mysql> help create database;
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_option] ...
create_option: [DEFAULT] {
CHARACTER SET [=] charset_name
| COLLATE [=] collation_name
}
CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.
URL: https://dev.mysql.com/doc/refman/5.7/en/create-database.html
语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
mysql> insert into test1(id,name,age,class) value(1,'yi',20,'one');
Query OK, 1 row affected (0.00 sec)
一次插入一条数据
mysql> insert into test1(id,name,age,class) values(2,'er',21,'two'),(3,'san',20,'three');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
一次插入多条数据
字段column表示
条件判断语句where
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
条件逻辑操作 | AND OR NOT |
order by:排序,默认为升序
语句 | 意义 |
---|---|
order by ‘字段名’ | 根据字段名进行升序排序 |
order by ‘字段名’ desc | 根据字段名进行降序排序 |
order by ‘字段名’ limit 2 | 根据字段名进行升序排序,只取前2个结果 |
order by ‘字段名’ limit 1,2 | 根据字段名进行升序排序,略过第一个结果取后面两个结果 |
order by ‘字段名’ desc limit 2 | 根据字段名进行降序排序,只取前2个结果 |
语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from test1; //查询表中所有数据
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 1 | yi | 20 | one |
| 2 | er | 21 | two |
| 3 | san | 20 | three |
+----+------+------+-------+
3 rows in set (0.00 sec)
mysql> select name from test1; //查询表中指定字段
+------+
| name |
+------+
| yi |
| er |
| san |
+------+
3 rows in set (0.00 sec)
mysql> select * from test1 order by age; //查询表中内容并以age字段进行升序排序
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 1 | yi | 20 | one |
| 3 | san | 20 | three |
| 2 | er | 21 | two |
+----+------+------+-------+
3 rows in set (0.00 sec)
mysql> select * from test1 order by age desc; //查询表中内容并以age字段进行降序排序
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 2 | er | 21 | two |
| 1 | yi | 20 | one |
| 3 | san | 20 | three |
+----+------+------+-------+
3 rows in set (0.00 sec)
mysql> select * from test1 order by age limit 2; //查询表中内容并以age字段进行升序排序并且只取前2个结果
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 1 | yi | 20 | one |
| 3 | san | 20 | three |
+----+------+------+-------+
2 rows in set (0.00 sec)
mysql> select * from test1 order by age limit 1,2; //查询表中内容并以age字段进行升序排序并且略过第一个结果取后两个结果
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 3 | san | 20 | three |
| 2 | er | 21 | two |
+----+------+------+-------+
2 rows in set (0.00 sec)
mysql> select * from test1 where age >=21; //查询表中age字段大于等于21的
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 2 | er | 21 | two |
| 4 | si | 24 | four |
| 5 | wu | 22 | two |
+----+------+------+-------+
3 rows in set (0.00 sec)
mysql> select * from test1 where age >=21 and name = 'si'; //查询表中age字段大于等于21并且名字是si的
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 4 | si | 24 | four |
+----+------+------+-------+
1 row in set (0.00 sec)
mysql> select * from test1 where age between 21 and 24; //查询表中age字段是21到24之间的包含21和24
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 2 | er | 21 | two |
| 4 | si | 24 | four |
| 5 | wu | 22 | two |
+----+------+------+-------+
3 rows in set (0.00 sec)
语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from test1 where name = 'er';
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 2 | er | 21 | two |
+----+------+------+-------+
1 row in set (0.00 sec)
mysql> update test1 set age = 23 where name = 'er'; //将name为er的数据的age字段更新为23
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test1 where name = 'er';
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 2 | er | 23 | two |
+----+------+------+-------+
1 row in set (0.00 sec)
语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from test1;
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 1 | yi | 20 | one |
| 2 | er | 23 | two |
| 3 | san | 20 | three |
| 4 | si | 24 | four |
| 5 | wu | 22 | two |
| 6 | liu | 18 | one |
+----+------+------+-------+
6 rows in set (0.00 sec)
mysql> delete from test1 where name = 'si'; //删除表里的某条记录
Query OK, 1 row affected (0.00 sec)
mysql> select * from test1;
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 1 | yi | 20 | one |
| 2 | er | 23 | two |
| 3 | san | 20 | three |
| 5 | wu | 22 | two |
| 6 | liu | 18 | one |
+----+------+------+-------+
5 rows in set (0.00 sec)
mysql> delete from test1; //删除某个表里的所有数据,保留字段
Query OK, 5 rows affected (0.00 sec)
mysql> select * from test1;
Empty set (0.00 sec)
mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) | YES | | NULL | |
| class | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 可以使用where指定条件从而实现删除部分数据 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据 并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 只能删除表的所有数据 |
语法:TRUNCATE table_name;
mysql> select * from test1;
+----+------+------+-------+
| id | name | age | class |
+----+------+------+-------+
| 1 | yi | 20 | ont |
| 2 | er | 21 | two |
| 4 | si | 24 | four |
| 5 | wu | 22 | two |
| 6 | liu | 18 | one |
+----+------+------+-------+
5 rows in set (0.00 sec)
mysql> truncate test1; //删除表中所有数据,保留字段
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test1;
Empty set (0.00 sec)
mysql> desc test1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) | YES | | NULL | |
| class | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
权限类型 | 代表 |
---|---|
all | 所有权限 |
select | 读取内容权限 |
insert | 插入内容的权限 |
update | 更新内容的权限 |
delete | 删除内容的权限 |
指定操作的对象
表示方式 | 意义 |
---|---|
*.* |
所有库的所有表 |
db_name | 指定库的所有表 |
db_name.table_name | 指定库的指定表 |
语法:GRANT priv_type,... ON [object_type] db_name.table_name TO ‘username'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];
//授权用户在本机上能访问登录所有数据库
mysql> grant all on *.* to 'keven'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@139 ~]# mysql -ukeven -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
//授权root用户在所有主机上访问登录所有数据库
mysql> grant all on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//查看当前登陆用户的授权信息
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//查看指定用户的授权信息
mysql> show grants for root;
+-------------------------------------------+
| Grants for root@% |
+-------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' |
+-------------------------------------------+
1 row in set (0.00 sec)
mysql> show grants for 'root'@'192.168.159.139';
+---------------------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.159.139' |
+---------------------------------------------------------+
1 row in set (0.00 sec)
语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> revoke all on *.* from 'root'@'192.168.159.139';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
1.创建一个以你名字为名的数据库,并创建一张表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)
答
mysql> create database keven;
Query OK, 1 row affected (0.00 sec)
mysql> use keven;
Database changed
mysql> create table student(id int(11) not null primary key auto_increment,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.00 sec)
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.00 sec)
2.查看下该新建的表有无内容(用select语句)
mysql> select * from student;
Empty set (0.00 sec)
3.往新建的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 |
+----+-------------+------+
答
mysql> insert into student(id,name,age) values(1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangsan',26),(6,'zhangsan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
4.修改lisi的年龄为50
mysql> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name = 'lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
+----+------+------+
1 row in set (0.00 sec)
5.以age字段降序排序
mysql> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 6 | zhangsan | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
+----+-------------+------+
11 rows in set (0.00 sec)
6.查询student表中年龄最小的3位同学跳过前2位
mysql> select * from student order by age limit 2,1;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 10 | qiuyi | 15 |
+----+-------------+------+
1 rows in set (0.00 sec)
7.查询student表中年龄最大的4位同学
mysql> select * from student order by age desc limit 4;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 3 | wangqing | 25 |
+----+----------+------+
4 rows in set (0.00 sec)
8.查询student表中名字叫zhangshan的记录
mysql> select * from student where name = 'zhangsan';
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
9.查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from student where name = 'zhangsan' and age > 20;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
+----+----------+------+
1 row in set (0.00 sec)
10.查询student表中年龄在23到30之间的记录
mysql> select * from student where age between 23 and 30;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
+----+----------+------+
4 rows in set (0.00 sec)
11.修改wangwu的年龄为100
mysql> update student set age =100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name = 'wangwu';
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.00 sec)
12.删除student中名字叫zhangshan且年龄小于等于20的记录
mysql> delete from student where name = 'zhangsan' and age <= 20;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.00 sec)