文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

MySQL

2020-04-23 20:13

关注


	MySQL
[数据库教程]

萌芽阶段

数据库的产生

核心:CRUD(最难的是“查找”)

数据库基本操作

create database if not exists `student`;
drop database if exists student;
show create database `teacher`;
create database if not exists `student` charset=gbk;
alter database teacher charset=gbk;

use frank_school;
show tables;
create table student(
  id int,
  name varchar(30),
  age int
  );
create table teacher(
    id int auto_increment primary key comment ‘主键id‘,
    
    #auto_increment 自动增长
    #primary key 主键,最主要的,用来区分
    #comment 注释
    
    name varchar(30) not null,
    #not null 不能为空
    
    phone varchar(20) comment ‘电话号码‘,
    address varchar(100) default ‘暂时未知‘ comment ‘住址‘
    #default 默认值,此处为‘暂时未知‘
    
    )engine=innodb;
desc student;
desc teacher;
drop table if exists oooo, stu,jjj;
#默认在最后面
alter table student add phone varchar(20);

#指定插入在哪,在name前面
alter table student add phone varchar(20) after name;

#在最前面
alter table student add phone varchar(20) first;
drop table s;
alter table student drop address;in
#既可以修改名字,也可以修改类型
alter table student change phone tel_phone int (11);

#只改变类型
alter table student modify tel_phone int (13);

数据操作

insert into teacher (id, name, phone, address) values(1, ‘Frank‘, ‘1888888‘, ‘ShangHai‘);

# 这样就必须按照创建表的时候的顺序填写
insert into teacher values(1, ‘Frank‘, ‘1888888‘, ‘ShangHai‘);
select * from teacher;
# *:全部
# from + 表
# 删除id为6的数据
delete from teacher where id=6;

# 删除大于三十岁的数据
delete from student where age>30;

# 清除表的所有数据(不建议:慢)
# 坏处:再创建数据的时候,id等自增数据,会接着被清空之前的id自动增长
delete from teacher;

# 建议这种方法清除数据
# 好处:id等自增数据,不会接着被清空之前的id
truncate table student;
# 将id=1的name,改为frank
# 注意where后面的,注意不要有重复的id等!!!!
update teacher set name=‘frank‘ where id=1;

# 也可以改多个数据
update teacher set name=‘frank‘,phone=‘1111‘ where id=1;

# 如果不写where,那么将改变所有的数据
update teacher set name=‘frank‘;

# SQL注入攻击就是改变where

# 多个条件的情况
# 将phone为1111111或者2222的数据修改address的值
update teacher set address=‘shanghai‘ where phone=‘1111111‘ or phone=‘2222‘;
# 查询teacher表中的id跟phone的数据
select id,phone from teacher;

# 查询teacher表中所有的数据
select * from teacher;
show variables like ‘character_set_%‘;
set character_set_client=utf8;

列属性完整性

alter table t_11 drop index phone;
// 执行后,phone不是唯一键了
create table t_12(
id int(20),
name varchar(20) comment ‘姓名‘
);
// comment注释
show create table t_12;
// 会显示怎么创建的,包括注释都可以看到
create table eatery(
id int primary key,
money decimal(10,4),
stuId int(4),
foreign key (stuId) references stu(stuId)
);
// 从stu表中的stuId创建外键
show create table eatery;

