mysql数据库DDL,DML,DCL操作

mysql数据库操作

文章目录

  • mysql数据库操作
    • 1:mysql工具使用
    • 2:服务器监听的两种socket地址
    • 3:mysql数据库数值类型
    • 4:DDL操作
      • 4.1 数据库操作
      • 4.2:表操作
      • 4.3 用户操作
      • 4.4 查看命令show
      • 4.5 获取帮助
    • 5:DML操作
      • 5.1 INSERT语句、
      • 5.2SELECT语句
      • 5.3 update语句
      • 5.4 delete语句
      • 5.5 truncate语句
    • 6:dcl操作
      • 6.1 创建授权 grant
      • 6.2 查看授权
      • 6.3 取消授权revoke
      • 6.4 刷新
    • 7:实战案例

1:mysql工具使用

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

2:服务器监听的两种socket地址

socket类型 说明
ip socket 默认监听在tcp的3306端口,支持远程通信
unix sock 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)仅支持本地通信
server地址只能是:localhost,127.0.0.1

3:mysql数据库数值类型

MySQL中定义数据字段的类型对数据库的优化是非常重要的。MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。

数值类型
MySQL支持所有标准SQL数值数据类型。这些类型包括严格数值数据类型(INTEGER、SMALLINT、DECIMAL和NUMERIC),以及近似数值数据类型(FLOAT、REAL和DOUBLE PRECISION)。

关键字INT是INTEGER的同义词,关键字DEC是DECIMAL的同义词。BIT数据类型保存位字段值,并且支持MyISAM、MEMORY、InnoDB和BDB表。作为SQL标准的扩展,MySQL也支持整数类型TINYINT、MEDIUMINT和BIGINT。下面的表显示了需要的每个整数类型的存储和范围

