mysql入門,詳細操作步驟,與知識彙總

數據更新
插入單條數據: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

PS: 修改密碼,建議用 alter user 用戶@host identified by 密碼;

修改用戶密碼
set password for 用戶名 @ localhost=‘加密密碼’;
set password for 用戶名 @ localhost=password(‘密碼’);

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