sql基础用法

--创建表
CREATE TABLE tb_teacher(
    id INT,
    sname VARCHAR(20),
    gender VARCHAR(2),
    age INT
)

DROP TABLE tb_teacher;

DESC tb_teacher;

-- 给tb_teacher添加servlet和jsp成绩列
ALTER TABLE tb_teacher ADD servlet INT, ADD jsp INT;
DESC tb_teacher;
UPDATE tb_teacher SET servlet=70,jsp=85 WHERE sname="王老师";
UPDATE tb_teacher SET servlet=75,jsp=95 WHERE sname="张老师";


-- 增加数据
INSERT INTO tb_teacher VALUES(1,'张老师','男',29);

INSERT INTO tb_teacher(id,sname,gender) VALUES(1,'王老师','男');

-- 修改数据
UPDATE tb_teacher SET gender='女';

UPDATE tb_teacher SET gender='男' WHERE sname='张老师';

UPDATE tb_teacher SET gender='中',age=20 WHERE sname='王老师';

-- 删除数据
-- delete 1)可以带条件删除 2)只能删除表的数据,不能删除表的约束 3)删除的数据可以回滚
-- truncate 1)不能带条件删除 2)可以删除表的数据,也可删除表的约束 3)删除的数据不能回滚
DELETE FROM tb_teacher;

DELETE FROM tb_teacher WHERE age=20;

TRUNCATE TABLE tb_teacher;

-- 查询数据
-- 1.1查询所有列
SELECT * FROM tb_teacher;

-- 1.2查询指定列
SELECT id,sname,gender FROM tb_teacher;

-- 1.3查询指定别名
SELECT id AS '编号',sname AS '姓名' FROM tb_teacher AS A;

-- 1.4查询是添加常量列
SELECT id,sname,'初中老师' FROM tb_teacher;

-- 1.5查询时合并列(需求:查询每个学生的servlet和jsp的总成绩)
SELECT id,sname,(servlet+jsp) AS '总成绩' FROM tb_teacher;

-- 1.6时取出重复记录
SELECT DISTINCT gender FROM tb_teacher;
SELECT DISTINCT(gender) FROM tb_teacher;

-- 1.7条件查询
-- 1.7.1逻辑条件:and or
-- 1.7.2比较条件:<  >  >=  <=  =  <>  between and
-- 1.7.3判空:is null    is not null
-- 1.7.4模糊查询:like  (_表示任意一个字符,%表示任意字符)
SELECT * FROM tb_teacher WHERE sname LIKE '%老师';

-- 1.8聚合查询(sum() avg() min() max() count)
SELECT * FROM tb_teacher;
SELECT SUM(servlet),SUM(jsp) FROM tb_teacher;
SELECT AVG(servlet),AVG(jsp) FROM tb_teacher;
SELECT MAX(servlet) FROM tb_teacher;
SELECT MIN(servlet) FROM tb_teacher;
SELECT COUNT(*) FROM tb_teacher;

-- 1.9分页查询(limit 0(起始行),1(查询几行))
SELECT * FROM tb_teacher LIMIT 0,1;


-- 2.0查询排序
SELECT * FROM tb_teacher ORDER BY servlet ASC;
SELECT * FROM tb_teacher ORDER BY servlet DESC;
SELECT * FROM tb_teacher ORDER BY servlet ASC,jsp DESC;

-- 2.1分组查询
SELECT gender,COUNT(*) FROM tb_teacher GROUP BY gender;

-- 2.2分组查询后筛选
SELECT gender,COUNT(*) FROM tb_teacher WHERE gender='中' GROUP BY gender HAVING COUNT(*)>=1;

CREATE DATABASE day16;
SHOW DATABASES
SHOW CREATE DATABASE day16;
ALTER DATABASE day16 DEFAULT CHARACTER SET utf8;
-- *******************************--数据约--*******************************************

-- 1.1 默认值(允许插入null值,在不插入值得时候才会使用默认值)
CREATE TABLE student(
    id INT,
    NAME VARCHAR(20),
    address VARCHAR(20) DEFAULT '北京' 
);

INSERT INTO student(id,NAME) VALUES(1,"小明");
INSERT INTO student(id,NAME,address) VALUES(2,'小王',NULL);


