首页 技术 正文
技术 2022年11月21日
0 收藏 497 点赞 2,302 浏览 5368 个字

一、连接查询

1)进入自己创建的zz数据库

2)创建学生表:

create table students (
id int unsigned not null auto_increment primary key,
name varchar() default '',
age tinyint unsigned default ,
high decimal(,),
gender enum('男', '女', '中性', '保密') default '保密',
cls_id int unsigned default ,
is_delete bit default
);

往学生表写入数据

insert into students values
(,'小明',,180.00,,,),
(,'小月月',,180.00,,,),
(,'彭于晏',,185.00,,,),
(,'刘德华',,175.00,,,),
(,'黄蓉',,160.00,,,),
(,'凤姐',,150.00,,,),
(,'王祖贤',,170.00,,,),
(,'周杰伦儿',,null,,,),
(,'程坤',,181.00,,,),
(,'和珅',,166.00,,,),
(,'刘亦菲',,162.00,,,),
(,'金星',,180.00,,,),
(,'静香',,170.00,,,),
(,'郭静',,167.00,,,),
(,'周杰',,178.00,,,),
(,'钱小豪',,178.00,,,),
(,'谢霆锋',,175.00,,,);

3)创建班级表:

create table classes(
id int unsigned auto_increment primary key not null,
name varchar() not null
);

往班级表里添加数据:

insert into classes values (, '云唯_01期'),(, '云唯_02期');

内关联:  –inner join(在zz数据库下)

1.查询学生对应的班级信息

select * from students as s  inner join  classes as c  on s.cls_id=c.id  order by s.cld_id ;

#将students与classes关联显示并且取别名为s和c。第三个蓝色块则是将具体的字符关联起来,并以学生表的id字段排序,如果查看相反信息则转换students和classes的位置。

2.左关联:

select * from students as s left join classes as c on c.id=s.cls_id order by s.cls_id;

#左关联既是以左边的students为基准关联classes。效果如下:

+----+--------------+------+--------+--------+--------+-----------+------+--------------+
| id | name | age | high | gender | cls_id | is_delete | id | name |
+----+--------------+------+--------+--------+--------+-----------+------+--------------+
| | 小明 | | 180.00 | 女 | | | | 云唯_01期 |
| | 钱小豪 | | 178.00 | 男 | | | | 云唯_01期 |
| | 周杰 | | 178.00 | 男 | | | | 云唯_01期 |
| | 谢霆锋 | | 175.00 | 男 | | | | 云唯_01期 |
| | 周杰伦儿 | | NULL | 男 | | | | 云唯_01期 |
| | 王祖贤 | | 170.00 | 女 | | | | 云唯_01期 |
| | 黄蓉 | | 160.00 | 女 | | | | 云唯_01期 |
| | 彭于晏 | | 185.00 | 男 | | | | 云唯_01期 |
| | 凤姐 | | 150.00 | 保密 | | | | 云唯_02期 |
| | 小月月 | | 180.00 | 女 | | | | 云唯_02期 |
| | 程坤 | | 181.00 | 男 | | | | 云唯_02期 |
| | 和珅 | | 166.00 | 女 | | | | 云唯_02期 |
| | 刘德华 | | 175.00 | 男 | | | | 云唯_02期 |
| | 刘亦菲 | | 162.00 | 中性 | | | NULL | NULL |
| | 静香 | | 170.00 | 男 | | | NULL | NULL |
| | 金星 | | 180.00 | 女 | | | NULL | NULL |
| | 郭静 | | 167.00 | 女 | | | NULL | NULL |
+----+--------------+------+--------+--------+--------+-----------+------+--------------+

因为classes的id没有3,4,5,所以在关联的时候默认为空值显示。

3.右关联

与左关联规则一致,上述命令的left 改为right后id指挥显示1和2,而不是显示3,4,5,因为以右边的classes为基准.

4.自关联

source test.sql                              #将终端下的文件复制到数据库下

以下是关联表:

MariaDB [zz]> select * from areas;
+-----+-----------+------+
| aid | name | pid |
+-----+-----------+------+
| | 北京市 | NULL |
| | 天津市 | NULL |
| | 河北省 | NULL |
| | 山西省 | NULL |
| | 海淀区 | |
| | 滨海区 | |
| | 沧州市 | |
| | 大同市 | |
| | 朝阳区 | |
| | 武清区 | |
| | 石家庄 | |
| | 太原市 | |
| | 西二旗 | |
| | 大港 | |
| | 任丘市 | |
| | 清徐 | |
| | 中关村 | |
| | 汉沽 | |
| | 河间市 | |
| | 阳曲 | |

1)查找北京市下的区

MariaDB [zz]> select * from areas as p inner join areas as c on p.aid=c.pid where p.name=’北京市’ ;

| aid | name      | pid  | aid | name      | pid  |       #自关联并列显示
+-----+-----------+------+-----+-----------+------+
| | 北京市 | NULL | | 海淀区 | |
| | 北京市 | NULL | | 朝阳区 | |

2)查找所有省市关联的区域,并且制作视图

create view v_v as select p.*,c.aid as id ,c.name as n,c.pid as i  from areas as p inner join areas as c where p.aid = c.pid

##蓝色块区域为查看p表下的所有字段以及将c表的name、aid、pid都改成别名显示(因为c表和b表的字段冲突,p、c表也是areas表的别名)