+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table
                                                                                                      |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| eatery | CREATE TABLE `eatery` (
  `id` int(11) NOT NULL,
  `money` decimal(10,4) DEFAULT NULL,
  `stuId` int(4) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `stuId` (`stuId`),
  CONSTRAINT `eatery_ibfk_1` FOREIGN KEY (`stuId`) REFERENCES `stu` (`stuId`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.04 sec)
alter table eatery drop foreign key eatery_ibfk_1;
 // 创建主表
 create table stu(
    -> stuId int(4) primary key,
    -> name varchar(20)
    -> );
 // 创建从表
   create table eatery(    
    -> id int (4) primary key,
    -> money decimal(10,4),
    -> stuId int(4),
    -> foreign key(stuId) references stu(stuId) on delete set null on update cascade 
       // on delete set null:当删除时,置空
       // on update cascade:当更新时,级联
    -> );
    

数据库设计

单表查询

mysql> select 3*4;
+-----+
| 3*4 |
+-----+
|  12 |
+-----+
1 row in set (0.00 sec)

// 起“别名”
mysql> select 2*6 as res;
+-----+
| res |
+-----+
|  12 |
+-----+
1 row in set (0.00 sec)
mysql> select * from t1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | frank |
|    2 | jerry |
+------+-------+
2 rows in set (0.00 sec)

mysql> select * from t2; 
+--------+--------+
| score1 | score2 |
+--------+--------+
|     98 |     99 |
|     90 |     77 |
+--------+--------+
2 rows in set (0.00 sec)

mysql> select * from t1,t2;
// 返回笛卡尔积
+------+-------+--------+--------+
| id   | name  | score1 | score2 |
+------+-------+--------+--------+
|    1 | frank |     98 |     99 |
|    2 | jerry |     98 |     99 |
|    1 | frank |     90 |     77 |
|    2 | jerry |     90 |     77 |
+------+-------+--------+--------+
4 rows in set (0.00 sec)
mysql> select * from t4;
+------+----------+
| id   | address  |
+------+----------+
|    1 | shanghai |
|    2 | shanghai |
|    3 | beijing  |
|    4 | shenzhen |
+------+----------+
4 rows in set (0.00 sec)

// where限定查找某一个范围
mysql> select * from t4 where address = ‘shanghai‘;
+------+----------+
| id   | address  |
+------+----------+
|    1 | shanghai |
|    2 | shanghai |
+------+----------+
2 rows in set (0.00 sec)

mysql> select * from t4 where address = ‘shenzhen‘;  
+------+----------+
| id   | address  |
+------+----------+
|    4 | shenzhen |
+------+----------+
1 row in set (0.00 sec)

mysql> select * from t4 where address = ‘shenzhen ‘;
+------+----------+
| id   | address  |
+------+----------+
|    4 | shenzhen |
+------+----------+
1 row in set (0.00 sec)
// 跟or一样
mysql> select * from t4 where address in(‘beijing‘,‘shanghai‘);
+------+----------+
| id   | address  |
+------+----------+
|    1 | shanghai |
|    2 | shanghai |
|    3 | beijing  |
+------+----------+
3 rows in set (0.00 sec)
mysql> select * from t3 where age between 15 and 19; 
+------+------+
| id   | age  |
+------+------+
|    1 |   18 |
|    2 |   19 |
|    3 |   19 |
|    4 |   16 |
|    5 |   17 |
+------+------+
5 rows in set (0.00 sec)
// 查找为空的
// is not null
mysql> select * from t3 where age is null;
+------+------+
| id   | age  |
+------+------+
|    7 | NULL |
+------+------+
1 row in set (0.00 sec)
mysql> select * from score;            
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    1 |      98 |      99 |  100 |
|    2 |      90 |      95 |   85 |
+------+---------+---------+------+
2 rows in set (0.00 sec)

// 求和
mysql> select sum(chinese) from score;
+--------------+
| sum(chinese) |
+--------------+
|          188 |
+--------------+
1 row in set (0.00 sec)

// 平均值
mysql> select avg(chinese) from score; 
+--------------+
| avg(chinese) |
+--------------+
|      94.0000 |
+--------------+
1 row in set (0.00 sec)

// 最大值
mysql> select max(chinese) from score; 
+--------------+
| max(chinese) |
+--------------+
|           98 |
+--------------+
1 row in set (0.00 sec)

// 最小值
mysql> select min(chinese) from score; 
+--------------+
| min(chinese) |
+--------------+
|           90 |
+--------------+
1 row in set (0.00 sec)

// 求数量(很坑,不建议用)
mysql> select count(*) from score;     
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)
mysql> select * from student;
+------+--------+--------+------+-----------+
| id   | name   | gender | age  | tel_phone |
+------+--------+--------+------+-----------+
|    1 | 张三   | 0      |   18 | 155555    |
|    2 | 张四   | 1      |   20 | 16541654  |
|    3 | 李四   | 1      |   22 | 1231      |
|    4 | 张某某 | 0      |   15 | 15646     |
+------+--------+--------+------+-----------+
4 rows in set (0.00 sec)

// %代表多个字符
mysql> select * from student where name like ‘张%‘;
+------+--------+--------+------+-----------+
| id   | name   | gender | age  | tel_phone |
+------+--------+--------+------+-----------+
|    1 | 张三   | 0      |   18 | 155555    |
|    2 | 张四   | 1      |   20 | 16541654  |
|    4 | 张某某 | 0      |   15 | 15646     |
+------+--------+--------+------+-----------+
3 rows in set (0.00 sec)

// _代表一个字符
mysql> select * from student where name like ‘张_‘;
+------+------+--------+------+-----------+
| id   | name | gender | age  | tel_phone |
+------+------+--------+------+-----------+
|    1 | 张三 | 0      |   18 | 155555    |
|    2 | 张四 | 1      |   20 | 16541654  |
+------+------+--------+------+-----------+
2 rows in set (0.00 sec)
mysql> select * from score;
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    1 |      98 |      99 |  100 |
|    2 |      90 |      95 |   85 |
|    3 |      87 |      79 |   99 |
+------+---------+---------+------+
3 rows in set (0.00 sec)

// asc升序
mysql> select * from score order by english asc;
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    3 |      87 |      79 |   99 |
|    2 |      90 |      95 |   85 |
|    1 |      98 |      99 |  100 |
+------+---------+---------+------+
3 rows in set (0.00 sec)

