mysql學習

/usr/local/mysql/bin/mysql -h 127.0.0.1 -u root -p
linux下安裝:rpm、二進制包安裝、源碼包安裝、
show databases;
create database 庫名;
drop database 庫名;
--顯示數據存儲引擎
show engines\G
show variables like 'have%'\G
show variables like 'storage_engine'; --查詢默認存儲引擎
show create table jay1\G--查詢表的默認存儲引擎
--更改默認存儲引擎可更改my.cnf中的default-storage-engine=Innodb選項;
--Innodb提供良好的事務管理,崩潰修復能力和併發控制,並且支持外鍵。但其讀寫效率稍差,佔用數據空間相對較大。其表結構存儲在.frm中,數據存儲在innodb_data_home_dir的定義表空間中,索引存儲在innodb_data—file_path的定義表空間中。
--Myisam佔用空間小,處理速度快,但不支持事務的完整性和併發性,不支持外鍵。其表結構存儲在.frm中,數據存儲在.MYD中,索引存儲在.MYI中。
create database jay;
use jay;
create table jay0(id int,name varchar(10),sex boolean);
--單字段主鍵:
create table jay1(stu_id int primary key,stu_name varchar(20),stu_sex boolean);
--多字段主鍵:
create table jay2 (stu_id int , course_id int, grade float,primary key(stu_id,course_id));
--外鍵:
create table jay3 (id int primary key, stu_id int,course_id int,constraint c_fk foreign key(stu_id,course_id)references jay2(stu_id, course_id));
--非空約束:
create table jay4(id int not null primary key,name varchar(50) not null,stu_id int,constraint d_fk foreign key (stu_id)references jay1(stu_id));
--唯一性約束:
create table jay5(id int primary key, stu_id int unique,name varchar(40) not null);
--屬性值自動增加:
create table jay6(id int primary key auto_increment,stu_id int unique,name varchar(20) not null);
--設置表屬性默認值:
create table jay7(id int primary key auto_increment,stu_id int unique,name varchar(10) not null,English varchar(20) default 'zero',math float default 0,computer float default 0);
--查看基本結構:
describe jay1;
desc jay1;
--查看錶詳細結構:
show create table jay1\G
show table status like 'jay1'\G
--修改表名:
alter table jay0 rename user;
--修改字段數據類型:
alter table user modify name varchar(50);
alter table user modify id int(10) primary key;
--只修改字段名:
alter table jay1 change stu_name name varchar(10);
--修改字段名和字段數據類型:
alter table jay1 change stu_sex sex int(2);
--增加無完整性約束條件的字段:
alter table user add phone varchar(20);
--增加完整性約束的字段:
alter table user add age int(4) not null;
--表的第一個位置增加字段:
alter table user add num int(10) primary key first;
--表的指定位置之後增加字段:
alter table user add address varchar(50) not null after phone;
--刪除字段
alter table user drop primary key;--刪除主鍵
alter table user drop id;
--修改字段到指定位置:
alter table user modify sex tinyint(1) after age;--一定要加上字段屬性
--更改表的存儲引擎
alter table user engine=myisam;
show create table user\G
--刪除表的外鍵約束
alter table jay3 drop foreign key c_fk;--刪除後原來的外鍵會變爲普通鍵。
--刪除沒有被關聯的表
drop table jay1;--刪除時需確認沒有外鍵依賴,否則刪除失敗
alter table jay4 drop foreign key d_fk;
show tables;--查詢庫中的所有表
=================================================================================
--索引:一個表中可以存在多個索引。
--索引分爲:普通索引、唯一索引、全文索引、單列索引、多列索引和空間索引。
--primary 主鍵,就是唯一且不能爲空。
--index索引,普通的
--unique 唯一索引。 不允許有重複。
--fulltext是全文索引,用於在一篇文章中,檢索文本信息的。
unique --唯一索引
fulltext --全文索引
spatial --空間索引
index/key --指定字段爲索引
asc--升序排列
desc--降序排列
---注意:desc與decs的區別
--創建普通索引
create table orcs1(id int,name varchar(20),sex boolean,index(id));
--查看索引是否被使用
explain select * from orcs1 where id=1\G
--唯一索引
create table orcs2(id int unique,name varchar(30),unique index orcs2_id(id asc));--orcs_id爲別名
--創建全文索引
create table orcs3(id int,info varchar(30),fulltext index orcs3_info(info))engine=myisam;
--全文索引只能創建在char、varchar、text字段上,並且目前只有myisam存儲引擎支持。
--創建單列索引
create table orcs4(id int,subject varchar(30),index orcs4_st(subject(10)));--對於字符型的數據,爲了查詢速度不錯全部數據的查詢,只查詢其前面若干字符信息,
--創建多列索引
create table orcs5(id int.name varchar(30),sex char(4),index orcs5_ns(name,sex)) ;--查詢條件中需使用索引中第一個字段時,該索引纔可以生效。
--創建空間索引
--空間索引必須使用spatial參數設置,並且表的存儲引擎必須是myisam,而且索引字段必須有非空約束。
create table orcs6(in int,space geometry not null, spatial index orcs6_sp(space))engine=myisam;
--注:geometry爲空間數據類型,空間類型包括:geometry、point、linestring、polygon等。
--在已存在的表上創建索引:create、alter都可以創建。
--創建普通索引
create index index_num on user(num);--首先該user表必須存在。
alter table user add index index_num(num);
--創建唯一索引
create unique index index1_id on jay2(stu_id);
alter table jay2 add unique index index_id(stu_id);
--創建全文索引
alter table jay4 engine=myisam;--修改該表存儲引擎爲myisam。
create fulltext index index2_name on jay4(name);
alter table jay4 engine=myisam ,add fulltext index index2_name(name);--該相當修改存儲引擎並創建索引。
--創建單列索引
create index index3_name on jay7(name(5));
alter table jay7 add index index3_name(name(5));
--創建多列索引
create index index4_ph on user(phone,address);
alter table user add index index4_ph(phone,address);
--創建空間索引
create spatial index index5_sp on orcs6(space);
alter table orcs6 add spatial index index5_sp(space);
--刪除索引
drop index index_num on user;
==============================================================================
--創建視圖:使操作簡單化、增加數據安全性、提高表對的邏輯獨立性
--algorithm選項視圖選擇的算法:包括以下三種
--undefined:自動選擇所使用的算法
--merge:將使用視圖的語句與視圖定義結合起來,使得視圖定義的某一部分取代語句的對應部分。
--temptable:將視圖的結果存入臨時表中,然後使用臨時表執行語句。
use mysql;select * from user where user='root'\G --查詢root權限
--單表創建視圖
create view user_view as select * from user;
desc user_view;--查看視圖基本信息
create view user_view1(name,age,sex,ph,addr)as select name,age,sex,phone,address from user;
--在多表上創建視圖:select選項在多表中無重複的則可以不用指定表名,重複屬性必須指定以那個表的重複屬性爲準。
--with local check option:表示更新視圖時要保證在該視圖的權限範圍之內。
create algorithm=merge view score_view2(name,age,sex,English,Math,Computer) as select user.name,age,sex,English,math,computer from user,jay7 where user.num=jay7.id with local check option;
--查看視圖基本信息
show table status like 'score_view2'\G
--查詢視圖詳細信息
show create view score_view2\G
select *from information_schema.views\G--查詢所有的視圖信息
--修改視圖
use jay;create or replace algorithm=merge view user_view1(name,sex,ph,addr) as select name,sex,phone,address from user;
use jay;alter view user_view1(name,sex,ph,addr,English,Math) as select user.name,sex,phone,address,English,math from user,jay7 where user.num=jay7.id with local check option;
--更新視圖
insert into user values(1002,'jay','1234567asda89','haksdfssjdhakjasdas',56);
insert into jay7 values(1002,2002,'jay','90',77,68);
update score_view2 set name='jaywang',age=23,sex=0,English=‘70’,Math=85.5,Computer=88.6 where name='wang';
--刪除視圖:if exists 判斷視圖是否存在
drop view if exists user_view;
show create view user_view\G
==================================================================
--------------------------觸發器----------------------------------------
--創建只有一個執行語句的觸發器
create trigger user_tring1 before insert on user for each row insert into trigger_time values(now());
--當在user表中執行insert操作時,系統都會在insert操作執行前向trigger_time表中插入當前時間。
create trigger user_tring2 after insert on user for each row insert into trigger_time values(now());
--當在user表中執行insert操作時,系統都會在insert操作執行後向trigger_time表中插入當前時間。
--創建有多個執行語句的觸發器
delimiter &&--'delimiter &&'可以講默認的結束符';'變爲'&&'.
create trigger user_tring3 after delete on user for each row\
begin\
insert into trigger_time values('21:01:01');\
insert into trigger_time values('21:01:01');\
end\
&&
delimiter ;--'delimiter ;'可以將當前的結束符'&&'變爲';'.
--查看觸發器
show triggers\G
select * from information_schema.triggers where trigger_name='user_tring3'\G
--觸發器中不能包含start transaction、commit或rollback等關鍵詞,也不能包含call語句。
--刪除觸發器
drop trigger user_tring3;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章