mysql复习笔记(1)

SQL语句快速复习笔记

create table student(
    _id INT NOT NULL,
    _name varchar(20),
    _teacher int,
    primary key(_id),
    index index_1(_name)
);
create table teacher(
    _id INT NOT NULL,
    _name varchar(20),
    primary key(_id)
);

-- 添加外键
alter table student add constraint FK_ID foreign key(_teacher) 
REFERENCES teacher(_id);

-- 插入语句
insert into student(_id,_name,_teacher)values(1,"学生1",1);
insert into student(_id,_name,_teacher)values(2,"学生2",1);


show tables;

drop table student;

describe tb1;

insert into teacher(_id,_name)
values(1,"老师_1");

-- 链接查询
select s._id,s._name,t._name from student s join teacher t on t._id=s._id;

select *from student s,teacher t where s._teacher = t._id;

-- 更新语句
update student set _name = "学生_1" where _id = 1;

select *from student;

delete from student where _id = 1;

-- like关键字(模糊查询)
select *from student where _name like '学%';

-- union操作符
select _id,_name from student
union
select _id,_name from teacher;

-- order by排序(asc desc)
select _id,_name from student
union
select _id,_name from teacher
order by _id desc;

-- group by 分组(COUNT, SUM, AVG),coalesce(a,b,c)
select coalesce(author,'总数') as author,count(title_2) as book_count from tb1 where author is not null 
group by author WITH ROLLUP;

-- having分组后的过滤条件
select coalesce(author,'总数'),count(title_2) as book_count from tb1 where author is not null 
group by author WITH ROLLUP
having count(book_count)>2;

-- REGEXP 正则表达式
select * from tb1 where author regexp '^小';


-- 事务提交
begin;
insert into student(_id,_name,_teacher)values(3,"学生3",1);
insert into student(_id,_name,_teacher)values(4,"学生4",1);
commit;

select *from student;

-- 回滚
begin;
insert into student(_id,_name,_teacher)values(5,"学生5",1);
select *from student;
rollback;


-- 增加表中一个字段
alter table tb1 add test int after id;
desc tb1;

-- 删除表中一个字段
alter table tb1 drop test;
desc tb1;

-- 修改字段类型或名称 MODIFY 或 CHANGE
alter table tb1 modify id int not null auto_increment;
desc tb1;

alter table tb1 change title title_2 varchar(100);
desc tb1;

-- 查看表信息
show table status;

-- 修改表名
alter table tb2 rename to tb1;
show tables;

-- 删除外键约束
alter table student drop foreign key FK_ID;

-- 添加普通索引
create index index_1 on tb1(id);
show index from tb1;

-- 删除索引
drop index index_1 on tb1;
show index from tb1;

-- 唯一索引
create unique index unique_index on tb1(title_2);
show index from tb1;

-- 全文索引
create fulltext index fullIndex on tb1(author);
drop index fullIndex on tb1;


-- 存储过程
drop procedure if exists InsertDataInTb1;
delimiter #
create procedure InsertDataInTb1()
begin
    declare i int default 0;
    declare title varchar(20) default 'title_' ;
    while i<100 do
        insert into tb1 
        (title_2,author,sdate)
        values
        (concat('title',i),concat('author_',i),curdate());
        set i = i+1;
    end while;
end #

DELIMITER ;

-- 调用存储过程
call InsertDataInTb1();


-- 创建临时表
create temporary table temporary_table(
    id int primary key not null,
    temp_name varchar(99)
);

-- 查看创建表的语句
show create table tb1;

-- 复制表结构
create table tb2 like tb1;

-- 复制数据
insert into tb2 select *from tb1;
select *from tb2;

-- 复制表数据,字段一样结构不同
create table tb3 select *from tb1;
desc tb3;
select *from tb2;


-- 查看最后自增ID
select LAST_INSERT_ID();

猜你喜欢

转载自blog.csdn.net/qq_32475739/article/details/80863455