// desc降序
mysql> select * from score order by english desc; 
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    1 |      98 |      99 |  100 |
|    2 |      90 |      95 |   85 |
|    3 |      87 |      79 |   99 |
+------+---------+---------+------+
3 rows in set (0.00 sec)
mysql> select * from info;
+----+------+--------+---------+
| id | age  | gender | address |
+----+------+--------+---------+
|  1 |   25 | 男     | 上海    |
|  2 |   23 | 女     | 上海    |
|  3 |   24 | 女     | 北京    |
|  4 |   26 | 男     | 北京    |
|  5 |   22 | 男     | 上海    |
+----+------+--------+---------+
5 rows in set (0.00 sec)

// 按照性别分组统计平均年龄
// 1.可以分组2.聚合函数
mysql> select avg(age) as ‘年龄‘,gender as ‘性别‘ from info group by gender;
+---------+------+
| 年龄    | 性别 |
+---------+------+
| 24.3333 | 男   |
| 23.5000 | 女   |
+---------+------+
2 rows in set (0.00 sec)
mysql> select * from student;
+----+------+-------+--------+---------+
| id | age  | name  | gender | phone   |
+----+------+-------+--------+---------+
|  1 |   18 | ????  | 0      | 115555  |
|  2 |   20 | ??    | 1      | 5621654 |
|  3 |   31 | ????? | 1      | 15646   |
|  4 |   32 | ??    | 1      | 4856748 |
+----+------+-------+--------+---------+
4 rows in set (0.00 sec)

// 根据gender将id分组,进行聚合显示
mysql> select group_concat(id),gender from student group by gender;
+------------------+--------+
| group_concat(id) | gender |
+------------------+--------+
| 1                | 0      |
| 2,3,4            | 1      |
+------------------+--------+
2 rows in set (0.01 sec)
mysql> desc info;
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | int(11)      | NO   | PRI | NULL    |       |
| age     | int(11)      | YES  |     | NULL    |       |
| gender  | varchar(5)   | YES  |     | NULL    |       |
| address | varchar(255) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

// having是在查询之后的数据进行筛选(已经查询之后的)
mysql> SELECT avg(age) AS ‘age‘,address AS ‘address‘ FROM info GROUP BY address HAVING age > 24;
+---------+---------+
| age     | address |
+---------+---------+
| 24.5000 | beijing |
+---------+---------+
1 row in set (0.00 sec)
mysql> select * from info
    -> ;
+----+------+--------+----------+
| id | age  | gender | address  |
+----+------+--------+----------+
|  1 |   25 | 0      | shanghai |
|  2 |   23 | 1      | shanghai |
|  3 |   26 | 1      | beijing  |
|  4 |   23 | 0      | beijing  |
|  5 |   21 | 0      | shanghai |
|  6 |   24 | 1      | shenzhen |
+----+------+--------+----------+
6 rows in set (0.00 sec)

// 从下标为0开始,查两个
mysql> select * from info limit 0,2;
+----+------+--------+----------+
| id | age  | gender | address  |
+----+------+--------+----------+
|  1 |   25 | 0      | shanghai |
|  2 |   23 | 1      | shanghai |
+----+------+--------+----------+
2 rows in set (0.00 sec)

// 降序前三个
mysql> select * from info order by age desc limit 3;
+----+------+--------+----------+
| id | age  | gender | address  |
+----+------+--------+----------+
|  3 |   26 | 1      | beijing  |
|  1 |   25 | 0      | shanghai |
|  6 |   24 | 1      | shenzhen |
+----+------+--------+----------+
3 rows in set (0.00 sec)
mysql> select distinct address from info;
+----------+
| address  |
+----------+
| shanghai |
| beijing  |
| shenzhen |
+----------+
3 rows in set (0.00 sec)

// 去除重复的,计算个数
mysql> select count(distinct address) from info;
+-------------------------+
| count(distinct address) |
+-------------------------+
|                       3 |
+-------------------------+
1 row in set (0.00 sec)

多表查询

mysql> select * from teacher;
+------+------+--------+----------+
| id   | age  | gender | address  |
+------+------+--------+----------+
|    1 |   25 | 0      | shanghai |
|    2 |   23 | 1      | shanghai |
|    3 |   26 | 1      | beijing  |
|    4 |   23 | 0      | beijing  |
|    5 |   21 | 0      | shanghai |
|    6 |   24 | 1      | shenzhen |
+------+------+--------+----------+
6 rows in set (0.00 sec)

mysql> select * from info;    
+----+------+--------+----------+
| id | age  | gender | address  |
+----+------+--------+----------+
|  1 |   25 | 0      | shanghai |
|  2 |   23 | 1      | shanghai |
|  3 |   26 | 1      | beijing  |
|  4 |   23 | 0      | beijing  |
|  5 |   21 | 0      | shanghai |
|  6 |   24 | 1      | shenzhen |
+----+------+--------+----------+
6 rows in set (0.00 sec)