-- 1.2 非空---(非空字段必须赋值,不能为NULL)
CREATE TABLE student(
    id INT,
    NAME VARCHAR(20),
    address VARCHAR(0) NOT NULL
)
INSERT INTO student(id,NAME) VALUES(1,'小李');
INSERT INTO student(id,NAME,address) VALUES(2,'小张',NULL);

-- 1.3 唯一 对NULL值没有约束
CREATE TABLE student(
    id INT UNIQUE,
    NAME VARCHAR(20),
    addresss VARCHAR(20)
)
INSERT INTO student(id,NAME) VALUES(1,'小李');
INSERT INTO student(NAME) VALUES('小李');
INSERT INTO student(id,NAME) VALUE(NULL,'小李');

-- 1.4 主键(非空+唯一)
CREATE TABLE student(
    id INT PRIMARY KEY,
    NAME VARCHAR(20),
    gender VARCHAR(2)
)
INSERT INTO student(id,NAME) VALUES(1,'小李');
INSERT INTO student(NAME) VALUES('小李');
INSERT INTO student(id,NAME) VALUE(NULL,'小李');

-- 1.5 自增长 --从0开始递增
CREATE TABLE student(
    id INT(4) ZEROFILL PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20),
    gender VARCHAR(2)
)
INSERT INTO student(id,NAME) VALUES(80000000,'小李');
INSERT INTO student(NAME) VALUES('小张');
TRUNCATE student;

-- 1.6 外键约束
    -- 作用:约束两张表的约束
    -- 注意1:先添加主表,后添加副表
    -- 注意2:先修改副表,后修改主表
    -- 注意3:先删除副表,后删除主表
    -- 注意4:可以使用级联修改ON CASCADE UPDATE(DELETE)(对主表的修改,可以把副表的也修改掉)
CREATE TABLE employee(
    id INT PRIMARY KEY AUTO_INCREMENT,
    empName VARCHAR(20),
    deptId INT,
    CONSTRAINT employee_dept_fk FOREIGN KEY(deptId) REFERENCES dept(id) ON CASCADE UPDATE
)

CREATE TABLE dept(
    id INT PRIMARY KEY,
    deptName VARCHAR(20)
)
INSERT INTO dept(id,deptName) VALUES(1,"服务器");
INSERT INTO dept(id,deptName) VALUES(2,"客户端");
INSERT INTO dept(id,deptName) VALUES(3,"美术");    

INSERT INTO employee(empName,deptId) VALUE('小张',1);
INSERT INTO employee(empName,deptId) VALUE('小明',2);
INSERT INTO employee(empName,deptId) VALUE('小王',4);

