AI训练营SQL-TASK01打卡

一:数据库的安装

Centos 7.6 下安装mysql8 (8.0.28)

http://t.csdn.cn/6hx3ghttp://t.csdn.cn/6hx3g

二:认识数据库的操作语言 (SQL)

1 SQL的概念

DDL

DDL(Data Definition Language,数据定义语言) 用来创建或者删除存储数据用的数据库以及数据库中的表等对象。DDL 包含以下几种指令。

CREATE : 创建数据库和表等对象

DROP : 删除数据库和表等对象

ALTER : 修改数据库和表等对象的结构

DML

DML(Data Manipulation Language,数据操纵语言) 用来查询或者变更表中的记录。DML 包含以下几种指令。

SELECT :查询表中的数据

INSERT :向表中插入新数据

UPDATE :更新表中的数据

DELETE :删除表中的数据

DCL

DCL(Data Control Language,数据控制语言) 用来确认或者取消对数据库中的数据进行的变更。除此之外,还可以对 RDBMS 的用户是否有权限操作数据库中的对象(数据库表等)进行设定。DCL 包含以下几种指令。

COMMIT : 确认对数据库中的数据进行的变更

ROLLBACK : 取消对数据库中的数据进行的变更

GRANT : 赋予用户操作权限

REVOKE : 取消用户的操作权限

 2 SQL的书写

操作数据库

-- 查看数据库
show databases;
-- 创建数据库
create database db1;
create database if not exists db1;
-- 删除数据库
drop database db1;
drop database if exists db1;
-- 

操作数据库表格

-- use db1; 使用数据库
-- 查看数据库中的表
show tables;
-- 创建表格
create table tb_1(
id int,
username varchar(255),
book text
);
-- 修改表名
alter table tb_1 rename to tb_2;
-- 展示表结构
desc tb_1;
-- 修改数据类型
alter table tb_1 modify describe varchar(255);
-- 修改字段
alter table tb_1 change username book text;
-- 添加字段
alter table tb_1 add weight int(11);
-- 添加字段到指定字段后面
alter table tb_1 add weight int(11) after age;
-- 添加到第一个
alter table tb_1 add weight int first;
-- 删除字段
alter table tb_1 drop weight ;
-- 删除表格
drop table tb_1;

对表格添加数据

-- 添加
insert into tb_1 values(1,'zly',"水浒");
-- 局部添加
insert into tb_1(id,username) values(1,'zly') ;
-- 添加多条数据
insert into tb_1 values(1,'zly',"水浒"),(2,'zzz',"西游记") ;
-- 修改数据
update tb_1 set books="";
-- 修改指定数据
update tb_1 books="" where username="zly";
-- 修改多个字段
update tb_1 books="",id=1314 where username="zly";
-- 删除数据
delete from tb_1 where username="zly";
-- 删除所有
delete from tb_1;
-- 删除所有
truncate tb_1;

查询数据

-- all 查询所有 可省略
select [all] * from tb_1;
-- distinct 去重查询
select distinct * from tb_1;
-- 字段查询
select (id,username) from tb_1;
-- 取别名
select (id [序号],username [姓名]) from tb_1 [tb];

--where 字句 查询条件
-- = > < != ...
select * from tb_1 where age >=18;
-- between and
select * from tb_1 where age between 17 and 19;
-- [not] in 在[不在]集合里
select * from tb_1 where age in(1,23,4,5,6,62,7,10);
-- 匹配查询
select * from tb_1 where username like "zly";
-- _ 一个字符
--  * 任意字符
select * from tb_1 where username like "%王—";
-- is [not] null;
-- 查询[不] 为空
select * from tb_1 where username is null;
-- 多重查询
select * from tb_1 where username is not null and age>18;

-- order by 字句 排序 [asc|desc] asc 升序(默认) desc降序
select * from tb_1 order by age desc;
-- limit [offset,]n offset偏移量   n个数 
-- 为方言 仅在 mysql中有
-- 从开始查询 一千个数据
select * from tb_1 limit 1000;
-- 从第十一个查询 一千个数据
select * from tb_1  limit 10,1000;

-- group by 分组查询
select * from tb_1 group by age;
-- 聚合函数
-- count
-- sum
-- max
-- avg

--having 子句
select max(age) from tb_1 
where username is not null 
group by sex having id>100 
order by desc limit 0,1000;
-- 字句顺序
 from | where | group by (having )|order by| limit;

约束

候选键 表内唯一确定的字段

主键

其他没有被选定的就叫候选键

外键

不是当前表的主键 别的表的主键

练习 关联查询

创建表

create tb_student(
id int primary key,
name varchar(255) ,
age int,
sex varchar(255)
)

主键值不能为空

添加主键约束

alter table tb_student add primary key(id)

删除主键约束

alter table tb_student drop primary key

外键约束 外键必须来自主标中的主键

添加外键约束时 主表要存在

foreign key ... reference

create table tb_student(
id int primary key,
name varchar(255),
age int,
cid int,
constraint fk_student_ciid foreign key(cid) reference tb_cLass(id)
)

用于关联查询

唯一约束

unique 不可以重复 可以null

not null 不为空

default 默认约束

default "男"

自增

auto_increment

 三:练习

3.1

编写一条 CREATE TABLE 语句,用来创建一个包含表 1-A 中所列各项的表 Addressbook (地址簿),并为 regist_no (注册编号)列设置主键约束

表1-A 表 Addressbook (地址簿)中的列

 create table Addressbook(
 regist_no int not null primary key,
 name varchar(128) not null,
 address varchar(256) not null,
 tel_no char(10),
 mail_address char(20)
 );

3.2

假设在创建练习1.1中的 Addressbook 表时忘记添加如下一列 postal_code (邮政编码)了,请把此列添加到 Addressbook 表中。

列名 : postal_code

数据类型 :定长字符串类型(长度为 8)

约束 :不能为 NULL

 alter table AddressBook add postal_code char(8) not null;

3.3

编写 SQL 语句来删除 Addressbook 表。

 drop table Addressbook

3.4

编写 SQL 语句来恢复删除掉的 Addressbook 表。

 create table if not exists Addressbook(
 regist_no int not null primary key,
 name varchar(128) not null,
 address varchar(256) not null,
 tel_no char(10),
 mail_address char(20)
 );

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZLY_2004

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值