mysql> select age,gender from info union select `name`,phone from student;
+-------+---------+
| age   | gender  |
+-------+---------+
| 25    | 0       |
| 23    | 1       |
| 26    | 1       |
| 23    | 0       |
| 21    | 0       |
| 24    | 1       |
| ????  | 115555  |
| ??    | 5621654 |
| ????? | 15646   |
| ??    | 4856748 |
+-------+---------+
10 rows in set (0.00 sec)
// 查name和score
mysql> select 
       		name,
       		score 
       from 
       		student 
       		inner join score on student.id=score.stuid;
+----------+-------+
| name     | score |
+----------+-------+
| zhangsan | 89.00 |
| ren      | 80.00 |
| lalala   | 70.00 |
+----------+-------+
3 rows in set (0.00 sec)
// 以左表为基准,该方式student为左表
mysql> select
    -> name,
    -> score
    -> from
    -> student left join score
    -> on student.id = score.stuid;
+----------+-------+
| name     | score |
+----------+-------+
| zhangsan | 89.00 |
| ren      | 80.00 |
| lalala   | 70.00 |
| moumou   |  NULL |
+----------+-------+
4 rows in set (0.00 sec)

子查询

mysql> select * from score;
+----+-------+-------+
| id | stuid | score |
+----+-------+-------+
|  1 |     1 | 89.00 |
|  2 |     3 | 80.00 |
|  3 |     4 | 70.00 |
+----+-------+-------+
3 rows in set (0.02 sec)

// 多个结果要用in不用=,还有not in
mysql> select * from student where id in (select stuid from score where score >= 75);
+----+------+----------+--------+--------+
| id | age  | name     | gender | phone  |
+----+------+----------+--------+--------+
|  1 |   18 | zhangsan | 0      | 115555 |
|  3 |   31 | ren      | 1      | 15646  |
+----+------+----------+--------+--------+
2 rows in set (0.01 sec)
mysql> select * from score;
+----+-------+-------+
| id | stuid | score |
+----+-------+-------+
|  1 |     1 | 89.00 |
|  2 |     3 | 80.00 |
|  3 |     4 | 70.00 |
+----+-------+-------+
3 rows in set (0.00 sec)

mysql> select * from student where exists (select stuid from score where score >=85);
+----+------+----------+--------+---------+
| id | age  | name     | gender | phone   |
+----+------+----------+--------+---------+
|  1 |   18 | zhangsan | 0      | 115555  |
|  2 |   20 | moumou   | 1      | 5621654 |
|  3 |   31 | ren      | 1      | 15646   |
|  4 |   32 | lalala   | 1      | 4856748 |
+----+------+----------+--------+---------+
4 rows in set (0.00 sec)

mysql> select * from student where exists (select stuid from score where score >=100);
Empty set (0.00 sec)

视图

// 这样就可以只看到student的name和phone,而不用看到其他敏感的数据
create view vw_stu as
select name,phone from student;

// 保存视图之后,就可以直接查视图,而不用再把查询语句写一遍
select * from vw_stu;
// 显示视图,所以必须要用前缀区分
mysql> show tables;
+------------------------+
| Tables_in_frank_school |
+------------------------+
| info                   |
| score                  |
| student                |
| teacher                |
| vw_stu                 |
+------------------------+
5 rows in set (0.00 sec)

mysql> desc vw_stu;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(255) | YES  |     | NULL    |       |
| phone | varchar(20)  | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> show create view vw_stu; 
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View   | Create View

                               | character_set_client | collation_connection |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| vw_stu | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `vw_stu` AS select `student`.`name` AS `name`,`student`.`phone` AS `phone` from `student` | utf8mb4              | utf8mb4_general_ci 
  |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

mysql> show table status where comment = ‘view‘ G 
*************************** 1. row ***************************
           Name: vw_stu
         Engine: NULL
        Version: NULL
     Row_format: NULL
           Rows: NULL
 Avg_row_length: NULL
    Data_length: NULL
Max_data_length: NULL
   Index_length: NULL
      Data_free: NULL
 Auto_increment: NULL
    Create_time: NULL
    Update_time: NULL
     Check_time: NULL
      Collation: NULL
       Checksum: NULL
 Create_options: NULL
        Comment: VIEW
1 row in set (0.00 sec)
// 修改视图要查的东西
mysql> alter view vw_stu as select age,name,phone from student;
Query OK, 0 rows affected (0.01 sec)

// 删除
drop view vw_stu;

事务

 

 

 

 

 

 

 

 

 

MySQL

原文地址:https://www.cnblogs.com/Angela-Joker/p/14602400.html

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