-- *********************************关联查询(多表查询**********************************

-- 2.1内连接查询
SELECT empName,deptName FROM employee,dept WHERE employee.deptId = dept.id;
SELECT empName,deptName FROM employee INNER JOIN dept ON employee.deptId=dept.id;

-- 2.2左(外)连接outer可以省略,左表的数据一定会完整显示
SELECT d.deptName,e.empName FROM dept d LEFT OUTER JOIN employee e ON d.id=e.deptId;

-- 2.3右(外)连接,右表的数据一定会完整显示
SELECT d.deptName,e.empName FROM employee e  RIGHT OUTER JOIN dept d ON d.id=e.deptId;

-- 2.4自连接查询
SELECT e.empName,d.empName AS bossName FROM employee AS e LEFT OUTER JOIN employee d ON e.id=d.bossId;


ALTER TABLE employee ADD bossId INT;
UPDATE employee SET bossId=2 WHERE id=1;
UPDATE employee SET bossId=1 WHERE id=2;
INSERT employee VALUES(3,'小王',2,NULL);
SELECT * FROM employee;
SELECT * FROM dept;


-- **********************************************存储过程***************************************
-- 声明结束符
DELIMITER $
CREATE PROCEDURE pro_test()
BEGIN
    -- 可以写过个sql语句
    SELECT * FROM employee;
END $

-- 调用存储过程
CALL pro_test()

-- 3.1 带存储输入参数的存储过程
DELIMITER $
CREATE PROCEDURE pro_findById(IN eid INT)
BEGIN 
    SELECT * FROM employee WHERE id=eid;
END $
CALL pro_findById(2);

-- 3.2 带输出参数的存储过程
DELIMITER $
CREATE PROCEDURE pro_testOut(OUT str VARCHAR(20))
BEGIN
    -- 给参数赋值
    SET str='这是一个输出参数';
END $

CALL pro_testOut();

-- 3.3 输入输出参数的存储过程
DELIMITER $
CREATE PROCEDURE pro_testInOut(INOUT n INT) -- inout:输入输出参数
BEGIN
    -- 查看变量
    SELECT n;
    SET n=500;
END $

DROP PROCEDURE pro_testInOut;

SET @n=10;
CALL pro_testInOut(@n);
SELECT @n;

-- 3.4带有条件判断的存储过程
DELIMITER $
CREATE PROCEDURE pro_testIf(IN num INT,OUT str VARCHAR(20))
BEGIN
    IF num=1 THEN
        SET str='星期一';
    ELSEIF num=2 THEN
        SET str='星期二';
    ELSEIF num=3 THEN
        SET str='星期三';
    ELSEIF num=4 THEN
        SET str='星期四';
    ELSEIF num=5 THEN
        SET str='星期五';
    ELSEIF num=6 THEN
        SET str='星期六';
    ELSEIF num=7 THEN
        SET str='星期日';
    ELSE
        SET str='输出错误';
    END IF;
        
END $

CALL pro_testIf(10,@str);
SELECT @str;

-- 3.5带有循环功能的存储过程
DELIMITER $
CREATE PROCEDURE pro_testWhile(IN num INT,OUT result INT)
BEGIN
    -- 定义个局部变量
    DECLARE i INT DEFAULT 1;
    DECLARE vsum INT DEFAULT 0;
    WHILE i<=num DO
        SET vsum = vsum+i;
        SET i=i+1;
    END WHILE;
    SET result=vsum;
END$

CALL pro_testWhile(100,@result);
SELECT @result;

DROP PROCEDURE pro_testWhile;

-- 3.6 使用查询的结果作为返回值
DELIMITER $
CREATE PROCEDURE pro_findById(IN eid INT,OUT vname VARCHAR(20))
BEGIN
    SELECT empName INTO vname FROM employee WHERE id=eid;
END $

DROP PROCEDURE pro_findById;
CALL pro_findById(1,@empName);
SELECT @empName;
-- ************************** mysql变量 *********************************
-- 查看所有全局内置变量
SHOW VARIABLES LIKE 'character_%';
SET character_set_server='utf8';

-- 设置回话变量.只要存储过程执行完毕,局部变量就会全部丢失
SET @NAME='haha';
SELECT @NAME;


-- ***********************************-四、触发器-***************************************************
CREATE TABLE test_log(
    id INT PRIMARY KEY AUTO_INCREMENT,
    content VARCHAR(100)
)

-- 4.1插入触发器
CREATE TRIGGER tri_empAdd AFTER INSERT ON employee FOR EACH ROW
    INSERT test_log(content) VALUES('员工表插入了一条记录');

INSERT INTO employee(id,empName,deptId,bossId) VALUES(4,'jack',2,1);

-- 4.2修改触发器
CREATE TRIGGER tri_empAdd AFTER UPDATE ON employee FOR EACH ROW
    INSERT test_log(content) VALUES('员工表插入了一条记录');

-- 4.3删除触发器
CREATE TRIGGER tri_empAdd AFTER DELETE ON employee FOR EACH ROW
    INSERT test_log(content) VALUES('员工表插入了一条记录');

SELECT * FROM employee;
SELECT * FROM test_log;

-- ***********************************-五、触发器-***************************************************
SELECT PASSWORD('root');

USE mysql;
SELECT * FROM USER;
UPDATE USER SET PASSWORD=PASSWORD('123') WHERE USER='root' AND HOST='localhost';

-- 分配账户权限
GRANT SELECT ON day16.employee TO 'rose'@'localhost' IDENTIFIED BY '123';
GRANT DELETE ON day16.employee TO 'rose'@'localhost' IDENTIFIED BY '123';
GRANT INSERT ON day16.employee TO 'rose'@'localhost' IDENTIFIED BY '123';

-- ***********************************-六、备份和还原-***********************************************

-- 不能再登录界面中备份
mysqldump -uroot -p day16 > e:/day16.sql

mysql -uroot -p day16 < e:/day16.sql;

 

你可能感兴趣的:(Mysql)