用户管理:
1、新建用户:
>CREATE USER name IDENTIFIED BY 'ssapdrow';
2、更改密码:
>SET PASSWORD FOR name=PASSWORD('fdddfd');
3、权限管理
>SHOW GRANTS FOR name; //查看name用户权限
>GRANT SELECT ON db_name.* TO name; //给name用户db_name数据库的所有权限
>REVOKE SELECT ON db_name.* TO name; //GRANT的反操作,去除权限;
一、数据库操作:
1、查看数据库:
>SHOW DATABASES;
2、创建数据库:
>CREATE DATABASE db_name; //db_name为数据库名
3、使用数据库:
>USE db_name;
4、删除数据库:
>DROP DATABASE db_name;
二、创建表:
1、创建表:
>CREATE TABLE table_name(
>id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, //id值,无符号、非空、递增——唯一性,可做主键。
>name VARCHAR(60) NOT NULL
>score TINYINT UNSIGNED NOT NULL DEFAULT 0, //设置默认列值
>PRIMARY KEY(id)
>)ENGINE=InnoDB //设置表的存储引擎,一般常用InnoDB和MyISAM;InnoDB可靠,支持事务;MyISAM高效不支持全文检索
>DEFAULT charset=utf8; //设置默认的编码,防止数据库中文乱码
如果有条件的创建数据表还可以使用 >CREATE TABLE IF NOT EXISTS tb_name(........
2、复制表:
>CREATE TABLE tb_name2 SELECT * FROM tb_name;
或者部分复制:
>CREATE TABLE tb_name2 SELECT id,name FROM tb_name;
3、创建临时表:
>CREATE TEMPORARY TABLE tb_name(这里和创建普通表一样);
4、查看数据库中可用的表:
>SHOW TABLES;
5、查看表的结构:
>DESCRIBE tb_name;
也可以使用:
>SHOW COLUMNS in tb_name; //from也可以
6、删除表:
>DROP [ TEMPORARY ] TABLE [ IF EXISTS ] tb_name[ ,tb_name2.......];
实例:
>DROP TABLE IF EXISTS tb_name;
7、表重命名:
>RENAME TABLE name_old TO name_new;
还可以使用:
>ALTER TABLE name_old RENAME name_new;
三、修改表:
1、更改表结构:
>ALTER TABLE tb_name ADD[CHANGE,RENAME,DROP] ...要更改的内容...
实例:
>ALTER TABLE tb_name ADD COLUMN address varchar(80) NOT NULL;
>ALTER TABLE tb_name DROP address;
>ALTER TABLE tb_name CHANGE score score SMALLINT(4) NOT NULL;
四、插入数据:
1、插入数据:
>INSERT INTO tb_name(id,name,score)VALUES(NULL,'张三',140),(NULL,'张四',178),(NULL,'张五',134);
这里的插入多条数据直接在后边加上逗号,直接写入插入的数据即可;主键id是自增的列,可以不用写。
2、插入检索出来的数据:
>INSERT INTO tb_name(name,score) SELECT name,score FROM tb_name2;
五、更新数据:
1、指定更新数据:
>UPDATE tb_name SET score=189 WHERE id=2;
>UPDATE tablename SET columnName=NewValue [ WHERE condition ]
六、删除数据:
1、删除数据:
>DELETE FROM tb_name WHERE id=3;
七、条件控制:
1、WHERE 语句:
>SELECT * FROM tb_name WHERE id=3;
2、HAVING 语句:
>SELECT * FROM tb_name GROUP BY score HAVING count(*)>2
3、相关条件控制符:
=、>、<、<>、IN(1,2,3......)、BETWEEN a AND b、NOT
AND 、OR
Linke()用法中 % 为匹配任意、 _ 匹配一个字符(可以是汉字)
IS NULL 空值检测
八、MySQL的正则表达式:
1、Mysql支持REGEXP的正则表达式:
>SELECT * FROM tb_name WHERE name REGEXP '^[A-D]' //找出以A-D 为开头的name
2、特殊字符需要转义。
九、MySQL的一些函数:
1、字符串链接——CONCAT()
>SELECT CONCAT(name,'=>',score) FROM tb_name
2、数学函数:
AVG、SUM、MAX、MIN、COUNT;
3、文本处理函数:
TRIM、LOCATE、UPPER、LOWER、SUBSTRING
4、运算符:
+、-、*、\
5、时间函数:
DATE()、CURTIME()、DAY()、YEAR()、NOW().....
十、分组查询:
1、分组查询可以按照指定的列进行分组:
>SELECT COUNT(*) FROM tb_name GROUP BY score HAVING COUNT(*)>1;
2、条件使用Having;
3、ORDER BY 排序:
ORDER BY DESC|ASC =>按数据的降序和升序排列
十一、UNION规则——可以执行两个语句(可以去除重复行)
十二、全文检索——MATCH和AGAINST
1、SELECT MATCH(note_text)AGAINST('PICASO') FROM tb_name;
2、InnoDB引擎不支持全文检索,MyISAM可以;
十三、视图
1、创建视图
>CREATE VIEW name AS SELECT * FROM tb_name WHERE ~~ ORDER BY ~~;
2、视图的特殊作用:
a、简化表之间的联结(把联结写在select中);
b、重新格式化输出检索的数据(TRIM,CONCAT等函数);
c、过滤不想要的数据(select部分)
d、使用视图计算字段值,如汇总这样的值。
十四、使用存储过程:
个人理解,存储过程就是一个自定义函数,有局部变量参数,可传入参数,可以返回值,不过这语法够呆滞的~~~
1、创建存储过程:
>CREATE PROCEDURE pro(
>IN num INT,OUT total INT)
>BEGIN
>SELECT SUM(score) INTO total FROM tb_name WHERE id=num;
>END;
***这里的 IN (传递一个值给存储过程),OUT(从存储过程传出一个值),INOUT(对存储过程传入、传出),INTO(保存变量)
2、调用存储过程:
>CALL pro(13,@total) //这里的存储过程两个变量,一个是IN一个是OUT,这里的OUT也是需要写上的,不写会出错
>SELECT @total //这里就可以看到结果了;
3、存储过程的其他操作:
>SHOW PROCEDURE STATUS; //显示当期的存储过程
>DROP PROCEDURE pro; //删除指定存储过程
十五、使用游标:
对这个理解不是很懂,朋友多多指点哦~~~
1、游标的操作
>CREATE PROCEDURE pro()
>BEGIN
>DECLARE ordername CURSOR FOR
>SELECT order_num FROM orders;
>END;
>OPEN ordername; //打开游标
>CLOSE ordername; //关闭游标
十六、触发器:
触发器是指在进行某项指定操作时,触发触发器内指定的操作;
1、支持触发器的语句有DELETE、INSERT、UPDATE,其他均不支持
2、创建触发器:
>CREATE TRIGGER trig AFTER INSERT ON ORDERS FOR EACH ROW SELECT NEW.orser_name;
>INSERT语句,触发语句,返回一个值
3、删除触发器
>DROP TRIGGER trig;
十七、语法整理:
1、ALTER TABLE(修改表)
ALTER TABLE table_name
( ADD column datatype [ NULL | NOT NULL ] [ CONSTRAINTS ]
CHANGE column datatype COLUMNS [ NULL | NOT NULL ] [ CONSTRAINTS ]
DROP column,
。。。。
)
2、COMMIT(处理事务)
>COMMIT;
3、CREATE INDEX(在一个或多个列上创建索引)
CREATE INDEX index_name ON tb_name (column [ ASC | DESC ] , .......);
4、CREATE PROCEDURE (创建存储过程)
CREATE PROCEDURE pro([ parameters ])
BEGIN
........
END
5、CREATE TABLE(创建表)
CREATE TABLE tb_name(
column_name datetype [ NULL | NOT NULL ] [ condtraints] ,
column_name datetype [ NULL | NOT NULL ] [ condtraints] ,
.......
PRIMARY KEY( column_name )
)ENGINE=[ InnoDB | MyiSAM ]DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
6、CREATE USER(创建用户)
CREATE USER user_name [ @hostname ] [ IDENTIFIED BY [ PASSWORD ] 'pass_word' ];
7、CREATE VIEW (在一个或多个表上创建视图)
CREATE [ OR REPLACE ] VIEW view_name AS SELECT。。。。。。
8、DELETE (从表中删除一行或多行)
DELETE FROM table_name [WHERE ......]
9、DROP(永久删除数据库及对象,如视图、索引等)
DROP DATEBASE | INDEX | PROCEDURE | TABLE | TRIGGER | USER | VIEW name
10、INSERT (给表添加行)
INSERT INTO tb_name [ ( columns,...... ) ] VALUES(value1,............);
使用SELECT值插入:
INSERT INTO tb_name [ ( columns,...... ) ]
SELECT columns , ....... FROM tb_name [ WHERE ...... ] ;
11、ROLLBACK(撤销一个事务处理块)
ROLLBACK [ TO savapointname ];
12、SAVEPOINT(为ROLLBACK设置保留点)
SAVEPOINT sp1;
13、SELECT (检索数据,显示信息)
SELECT column_name,.....FROM tb_name [ WHERE ] [ UNION ] [ RROUP BY ] [ HAVING ] [ ORDER BY ]
14、START TRANSACTION (一个新的事务处理块的开始)
START TRANSACTION
15、UPDATE(更新一个表中的一行或多行)
UPDATE tb_name SET column=value,......[ where ]
事例
create table Student_cxy
(Sno char(13) primary key,
Sname char(20) unique,
Sex char(2),
Sdept char(20)
);
create table Course_cxy
(Cno char(4) primary key,
Cname char(10) not null,
Cpno char(4),
Ccredit smallint
);
create table SC_cxy
(Sno char(13),
Cno char(4),
Grade smallint,
primary key (Sno,Cno)
);
alter table Student_cxy add Sbirthday date;
alter table Course_cxy modify Cname char(20);
//在学生表中添加学生信息
insert
into student_cxy
values
('121002','李佳慧','女','计算机系','2001/7/2');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('121001','刘鹏翔','男','计算机系','2000/2/15');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('121004','周仁超','男','计算机系','1999/3/2');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('124001','林琴','女','通信学院','2004/2/23');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('124002','杨春容','女','通信学院','1999/10/5');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('124003','徐良成','男','通信学院','2000/3/18');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('124004','刘良成','男','通信学院','2000/12/12');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('121005','王小红','女','计算机系','2001/12/1');
insert
into Student_cxy(Sno,Sname,Sex,Sdept,Sbirthday)
values
('121006','刘晨','男','计算机系','2003/2/14');
//在课程表中添加课程信息
INSERT INTO Course_cxy
VALUES
('1004','数据库系统','1204',4);
INSERT INTO Course_cxy(Cno,Cname,Cpno,Ccredit)
VALUES
('1012','计算机网络','4002',3);
INSERT INTO Course_cxy(Cno,Cname,Cpno,Ccredit)
VALUES
('4002','数字电路','8001',3);
INSERT INTO Course_cxy(Cno,Cname,Cpno,Ccredit)
VALUES
('8001','高等数学',NULL,6);
INSERT INTO Course_cxy(Cno,Cname,Cpno,Ccredit)
VALUES
('1201','英语',NULL,5);
INSERT INTO Course_cxy(Cno,Cname,Cpno,Ccredit)
VALUES
('1204','程序设计基础','8001',3);
//在选课表中添加选课信息
INSERT
INTO SC_cxy
VALUES
('121001','1004',92),
('121002','1004',85),
('121004','1004',56),
('124001','4002',34),
('124002','4002',74),
('124003','4002',87),
('121001','8001',94),
('121002','8001',32),
('121004','8001',81),
('124001','8001',58),
('124002','8001',73),
('124003','8001',21),
('121001','1201',93),
('121002','1201',67),
('121004','1201',63),
('124001','1201',92),
('124002','1201',null),
('124003','1201',86),
('121002','1204',50),
('121001','1204',null),
('121004','1204',90),
('124001','1204',89),
('121006','1204',78),
('121005','1012',68);
//将姓“刘”的同学出生日期统一修改为‘2000-01-01’
UPDATE Student_cxy
SET Sbirthday='2000/1/1'
WHERE Sname LIKE'刘%';
//将“计算机系”同学的选课信息中的成绩置0
UPDATE SC_cxy
SET Grade = 0
WHERE '计算机系'=
(SELECT Sdept
FROM student_cxy
WHERE student_cxy.Sno = sc_cxy.Sno
);
//删除“计算机系”的姓“王”的同学
DELETE
FROM student_cxy
WHERE Sname LIKE'王%'AND Sdept = '计算机系';
//单表查询
//查询通信学院的所有女学生
SELECT*
FROM student_cxy
WHERE Sdept = '通信学院' AND Sex = '女';
//查询所有姓“刘”的学生
SELECT*
FROM student_cxy
WHERE Sname LIKE'刘%';
//查询各个学院各有多少学生
SELECT Sdept,COUNT(Sno)
FROM student_cxy
GROUP BY Sdept;
//查询平均成绩在80分以上的学生学号和平均成绩。
SELECT Sno,AVG(Grade)
FROM sc_cxy
GROUP BY Sno
HAVING AVG(Grade)>80;
//选修课程号为1004, 8001的学号。(注意去除重复值)
SELECT DISTINCT Sno
FROM sc_cxy
WHERE Cno = '1004'OR Cno = '8001';
//查询选修了1门且平均分在80分以下课程的学生的学号。
SELECT Sno
FROM sc_cxy
GROUP BY Sno
HAVING COUNT(cno)=1 AND AVG(Grade)<80;
//查询有1门以上课程不及格科目的学生学号(较难,选作
SELECT sno
FROM sc_cxy
WHERE Grade < 60
GROUP BY Sno
HAVING COUNT(cno)>1;
//连接查询
//查询每个选课学生的学生姓名、课程号和分数。(2表连接)
SELECT student_cxy.Sname,sc_cxy.Cno,sc_cxy.Grade
FROM student_cxy,sc_cxy
WHERE student_cxy.Sno=sc_cxy.Sno;
//查询学生的学号、姓名、选修课程的课程名及成绩。(3表连接)
SELECT student_cxy.Sno,student_cxy.Sname,course_cxy.Cname,sc_cxy.Grade
FROM student_cxy,sc_cxy,course_cxy
WHERE student_cxy.Sno=sc_cxy.Sno AND sc_cxy.Cno=course_cxy.Cno;
//查询选修课程为“数字电路”的学生姓名和学号。
SELECT student_cxy.Sname,student_cxy.Sno
FROM student_cxy,sc_cxy,course_cxy
WHERE student_cxy.Sno=sc_cxy.Sno AND sc_cxy.Cno=course_cxy.Cno AND cname = "数字电路";
//对学生表和选课表做自然连接,并输出结果。
SELECT student_cxy.Sno,sname,sex,sdept,sbirthday,cno,grade
FROM student_cxy,sc_cxy
WHERE student_cxy.Sno=sc_cxy.Sno;
//查询每一门课程的间接先行课的课程号。
SELECT FIRST.cno,SECOND.cpno
FROM course_cxy FIRST,course_cxy SECOND
WHERE FIRST.cpno = SECOND.cno;
//以学生为主体显示学生的信息及其选课的信息。(左外连接,学生表为左边,选作)
SELECT student_cxy.Sno,sname,sex,sdept,sbirthday,cno,grade
FROM student_cxy LEFT JOIN sc_cxy ON(student_cxy.Sno=sc_cxy.Sno);
//嵌套子查询
//查询与刘晨在同一个系学习的学生。
SELECT sname
FROM student_cxy
WHERE sdept IN
(SELECT sdept
FROM student_cxy
WHERE sname = "刘晨");
//查询平均分(指某个学生选修的所有课程的平均分)高于“杨春容”的学生学号及成绩。
SELECT sno,AVG(grade)
FROM sc_cxy
GROUP BY sno
HAVING AVG(grade) >
(SELECT AVG(grade)
FROM sc_cxy
GROUP BY sno
HAVING sno =
(SELECT sno
FROM student_cxy
WHERE sname = "杨春容"
));
//查询其他系中存在比计算机系某一学生年龄小的学生(即年龄小于计算机系年龄最大者的学生)。
SELECT sno,sname,sdept
FROM student_cxy
WHERE sbirthday>ANY
(SELECT sbirthday
FROM student_cxy
WHERE sdept = "计算机系"
) AND sdept <> "计算机系";
//查询其他院系中比计算机系学生年龄都小的学生姓名及年龄。
SELECT sno,sname,sdept
FROM student_cxy
WHERE sbirthday>ALL
(SELECT sbirthday
FROM student_cxy
WHERE sdept = "计算机系"
) AND sdept <> "计算机系";
//查询没有选修“英语”课程的学生姓名。
SELECT sname
FROM student_cxy
WHERE NOT EXISTS
(SELECT *
FROM sc_cxy
WHERE sno = student_cxy.Sno AND cno IN
(SELECT cno
FROM course_cxy
WHERE cname = "英语"
)
);
//查询没有不及格科目的学生(SC表中Grade为NULL记为不及格)
SELECT sname
FROM student_cxy
WHERE NOT EXISTS
(SELECT *
FROM sc_cxy
WHERE sno = student_cxy.Sno AND (grade <60 OR grade is NULL)
);
//查询选修课程超过2门的学生的学号和姓名。
SELECT sno,sname
FROM student_cxy
WHERE sno IN
(SELECT sno
FROM sc_cxy
GROUP BY sno
HAVING COUNT(cno)>2
);
//查询至少选修了学号为“121004”的学生所选修全部课程的学生学号和姓名。(难,选作)
SELECT sno,sname
FROM student_cxy
WHERE sno IN
(SELECT DISTINCT sno
FROM sc_cxy scx
WHERE NOT EXISTS
(SELECT *
FROM sc_cxy scy
WHERE scy.sno ="121004" AND NOT EXISTS
(SELECT *
FROM sc_cxy scz
WHERE scz.sno = scx.sno AND scz.cno = scy.cno
)
)
);
//查询与“英语”成绩最高分获得者相同院系的所有学生学号和姓名(难,选作)
SELECT sno,sname
FROM student_cxy
WHERE sdept IN
(SELECT sdept
FROM student_cxy
WHERE sno IN
(SELECT sno
FROM sc_cxy
WHERE grade IN
(SELECT MAX(grade)
FROM sc_cxy
WHERE cno IN
(SELECT cno
FROM course_cxy
WHERE cname = "英语"
)
)
)
);
//基于实验三所创建的表,进行视图操作:
//建立男学生的视图View_MaleStudent,属性包括学号、姓名、选修课程和成绩。
CREATE VIEW View_MaleStudentd
AS
SELECT student_cxy.sno,sname,cname,grade
FROM student_cxy,sc_cxy,course_cxy
WHERE student_cxy.Sno = sc_cxy.Sno AND sc_cxy.Cno = course_cxy.Cno AND sex = "男";
//查询
SELECT *
FROM View_MaleStudentd;
//在男学生视图中查询平均成绩大于80分的学生学号和姓名。
SELECT sno , sname
FROM View_MaleStudentd
GROUP BY sno
HAVING avg(grade)>80;
//删除生成的视图View_MaleStudent。
drop view View_MaleStudentd;
//查看学生表
select*
from student_cxy;
//查看课程表
select*
from Course_cxy;
//查看选课表
select*
from SC_cxy;
//删除表
drop table Student_cxy;
drop table Course_cxy;
drop table SC_cxy;
create procedure 评分(in Sno int,in Cno int)
begin
declare 评语 char(20);
declare Grade int ;
set Grade=(
select sc_cxy.Grade
from sc_cxy
where sc_cxy.Sno=Sno && sc_cxy.Cno=Cno);
if Grade=100 then
set 评语='太牛了!';
elseif 90<=Grade<100 then set 评语='优秀';
elseif 80<=Grade<90 then set 评语='良好';
elseif 70<=Grade<80 then set 评语='中等';
elseif 60<=Grade<70 then set 评语='及格';
elseif 40<=Grade<60 then set 评语='不及格';
elseif Grade<40 then set 评语='太渣了!';
elseif Grade=null then set 评语='缺考';
end if ;
select 评语;
end;
call 评分(121004,1004);
create procedure 阶乘(in n int)
begin
declare sum int default 0;
declare temp int default 0;
declare s int default 1;
while n>0 do
set temp=n;
while temp>1 do
set s=s*temp;
set temp=temp-1;
end while;
set n=n-1;
set sum=sum+s;
set s=1;
end while ;
select sum;
end;
call 阶乘(5);
create procedure 课程(in Sname char(5))
begin
select sc_cxy.Cno,course_cxy.Cname ,sc_cxy.Grade
from sc_cxy,course_cxy
where sc_cxy.Cno in
(select sc_cxy.Cno
from sc_cxy
where sc_cxy.Sno=(
select student_cxy.Sno
from student_cxy
where student_cxy.Sname=Sname))
&&sc_cxy.Sno=(select student_cxy.Sno
from student_cxy
where student_cxy.Sname=Sname)&&sc_cxy.Cno=course_cxy.Cno;
end;
call 课程('王小红');
create procedure 选作(in Sname char(5),out Grade int,out Count int)
begin
set Grade=
(select avg(Grade) from sc_cxy where sc_cxy.Sno=
(select student_cxy.Sno from student_cxy where student_cxy.Sname=Sname) group by sc_cxy.Sno);
set Count=(select count(Grade)from sc_cxy where sc_cxy.Sno=
(select student_cxy.Sno from student_cxy where student_cxy.Sname=Sname) group by sc_cxy.Sno);
select Grade,Count;
end;
call 选作('李佳慧',@Grade,@Count);
select @grade,@count;
# 1.创建一个用户user1。
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'cxy20020225'
# 2.把查询Student表权限授给用户user1,并验证授权后效果。
GRANT SELECT ON teacer.student_cxy TO 'user1'@'localhost';
# 3.把对表SC的查询权限授予所有用户。
GRANT SELECT ON teacer.sc_cxy TO public
# 5.把对表Student的Insert权限授予user1,并允许将权限转授给其他用户。
GRANT INSERT ON student_cxy TO 'user1'@'localhost' WITH GRANT OPTION
SHOW GRANTS
# 1.收回所有用户对表SC的查询权限。
REVOKE SELECT ON TABLE sc_cxy FROM *@localhost
# 2.收回用户user1对Student表的Insert权限。
REVOKE INSERT ON TABLE student_cxy FROM user1;
# 3.在回收权限之后验证用户是否真正丧失了该权限(查询表,插入记录)。
SELECT * FROM sc_cxy;
INSERT INTO student_cxy
VALUES(111,'hh','女','计算机系','2001-07-02')
# 1. 创建一个角色。
CREATE ROLE Role1
# 2. 给角色授予权限。
GRANT SELECT, UPDATE, INSERT
ON TABLE student_cxy
TO Role1
# 3. 将角色授予某一用户。
GRANT Role1 TO 'user1'@'localhost';
# 4. 检查用户是否具有相应的权限。
SHOW GRANTS