MySQL基本知识点汇总 以及 高级查询练习



首先所有的子查询观看都必须从最里层开始,里层语句生成的虚拟表被外层应用,最后才能出现想要的结果。

一:库

DBMS 数据库管理系统 对象关系型

①创建数据库 create database 库名

②使用数据库 user 库名

③删除数据库 drop database 库名

范式:
第一范式:确保列的原子性,每个列都是不可分割的单元

    第二范式:每行数据都应根据一个依赖列被唯一区分

    第三范式:行中的每列都应该直接关联主键,不能出现其他表主键以外的字段

主键:
每张表中只能有一个主键,用于区分不同的数据行

主键要求该数据具有唯一性和稳定性

创建 添加 练习题

-- 新建数据库
create database geekhome;
-- 删除数据库
drop database geekhome;

-- 使用数据库
use geekhome;

create table users
(
	-- 添加主键方式一
	userId int primary key auto_increment,
	-- userId int,
	userName varchar(50) not null, -- 非空
	email varchar(50) default 'xxx@geekhome.com',
	birthday date
	-- ,
	-- 添加主键方式二
	-- constraint pk_userid primary key(userId)
)

-- 添加主键方式三  这种方式可以将主键约束与建表语句进行分离
alter table users add constraint pk_userId primary key(userId);

-- 删除主键
alter table users drop primary key;

-- 添加唯一约束,一张表中可以对多个列都进行唯一约束处理
alter table users add constraint uq_userName unique(userName);

-- 删除唯一约束
alter table users drop index uq_userName;

-- 添加外键约束
alter table orders add constraint fk_userId foreign key(userId) references users(userId);
-- 删除外键约束
alter table orders drop foreign key fk_userId;

二:表

创建:

create table 表名(字段名 数据类型 (长度)…)

添加主键: alter table 表名 add constraint 约束名 primary key (字段名)

删除主键: alter table 表名 drop primary key外键约束:alter table 外表名 add constraint 约束名 foreign key (外表中的字段名) references 主表名 (主表字段)

唯一约束:alter table 表名 add constraint 约束名 unique(字段名)

删除唯一约束: alter table 表名 drop index 约束名

非空约束: 字段名 类型 not null

默认约束: 字段名 类型 default ‘默认值’

自动增长:字段名 类型 auto_increment 从1开始,每次自增1

新增:

修改数据表,添加列:alter table 表名 add 列名 类型

新增行:

    方式一:insert into 表名(列名) values(值)

    方式二:insert into 表名 values(所有值)

注意点:

①日期格式:‘2021-8-21’

②新增不能违背约束条件

③新增列和值必须要完全匹配

④使用方式二时:当有不写的值时,写null。 想用默认值时,写default

新增表特殊方式:
创建新表,将原来的表中数据复制到新表

create table 新表名 as select * from 原表名

获取系统时间:
①select current_date 年月日

②select current_time 时分秒

③select current_timestamp 年月日 时分秒

④select now()年月日 时分秒

⑤select sysdate()

修改:

update 表名 set 列名=值

同时修改多个列:update 表名 set 列名=值,列名=值 [where 条件]

符号:

and 并且

or 或者

between 值 and 值 (在两个值中间,包括值)

in() 表示列值匹配括号中各个值的情况

like 用于模糊查询 用结合通配符使用

通配符有 :

    % 表示任意长度的任意字符

    _ 表示一位长度的任意字符

is null 判断是否为空

is not null

删除:

delete from 表名

delete from 表名 where …

truncate table 表名 (直接把表删除 不会写入日志)

删除外表数据 不会收到外键的影响

删除主表数据,如果该行数据正在被外表引用,则必须先删除外表的引用行,然后再删除主表对应的数据

练习题

-- 新建用户表
create table users
(
	userId int primary key auto_increment,
	userName varchar(20) not null,
	sex char(1),
	age int,
	email varchar(50) default 'xxx@geekhome.com',
	birthday date
)

-- 修改数据表添加列
alter table users add remoke varchar(200);

-- 新增行
-- 新增的数据不能违反约束条件
-- 新增列和值必须要完全匹配

insert into users(userName,sex,age,email,birthday) values('tom','男',20,'tom@qq.com','1992-5-3');
insert into users(userName,sex,age,email) values('lucy','女',22,'lucy@geekhome.com');
-- 整表插入,值必须与整表字段匹配
-- 如果需要呈现默认值使用default补位
-- 如果某个列需要为空值则使用null补位
insert into users values(null,'jack','男',23,'jack@126.com',null);
insert into users values(null,'white','男',24,null,'1990-2-3');
insert into users values(null,'rose','女',25,default,null);
insert into users values(null,'tim','男',27,null,null);

-- 创建新表,将原表中的数据复制至新表
create table new_users as select * from users;

-- 获取系统时间
select CURRENT_DATE
select CURRENT_TIME
select CURRENT_TIMESTAMP
select sysdate();

-- 修改
-- 没有添加where添加则表示整表修改
update new_users set email='geek@geekhome.com';
-- 同时修改多个列的数据
update new_users set email='geekhome@qq.com',birthday=sysdate();
-- 根据指定条件修改数据
update new_users set age=18 where userId=1;

-- 修改用户年龄在18-23岁间所有用户的邮箱
update new_users set email='user@qq.com' where age>=18 and age<=23;
update new_users set email='geek@qq.com' where age between 18 and 23;
update new_users set email='home@qq.com' where age in(18,19,20,21,22,23);
-- in表示列值匹配括号中各个值的情况,等价于如下代码 age=18 or age=19 or age=20.....


-- 修改用户姓名以t开头的邮箱
-- like用于模糊匹配,使用like必须要结合通配符进行使用
-- %表示任意长度的任意字符  _表示一位长度的任意字符
update users set email='t@qq.com' where userName like 't%';
-- 修改姓名中包含t的用户邮箱
update users set email='u@qq.com' where userName like '%t%' or userName like '%T%';
-- 将没有填写过生日的用户,将生日改为系统时间
update users set birthday=sysdate() where birthday is null;
-- 将所有填写过生日的用户将其生日清空
update users set birthday=null where birthday is not null;


-- 创建课程表
create table course
(
	courseId int primary key auto_increment,
	courseName varchar(20) not null
);
-- 创建成绩表
create table score
(
	userId int,
	courseId int,
	score int
);

-- 新增数据
insert into course values(null,'Java');
insert into course values(null,'MySQL');
insert into course values(null,'Spring');

insert into score values(1,1,58);
insert into score values(1,1,55);
insert into score values(1,1,62);
insert into score values(1,2,78);
insert into score values(1,3,82);
insert into score values(2,1,91);
insert into score values(2,2,75);
insert into score values(2,3,83);
insert into score values(3,1,45)<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值