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();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章