视图**

  • 对于复杂的查询,在多个地方被使用,如果需求发生了改变,需要更改sql语句,则需要在多个地方进行修改,维护起来非常麻烦

create  view 视图名 as select语句;     #创建视图

show tables;                 #查看视图

drop  view 视图名;     #删除视图

# 视图与表同级,所以最好用带‘v’来区分。

 数据库的备份与恢复

#备份某一个数据库
mysqldump -uroot -proot --databases 数据库名 > testdb.sql
#备份所有数据库
mysqldump -uroot -proot --all-databases > all_databases.sql
#备份数据库下的表
mysqldump -uroot -proot 数据库名 表名 > students.sql
#还原数据(sql命令行下)
source testdb.sql(文件名)

事务***

为什么要有事务

   事务具有ACID特性:原子性(A,atomicity)、一致性(C,consistency)、隔离性(I,isolation)、持久性(D,durabulity)。

  • 事务广泛的运用于订单系统、银行系统等多种场景
  • 例如:A用户和B用户是银行的储户,现在A要给B转账500元,那么需要做以下几件事:
      1. 检查A的账户余额>500元;
      2. A 账户中扣除500元;
      3. B 账户中增加500元;
  • 上述3个步骤的操作必须打包在一个事物中,任何一个步骤失败,则必须回滚所有步骤
  • 可以用start transaction或者begin语句开始一个事物,然后要么使用commit提交将修改的数据持久保存,要么使用rollback撤销所有修改,事物sql的样本如下:
    • start transation ;            #开始事务
  1. select balance from checking where customer_id=1024232;
  2. update checking(表名)set  balance=balance-200.00 where customer_id=1024232;
  3. update savings set balance=balance+200.00 where customer_id=1024232;
    1. commit;                   #提交事务
  • 原子性:事务内的所有操作要么都执行,要么都不执行,它是一个不可分割的工作单位。
  • 一致性:数据库总是从一个一致性的状态转换到另一个一致性的状态(在前面的例子当中,一致性确保了,即使在执行第三、四条语句之间系统崩溃,支票账户也不会损失200美元,因为事物最终没有被提交,所以事物中所做的修改也不会保存到数据库中)
  • 隔离性:一个事物所做的修改在最终提交之前,对其他事物是不可见的(在前面的例子中,当执行完第三条语句、第四条语句还未开始时,此时有另外一个账户汇总程序开始运行,则其看到的支票账户的余额并没有被减去200美元。)
  • 持久性:事务完成后,该事务内涉及的数据必须持久性的写入磁盘保证其持久性。当然,这是从事务的角度来考虑的的持久性,从操作系统故障或硬件故障来说,这是不一定的。

事务命令

  • 要求:表的引擎类型必须是innodb类型才可以使用事务,这是mysql表的默认引擎
  • 查看表的创建语句,可以看到engine=innodb
    • 要求:表的引擎类型必须是innodb类型才可以使用事务,这是mysql表的默认引擎
    • 查看表的创建语句,可以看到engine=innodb
    show create table students;
    • 修改数据的命令会触发事务,包括insert、update、delete

    • 开启事务,命令如下:

      • begin
      • 开启事务后执行修改命令,变更会维护到本地缓存中,而不维护到物理表中
    • 提交事务,命令如下

      • 将缓存中的数据变更维护到物理表中
    rollback
    • 回滚事务,命令如下:

      • 放弃缓存中变更的数据

           

实例
--在创建事务之前将表创建好,不能begin后在创建
MariaDB [test]> create table xixi (id int primary key auto_increment,num int unsigned);
MariaDB [test]> insert into xixi values (,),(,),(,);--事务开始
MariaDB [test]> begin;
MariaDB [test]> select * from xixi;
+----+------+
| id | num |
+----+------+
| | |
| | |
| | |
+----+------+
MariaDB [test]> update xixi set num=num- where id=;
MariaDB [test]> update xixi set num=num+ where id=;
MariaDB [test]> select * from xixi;
+----+------+
| id | num |
+----+------+
| | |
| | |
| | |
+----+------+--回滚事务
MariaDB [test]> rollback;MariaDB [test]> select * from xixi;
+----+------+
| id | num |
+----+------+
| | |
| | |
| | |
+----+------+--提交事务
MariaDB [test]> commit;
MariaDB [test]> select * from xixi;
+----+------+
| id | num |
+----+------+
| | |
| | |
| | |

 索引**

  • 思考:在图书馆中是如何找到一本书的?
  • 一般的应用系统对比数据库的读写比例在10:1左右,而且插入操作和更新操作很少出现性能问题,遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,所以查询语句的优化显然是重中之重
  • 当数据库中数据量很大时,查找数据会变得很慢
  • 优化方案:索引

1)查看索引:

方式一:建表时创建索引

create table create_index(
id int primary key,
name varchar() unique,
age int,
key (age) #在字段上设置索引
);

方式二:对于已经存在的表,添加索引

如果指定字段是字符串,需要指定长度,建议长度与定义字段时的长度一致
字段类型如果不是字符串,可以不填写长度部分create index 索引名称 on 表名(字段名称(长度))
例:
create index age_index on create_index(age) #在create_index表上的age字段创建索引
show create table create_index #查看此表是否添加上索引。
  • 删除索引:
drop index 索引名称 on 表名;                  #删除成功则KEY `age_index` (`age`)消失
下一篇: JS创建表格完整
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898