类型 大小 范围(有符号) 范围(无符号) 用途
tinyint 1byte (-128,127) (0.255) 小整数值
smallint 2bytes (-32768.32767) (0.65535) 大整数值
mediumint 3bytes (-8388608,8.388607) (0,16777215) 大整数值
int或integer 4bytes (-2147483,2147483647) (0,4294967295 大整数值
bigint 8bytes (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615 极大整数值
float 4 bytes (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度,浮点数值
double 8bytes (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度浮点数值
decimal 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 依赖于M和D的值 依赖于M和D的值 小数值

日期和时间类型
表示时间值的日期和时间类型为DATETIME、DATE、TIMESTAMP、TIME和YEAR。每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值。TIMESTAMP类型有专有的自动更新特性,将在后面描述。

类型 大小(bytes) 范围 格式 用途
date 3 1000-01-01/9999-12-31 YYYY-MM-DD 日期值
time 3 ‘-838:59:59’/‘838:59:59’ HH:MM:SS 时间值或持续时间
year 1 1901/2155 YYYY 年份值
datetime 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS 混合日期和时间值
timestamp 4 1970-01-01 00:00:00/2038结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 YYYYMMDD HHMMSS 混合日期和时间值,时间戳

字符串类型
字符串类型指CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM和SET。该节描述了这些类型如何工作以及如何在查询中使用这些类型。

类型 大小(bytes) 用途
char 0-255 定长字符串
varchar 0-65535 变长字符串
tinyblob 0-255 不超过255个字符的二进制字符串
tinytext 0-255 短文本字符串
blob 0-65535 二进制形式的长文本数据
text 0-65535 长文本数据
mediumblob 0-16777215 二进制形式的中等长度文本数据
mediumtext 0-16777215 中等长度文本数据
longblob 0-4294967295 二进制形式的极大文本数据
longtext 0-4294967295 极大文本数据

4:DDL操作

4.1 数据库操作

创建数据库
create database 库名;


mysql> create database nian;
Query OK, 1 row affected (0.00 sec)

查看当前实例有哪些数据库
show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)


删除数据库
drop database 库名;

mysql> drop database nian;
Query OK, 0 rows affected (0.00 sec)

进入数据库
use 库名;


mysql> use nian;
Database changed

4.2:表操作

在数据库里面创建表
create table 表名(字段名1 数据类型,字段名2 数据类型){表选项};//创建表

mysql> create table xuan(id int not null,name varchar(100) ,age tinyint);
Query OK, 0 rows affected (0.00 sec)


查看当前数据库有哪些表
show tables;

mysql> show tables;
+----------------+
| Tables_in_nian |
+----------------+
| xuan           |
+----------------+
1 row in set (0.00 sec)

删除表
drop table +表名(删除表)

mysql> drop table xuan;
Query OK, 0 rows affected (0.00 sec)

4.3 用户操作

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

这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:

IP地址,如:172.16.12.129
通配符
%:匹配任意长度的任意字符,常用于设置允许从任何主机登录
_:匹配任意单个字符

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

这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:

IP地址,如:172.16.12.129
通配符
%:匹配任意长度的任意字符,常用于设置允许从任何主机登录
_:匹配任意单个字符

用户创建
语法:CREATE USER ‘username’@‘host’ [IDENTIFIED BY ‘password’];


mysql> create user 'xuanning'@'127.0.0.1' identified by 'xuanning@123!.Com';
Query OK, 0 rows affected (0.00 sec)

删除数据库用户
DROP USER ‘username’@‘host’;


mysql> drop user 'xuanning'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)

4.4 查看命令show

查看支持的所有字符集
SHOW CHARACTER SET;


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 |
.......................
.......................

查看当前数据库支持的所有存储引擎
SHOW ENGINES;


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       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

查看数据库信息
SHOW DATABASES;


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

不进入某数据库而列出其包含的所有表
SHOW TABLES FROM 库名;


mysql> show tables from nian;
+----------------+
| Tables_in_nian |
+----------------+
| xuan           |
+----------------+
1 row in set (0.00 sec)

查看表结构
语法:DESC [db_name.]table_name;


mysql> desc nian.xuan;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | NO   |     | NULL    |       |
| name  | int(11) | YES  |     | NULL    |       |
| age   | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
3 rows in set (0.03 sec)

查看某表的创建命令
语法:SHOW CREATE TABLE table_name;


mysql> show create table nian.xuan;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                      |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| xuan  | CREATE TABLE `xuan` (
  `id` int(11) NOT NULL,
  `name` int(11) DEFAULT NULL,
  `age` int(11) 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 'xuan'\G
*************************** 1. row ***************************
           Name: xuan
         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-26 09:36:11
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

4.5 获取帮助

获取创建表的帮助
语法:HELP keyword;

mysql> help create table;
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
    (create_definition,...)
    [table_options]

......
......

5:DML操作

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

5.1 INSERT语句、

DML操作之增操作insert
insert into 表名(字段名1,字段名2)values(值1,值2)(在表中插入一条数据(插入多条直接在后面加括号写值即可))
insert into 表名 (字段名3,字段名5)values(值3,值5);(值与字段名对应要插在哪里就打哪里的字段名和其对应的值)、

mysql> insert into xuan(id,name,age) values (1,'zhangsan',20);
Query OK, 1 row affected (0.00 sec)
mysql> insert into xuan(id,name) values (1,'zhangsan');
Query OK, 1 row affected (0.00 sec)
mysql> insert into xuan(id,name) values (1,'zhangsan'),(2,'zhaosi');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0


5.2SELECT语句

字段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];


mysql> select * from xuan; //查看表内所有内容
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   20 |
|  1 | zhangsan | NULL |
|  1 | zhangsan | NULL |
|  2 | zhaosi   | NULL |
+----+----------+------+
4 rows in set (0.00 sec)

mysql> select name from xuan; //查看表内name字段所有的数据
+----------+
| name     |
+----------+
| zhangsan |
| zhangsan |
| zhangsan |
| zhaosi   |
+----------+
4 rows in set (0.00 sec)

mysql> select * from xuan order by age; //以age字段升序排序查询表内所有数据
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan | NULL |
|  1 | zhangsan | NULL |
|  2 | zhaosi   | NULL |
|  1 | zhangsan |   20 |
+----+----------+------+
4 rows in set (0.00 sec)

mysql> select * from xuan order by age desc; //以age字段降序排序查询表内所有数据
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   20 |
|  1 | zhangsan | NULL |
|  1 | zhangsan | NULL |
|  2 | zhaosi   | NULL |
+----+----------+------+
4 rows in set (0.00 sec)

mysql> select * from xuan order by age limit 2; //以age字段升序排序查询表内所有数据,只截取前两行
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  2 | zhaosi   | NULL |
|  1 | zhangsan | NULL |
+----+----------+------+
2 rows in set (0.00 sec)

mysql> select * from xuan order by age desc limit 2;//以age字段降序排序查询表内所有数据,只截取前两行
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   20 |
|  1 | zhangsan | NULL |
+----+----------+------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM xuan WHERE age >= 25;
+----+-----------+-------
| id | name      | age  |
+----+-----------+------+
|  3 | wangqing  |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM xuan WHERE age >= 25 AND name = 'zhangshan';
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
+----+-----------+------+
1 row in set (0.00 sec)

mysql> SELECT * FROM xuan WHERE age BETWEEN 23 and 28;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  2 | jerry     |   23 |
|  3 | xuanning  |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
4 rows in set (0.00 sec)

mysql> select * from xuan where age is not null;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  1 | tom       |   20 |
|  2 | jerry     |   23 |
|  3 | xuanning  |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
+----+-----------+------+
6 rows in set (0.00 sec)

mysql> select * from xuan where age is null;
+----+------+------+
| id | name | age  |
+----+------+------+
|  7 | lisi | NULL |
+----+------+------+
1 row in set (0.00 sec)

5.3 update语句

DML操作之改操作update
语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,…] [WHERE clause] [ORDER BY ‘column_name’ [DESC]] [LIMIT [m,]n];



mysql> update xuan set age=30 where id=1; //修改id为1的数据age为30
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   30 |
|  1 | zhangsan |   30 |
|  1 | zhangsan |   30 |
|  2 | zhaosi   | NULL |
+----+----------+------+
4 rows in set (0.00 sec)




5.4 delete语句

语法:DELETE FROM table_name [WHERE clause] [ORDER BY ‘column_name’ [DESC]] [LIMIT [m,]n];


mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   30 |
|  1 | zhangsan |   30 |
|  1 | zhangsan |   30 |
|  2 | zhaosi   | NULL |
|  3 | wangwu   |   21 |
|  4 | gg       |   40 |
|  5 | issi     |    7 |
+----+----------+------+
7 rows in set (0.00 sec)

mysql> delete from xuan where name='issi'; //删除表内name=issi的数据
Query OK, 1 row affected (0.00 sec)

mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   30 |
|  1 | zhangsan |   30 |
|  1 | zhangsan |   30 |
|  2 | zhaosi   | NULL |
|  3 | wangwu   |   21 |
|  4 | gg       |   40 |
+----+----------+------+
6 rows in set (0.00 sec)

mysql> delete from xuan;  //删除表内所有数据
Query OK, 6 rows affected (0.00 sec)

mysql> select * from xuan;
Empty set (0.00 sec)

5.5 truncate语句

truncate与delete的区别:

语句类型 特点
delete DELETE删除表内容时仅删除内容,但会保留表结构
DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项
可以通过回滚事务日志恢复数据
非常占用空间
truncate 删除表中所有数据,且无法恢复
表结构、约束和索引等保持不变,新添加的行计数值重置为初始值
执行速度比DELETE快,且使用的系统和事务日志资源少
通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放
对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据
不能用于加入了索引视图的表

语法:TRUNCATE table_name;

mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | zhangsan |   20 |
|  2 | zhaosi   |   30 |
|  3 | wangwu   |   40 |
|  4 | issi     |   50 |
+----+----------+------+
4 rows in set (0.00 sec)

mysql> truncate xuan;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from xuan;
Empty set (0.00 sec)

mysql> desc xuan;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(11)      | NO   |     | NULL    |       |
| name  | varchar(100) | YES  |     | NULL    |       |
| age   | tinyint(4)   | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

6:dcl操作

6.1 创建授权 grant

权限类型(priv_type)

权限类型 代表什么?
all 所有权限
select 读取内容的权限
insert 插入内容的权限
update 更新内容的权限
delete 更新内容的权限

指定要操作的对象db_name.table_name

表示方式 意义
*.* 所有库的所有表
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 'xuanning'@'localhost' identified by 'XUANning@123!'; //给予xuan用户在本机对所有库所有表有所有的权限,并设定密码为XUANning@123!
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to 'xuanning'@'%' identified by 'XUANning@123!';//给予xuan用户在所有位置上远程访问对所有库所有表有所有的权限,并设定密码为XUANning@123!
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to 'xuanning'@'127.0.0.1' identified by 'XUANning@123!';//给予xuan用户在本机对所有库所有表有所有的权限,并设定密码为XUANning@123!
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to 'xuanning'@'192.168.226.139' identified by 'XUANning@123!';  //给予xuan用户在192.168.226.139上远程访问对所有库所有表有所有的权限,并设定密码为XUANning@123!
Query OK, 0 rows affected, 1 warning (0.00 sec)


6.2 查看授权

查看当前登录用户的授权信息


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 xuanning;
+-----------------------------------------------+
| Grants for xuanning@%                         |
+-----------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'xuanning'@'%' |
+-----------------------------------------------+
1 row in set (0.00 sec)

6.3 取消授权revoke

mysql> revoke all on *.* from 'xuanning'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> revoke all on *.* from 'xuanning'@'%';
Query OK, 0 rows affected (0.00 sec)


6.4 刷新


mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

7:实战案例

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 xuanning;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
| xuanning           |
+--------------------+
6 rows in set (0.00 sec)
mysql> use xuanning;
Database changed
mysql> create table student (id int 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 | xuanning    |   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 student (name,age) values ('tom',20),('jerry',23),('xuanning',25),('sean',28),('zhangshan',26),('zhangshan',20),('lisi',null),('chenshuo',10),('wangwu',3),('qiuyi',15),('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 | xuanning    |   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.00 sec)


4.修改lisi的年龄为50


mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | xuanning    |   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.00 sec)

mysql> update student set age=50 where name = lisi;
ERROR 1054 (42S22): Unknown column 'lisi' in 'where clause'
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;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | xuanning    |   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.00 sec)


5.以age字段降序排序


mysql> select * from student order by age desc;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  7 | lisi        |   50 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  3 | xuanning    |   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)

6.查询student表中年龄最小的3位同学跳过前2位

mysql> select * from student order by age limit 2,3;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
| 10 | qiuyi       |   15 |
|  1 | tom         |   20 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
3 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 | zhangshan |   26 |
|  3 | xuanning  |   25 |
+----+-----------+------+
4 rows in set (0.00 sec)

8.查询student表中名字叫zhangshan的记录


mysql> select * from student where name='zhangshan';
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
+----+-----------+------+
2 rows in set (0.00 sec)


9.查询student表中名字叫zhangshan且年龄大于20岁的记录

mysql> select * from student where name='zhangshan' and age > 20;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
+----+-----------+------+
1 row in set (0.00 sec)

10.查询student表中年龄在23到30之间的记录


mysql> select * from student where not age <23 and not age > 30;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  2 | jerry     |   23 |
|  3 | xuanning  |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   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.01 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='zhangshan' and not age >20;
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | xuanning    |   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.00 sec)

你可能感兴趣的:(mysql,数据库,服务器)