您的位置:首页 > 数据库 > MySQL

零基础之MySQL学习笔记(三)

2020-08-08 19:30 513 查看

一. alter语句修改表结构
1.创建表及显示表

2.增加列
alter table demo1 add salart int(10);

3.删除列
alter table demo1 drop salart;

4.更改列类型
alter table demo1 modify age tinyint(5);

5.更改列名
alter table demo1 change age age1 int(5);

6.更改表名
方式一:alter table demo1 rename demo2;
方式二:rename table demo2 to demo1;

二. DML语句
DDL:数据库模式定义语句,关键字:create
DML:数据操作语句,关键字:insert 、delect 、update
DCL:数据库控制语句,关键字:grant 、 remove
DQL:数据库查询语句,关键字:select
1.插入数据
insert into 表名(col_name,col_name…)values(val,val,…);
insert into 表名 values(val,val…);
insert into 表名(col_name) values(val);
2.删除数据
delete from 表名;(删除数据保留表结构,最慢,可以找回)
truncate table 表名;(删除数据保留表结构,一次性删除所有数据,相对较快,不能找回)
drop table 表名;(删除数据和表结构,直接删除本地文件,最快,数据不能找回)
删除指定行数据:
delete from 表名 where col_name=‘val’;
3.修改数据
update 表名 set col_name=‘val’;
update 表名 set col_name=‘val’ where col_name2=‘val’;
4.给指定列初始值
create table 表名(name varchar(20) default ‘张三‘,id int(5) default’5’);

三. DQL语句
1.查看若干列
select col_name,col_name2 from 表名;
2.取别名
select col_name '别名’ from 表名;
3.合并列显示
select concat(col_name,col_name2) ‘别名+别名2’ from 表名;
select concat(col_name,”++“,col_name2) ‘别名+别名2’ from 表名;
4.只查看若干列
select * from 表名 where 条件;
5.查找并去重
select distinct * from 表名;
6.可以结合算术表达式使用
select age*10 from demo1;

7.查找指定区间数据
select * from 表名 where 条件1 AND 条件2;
select * from 表名 where col_name between 条件1 and 条件2;
eg:select * from 表名 where salary between 500 and 3000;
8.查null值
is匹配固定值
select * from 表名 where col_name is null;
9.模糊查询
like利用%和/
查询名字中带a的:select * from 表名 where name like ‘%a%’;

查询名字中以a开头的:select * from 表名 where name like ‘a%’;
查询名字长度为2并以a结尾的:select * from 表名 where name like binary ‘_a’;
10.与或非
select * from 表名 where 条件1 and 条件2;
select * from 表名 where 条件1 or 条件2;
select * from 表名 col_name not 条件;
11.对结果排序
利用order by
select * from 表名 where salary between 500 and 3000 order by ase;
四. 事物

  • 原子性
  • 一致性
  • 隔离性
  • 持久性

五.多表连接
1.交叉连接——返回两张表记录的乘积
select * from 表名1 cross join 表名2;
select * from 表名1,表名2;

2.内连接——join in加on条件
select * from stu inner join teacher on stu.book=teacher.book;

六.索引
为了提高查询效率,所以把数据中具有代表性的数据提取出来单独存储,下次查询通过快速路径访问方法快速定位数据减少磁盘的I/O,功能跟目录类似
七.外键

  • 外键:一张表的主键列,拿到另一张表中使用
    (外键可以重复,可以为空)
  • alter语句添加外键:
    alter table 子表名 add constraint 外键名 foreign key(子表列名) references 主表名(对应列名) on delete cascade on update cascade;

    (外键中的on delete 和on update表示:在主表(外键对应的主键所在的表)中主键数据发生删除或更新时子表应该做什么,cascade表示从父表删除和更新时子表中匹配的行一样删除或更新;)
  • 例子
    两个实体:
    一对多——>主键对应外键
    eg:一个学校有多个学生
mysql> alter table student add constraint fk1 foreign key(schoolid) references school(id) on delete cascade on update cascade;
Query OK, 5 rows affected (0.08 sec)
Records: 5  Duplicates: 0  Warnings: 0

一对一——>外键唯一
eg:一个学生只有一个课桌,一个课桌只能坐一个学生

mysql> alter table desk add constraint fk2 foreign key(studentid) references student(id) on delete cascade on update cascade;
Query OK, 3 rows affected (0.07 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> alter table desk add constraint unique(studentid);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

多对多——>关系表,两个表的主键作为第三张表的外键,然后把两个外键设置为联合主键(联合唯一)
eg:一个学生多门课,一门课可以多个学生学习

使两者的组合唯一不重复

mysql> alter table stu_course add constraint unique(studentid,courseid);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

八.触发器
当另一张表数据发生变化时,当前表数据也发生相应改变;
eg:给orderlist添加触发器,当其orderNum添加后store的storeNum相应变化

mysql> delimiter##
mysql> create trigger tg1 after insert on orderlist for each row
-> begin
-> update store set storeNum=storeNum-new.orderNum where storeid=new.orderid;
-> end
-> ##
Query OK, 0 rows affected (0.01 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: