mysql入门,详细操作步骤,与知识汇总

筑·梦 2018-09-17 08:16:07
数据更新
插入单条数据:insert into tb_dept values('D4','公关部','Liming','010-82953306');
插入多条数据:insert into tb_dept values('D4','公关部','Liming','010-82953306'),('D5','财务部','Lina','010-82953306'),('D5','财务部','Lina','010-82953306');
插入查询结果记录:insert into 表名 (字段名) select 字段 from 表名 where 条件;
update tb_dept set deptno='D6',dname='会计部' where manager='liming';
删除表:drop table 表名
删除数据:delete from 表名 where 条件;
删除数据:Truncate 表名
数据约束
实体完整性约束、参照完整性约束、用户自定义约束
实体完整性:主键约束(primary key)、候选键约束(unique)
参照完整性约束:外键约束(foreign key)
用户自定义约束:not null、check、default、auto_increment
表级约束、列级约束

创建student表,字段id int型、name 定长字符串10 ,infor 变长字符串50,id设为主键,name 设为唯一键;
创建course表,字段id int 型,cname 变长字符串50
create table student (id int primary key,name char(10) unique,infor varchar(50));
create table student(id int,name char(10),infor varchar(50),constraint pr_k primary key(id),constraint un_p unique(name));
create table course (id int ,cname varchar(50),constraint f_k foreign key(id) references student(id));
check 约束
创建score 表中(id int ,score double),要求成绩取值只能在0-100之间;
create table score(id int,score double check(score>=0 and score <=100));
更改默认值:ALTER TABLE 表名alter 字段名set default 新数值;
更改数据类型:Alter table表名 modify 字段名 数据类型
更改字段名:Alter table 表名 change 旧字段名 新字段名 数据类型;

更新约束
添加主键约束:
alter table 表名 add 【constraint 约束名】 primary key(字段);
添加唯一键约束
alter table 表名 add 【constraint 约束名】unique(字段);
添加外键约束
alter table 表名 add 【constraint 约束名】foreign key(字段) references 被参照表(字段);
同时添加多个约束
alter table 表名 add 【constraint 约束名】 primary key(字段),add 【constraint 约束名】unique(字段);
删除约束
删除主键约束:alter table 表名 drop primary key;
删除唯一键约束:alter table 表名 drop index 唯一键字段名|约束名;
删除外键约束:alter table 表名 drop forign key 约束名;

注意:删除外键约束的时候,只能用约束名去删除,如果建立外键的时候没有给定约束名,那就使用show create table 语句查看系统默认分配的外键名称,然后使用该名称删除;

===========================================
删除字段:alter table 表名 drop 字段名
===============================================
查询
单表查询: select * from 表名
带条件的单表查询:select * from 表名 where 条件



select * from 表 where name in(“会计”“测试”)
多表查询
内连接、左连接、右连接、等值连接、交叉连接、自连接、自然连接
inner join
left join
right join
cross join
select * from 表1 join 表2 on 表1.值 =表2.值 where 条件
select * from 表1 cross join 表2

========================
视图
1、视图是一张虚拟表
2、视图是从一个或多个表或者视图中导出的表


create view 视图名 as select *from 表 where条件

删除视图
drop view 视图名
修改视图
alter view 视图名 as select * from 表
alter view v_emp as select age from tb_employee ,tb_dept where tb_employee.deptno=tb_dept.deptno and dname=‘销售部’

通过视图更新基表数据
1、insert into
2、update
注意:视图来自于多张表的时候,不允许有添加、删除操作
视图不能建立索引、触发器、默认值


select * from 表1 ,(select * from 表2)
===============================
创建用户
create user 用户名 @ localhost identified by '123';
create user 用户名 @ % identified by password'加密密码';
create user 用户名 @ 192.168.1.1 identified by password'加密密码';
删除用户
drop user 用户名 @ localhost;
修改用户账号
rename user 旧用户名@localhost to 新用户名@localhost;
修改用户密码
set password for 用户名 @ localhost=‘加密密码’;
set password for 用户名 @ localhost=password(‘密码’);
;
权限的授予

grant 权限 on 数据库.表名 to 用户名

all :表示所有权限
*:所有库
*.*:所有库下所有表

==============================
权限的撤销
revoke 权限 on 数据库.表 from 用户名@localhost

===========================
存储函数
create function 函数名 (参数1,参数2)
returns 返回值类型
函数体
return 值
call 函数名
存储过程
delimiter //
create procedure 过程名(in a int,out b char(20))
begin
过程体
end //
select 过程名
===================
流程控制语句

条件判断语句
if 条件 then
执行语句
end if

if 条件 then
执行语句1
else
执行语句2
end if

case 表达式
when 值 then 执行语句
when 值2 then 执行语句
end case


declare a int;
set a=1;
case a=a+1
when 1 then select * from 表1
when 2 then select * from 表2
when 3 then select * from 表3


循环
while 条件 do 执行语句
end while

repeat 语句
until 条件
end repeat

loop
语句
end loop

leave iterate

================================
游标
1、声明游标
declare 游标名 cursor for select 语句
2、打开游标
open 游标名
3、使用游标
fetch 游标名 into 变量名
4、关闭游标
close 游标名

===============================================
触发器
create trigger 触发器名 触发时间 触发事件
on 表
for each row
触发体

在数据库db_school的表db_student(id int,name varchar(20),time date)中创建一个触发器tb_student_insert,用于每次向表db_student中插入一行数据时将time时间设置为插入数据的时间
create trigger tb_student_insert after insert
on db_student
for each row

update tb_student set time=now();



insert into db_student(id) values(1);
select * from db_student;



==============================
索引
创建索引
create index 索引名 on 表名(字段 asc|desc)

create unique index 索引名 on 表名(字段 asc|desc)
更改索引
alter table 表名 add index 索引名(字段)
删除索引
drop 索引名
============================
事件
create event 事件名 on schedule at ‘2018-08-17 9:35’+ interval 1 day
do
insert into 表1 values(值);

create event 事件名 on schedule every 1 hour
starts now()
ends now()+interval 1hour
do
insert into 表1 values(值);

=====================
tb_student(id int,name varchar(20),sex char(2))
创建一个事件,用于每2天向表tb_student中插入一条数据,该事件开始于下个月,于2019年12月31日结束
create event e_insert on scheduler every 2day
starts now()+1 month
ends '2019-12-31';
do
insert into tb_studnet values(1,'zhang','nan');
order表(orderid int,num int,name varchar(20))
创建一个事件,用于每2天调用一次存储函数,该函数实现order表的查询,根据订单号查询出该订单的数量,该事件开始于本月,于下个月结束
create function f_s (oid int) procudure
returns int
begin
declare a int;
select num into a from order where orderid=oid;
return a;
end //

create event e_select on schedule every 2 day
starts curdate()
ends curdate()+1 month
do
select f_s('12');






在给定的学生选课数据库xsxk中,有“学生”、“课程”、“选课”三张表。
其中:
学生(学号 字符型,姓名姓名 字符型,出生日期 日期型,学院名称 字符型),“学号”为主键;
课程(课程名称 字符型,课程学分 整型),“课程名称”为主键;
选课(课程名称 字符型,学号 字符型,成绩 浮点型),其中(“课程名称”、“学号”)为复合主键,“学号”、“课程名称”分别为指向“学生”、“课程”表中同名属性的外键。
1. 设计一个名称为tr_选课的触发器,完成的功能是:当在选课表上插入一条记录之前,若该记录中的学号和课程名称在学生表和课程表中不存在,则在相关表中插入相应记录。
DELIMITER $$
CREATE TRIGGER tr_选课 (1) INSERT ON 选课
FOR EACH ROW
BEGIN
DECLARE sno,cno INT;
SELECT COUNT(*) INTO sno FROM 学生 WHERE 学号=NEW.学号;
SELECT COUNT(*) INTO cno FROM 课程 WHERE 课程名称=(2);
IF(sno=0) THEN
INSERT INTO 学生(学号) values((3));
END IF;
IF(cno=0) THEN
INSERT INTO 课程(课程名称) values(NEW.课程名称);
END IF;
END $$
DELIMITER ;
2. 设计一个存储函数fn_平均成绩,根据学生姓名返回学生的平均成绩。
create function fn_平均值(name varchar(20))
returns float
begin
declare a float;
select avg(成绩) into a from 选课,学生
where 选课.学号=学生.学号 and 姓名=name;
renturn a;
end //






答案:(1)before (2)NEW.课程名称 (3)NEW.学号
======================================
new old
在insert触发器中,new表示将要(before)或已经(after)插入的新数据;
在update触发器中,old表示将要或已经被修改的原数据,new表示将要或已经修改的新数据
在delete触发器中,old表示将要或已经删除的原数据
注意:old是只读的,而new可以在触发器中使用set进行赋值
create trigger tb_student1 after insert
on tb_student for each row set @str=new.studentno;
insert into tb_student(studentno) values(1);

create trigger t_de before update on tb_student
for each row set new.studentname=old.sex
...全文
905 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingmax54212008 2018-10-16
  • 打赏
  • 举报
回复
整理的挺好,建议放在博客中去
求知蚂蚁 2018-10-14
  • 打赏
  • 举报
回复
不错不错,,可以学习学习,谢谢楼主
Camonm 2018-10-12
  • 打赏
  • 举报
回复
感谢分享,正需要
grace8 2018-10-08
  • 打赏
  • 举报
回复
感谢分享,感觉挺有价值,但格式需要稍微注意点,比如区分下主次,加上序号也许更好 更有层次些,也方便其他人学习。
参考资料下载 深入理解MySQL
  • 打赏
  • 举报
回复
整理得很详细
Rotel-刘志东 2018-09-20
  • 打赏
  • 举报
回复
基础是重要的。
二月十六 2018-09-18
  • 打赏
  • 举报
回复
感谢分享~~
筑·梦 2018-09-18
  • 打赏
  • 举报
回复
我是新手,今年才大二,学的是it专业,谢谢指点
吉普赛的歌 2018-09-17
  • 打赏
  • 举报
回复
不错, 谢谢分享
卖水果的net 2018-09-17
  • 打赏
  • 举报
回复
整理的挺好,建议放在博客中去,这里没准哪天就没了。 PS: 修改密码,建议用 alter user 用户@host identified by 密码; 修改用户密码 set password for 用户名 @ localhost=‘加密密码’; set password for 用户名 @ localhost=password(‘密码’);

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