【学习笔记】mysql

MySQL
1.关系型数据库管理系统,体积小、速度快、成本低、开源。
2.官网下载:https://www.mysql.com/downloads/
3.cmd进入
mysql -uroot -p
4.更改密码:
    SET PASSWORD FOR 'root'@'localhost'=PASSWORD('新密码');
5.命令语句
(1)创建数据库
create database test;
(2)查看当前所有数据库
show databases; 
(3)删除数据库
drop database test;
(4)使用/链接数据库
use test;
(5)查找当前使用/链接的数据库
selete database();
(6)查看表
show tables;
(7)创建表
    创建表tb_score,id为自增主键:
create table if not exists tb_score(id int(4) not null primary key auto_increment,name char(20) not null,score double(16,2));
(8)查看表的结构
①show columns from tb_score;
②desc 表名;
(9)删除表
drop table tb_score;
——数据的增删改查
(10)插入数据
insert into 表名 values(数据1),(数据2),(数据3);
(11)查看表中数据
select * from tb_score;
(12)查找前两位
①根据id排序,默认升序排列:select * from tb_score order by id limit 0.2;
②select * from tb_score limit 0,2;
(13)排序
select * from 表名 order by 字段名 desc;  降序排序
select * from 表名 order by 字段名 asc; 升序排序
(14)where 字句
select * from tb_score where id=1;
(15)and(且)与or(或)链接两个条件
    select * from tb_score where name='zhao' and score>60.00
 (16)修改语句
  ①update tb_score set score=78 where id=1;
  ②update tb_score set name=replace(name,'旧值','新值') where 字段=值;
 (17)删除数据
  删除特定数据:
  delete from tb_score where id=1;
  清空表:
  delete from tb_score; 
——修改表结构
(18)
①更改表结构
alter table 表名 add 字段名 数据类型 default 默认值;
②更改字段的数据类型
alter table 表名 change 旧字段 新字段 新数据类型 default 默认值
(19)删除字段
alter table 表名 drop column 字段名;
(20)重命名表
rename table 旧表名 to 新表名;

6.内连接|左连接|有连接
(1)内连接/等值连接(inner join)
select 表A.字段1,表A.字段2,表B.字段3 from 表A inner join 表B on 表A.字段2=表B.字段2;
(2)左连接(left join)
select 表A.字段1,表A.字段2,表B.字段3 from 表A left join 表A.字段2=表B.字段2;
(3)右连接(right join)
select 表A.字段1,表A.字段2,表B.字段3 from 表A right join 表A.字段2=表B.字段2;
区别:
内连接:两张表都有的值
左连接:以左表为准,读取左表所有选取的字段,即使右表没有对应的值,以null值补全(右边值可能会空)
右连接:以右表为准,读取右表所有选取的字段,即使左表没有对应的值,以null值补全(左边值可能为空)


7.索引:索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构。类似于一本书的目录
(1)创建索引
-对某表中某字段创建一个索引,名为
create index 索引名 on 表名(字段名);
-在创建表的时候直接指定索引
create table 表名(id int(4) not null,name char(20) not null,index ind_test(name));
(2)查看表中索引
show index from score;
(3)删除索引
drop index 索引名 on 表名;
(4)通过alter创建或删除索引
alter table 表名 add index 索引名(字段名);
alter table 表名 drop index 索引名;
(5)创建唯一索引
create unique index 索引名 on 表名(字段名);
(6)普通索引与唯一索引的区别:唯一索引的值必须唯一,但可以有空值。


8.外键:
如果一个表的某个字段指向另一个表的主键,就称之为外键;被指向的表,称之为主表,也叫父表;那么另一张表就是从表,也叫子表。
(1)创建外键
①create table tb2(xxxx,
tb1_id int(4) not null,
foreign key(tb1_id) references tb1(tb1_id));
②通过alter创建外键
alter table tb2 add foreign key fk_id(tb1_id)
references tb1(tb1_id);
(2)查看外键
show create table 表名;
(3)删除外键
alter table 表名 drop foreign key 外建名;
外建删除后不会对表中的数据有任何影响,只是改变了表的约束。
(4)通过外键给两表设置级联操作
alter table tb2 add foreign key fk_id(tb1_id) references tb1(tb1_id) on delete cascade on update cascade;
cascade 表示关联操作,即如果父表中数据被删除或更新,子表中对应数据也会执行同样的操作
on delete cascade
on update cascade
set null,表示子表数据不指向父表任何记录 
restrict,表示拒绝主表的相关操作
当不加这两句话时,默认就是 restrict,这也就是为什么开始我们主表中数据无法删除的原因
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值