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