part-1:
#use mysql;
/*
show tables;
select * from user;
use mysql;
show databases;
#create database db1;
#drop database test;
#show enginesg; #存储引擎
#show engineG; #"g,G友好形式
#show variables like "Storage_engine%"; #系统全部存储引擎
#default InnoB
#HELP contents; #帮助
#use mysql;
#create table test( id int);
#insert into test values(1),(-1),(10000000000);
#show warnings; #警告
#select * from test;
#数据类型
#use mysql;
#create table test2( a float(1,2),b double(1,10));
#insert into test2 values(1.00000,4.00);
#create table test3 (a bit(10));
#insert into test3 values(11,b'11');
#select * from test3;
create table date_test(
f_date DATE, #日期类型
f_datetime datetime,
f_timestmp timestamp,
f_time time,
f_year year
);
insert into date_test values(curdate(),now(),now(),time(now()),year(now()));
select * from date_test;
*/
#字符型
/*
create table date_test_2(
id int,
name char(10),
sex varchar(10),
number binary(3)
);
insert into date_test_2 values(1,"张三","man",0);
insert into date_test_2 values(2,"李四","man",1);
insert into date_test_2 values(3,"吴燕","women",0);
insert into date_test_2 values(4,"赵九","man",1);
*/
#select * from date_test_1;
#use mysql;
#describe example_1; #date_test_2; #定义
#drop table example_1; #删除
#show create table example_1; #详细定义
#use mysql;
#alter table example RENAME example_2; #修改
#describe example_2;
#alter table example_2 add sex varchar(10); #最后增加列
#describe example_2;
#alter table example_2 add money int first; #在首行增加列
#describe example_2;
#alter table example_2 add weather varchar(10) after name; #指定字段后增减列
#describe example_2;
#alter table example_2 drop sex ; #删除字段
#alter table example_2 modify name varchar(10); #修改字段数据类型
#alter table example_2 change names name varcharacter(10); #修改字段名称
#并发修改字段和数据类型
#调至首位,放置其后
#alter table example_2 modify id int first ;
/*
alter table example_2 modify money int after weather;
describe example_2;
*/
#约束
/*
create table example_4(id int not null,name varchar(10),sex char(10));
#非空完整性约束
describe example_4;
*/
#试图插入数据,遭非空约束屏蔽
#insert into example_4(id,name,sex)values(null,"王明","man");
#字段默认值
#drop table example_4;
#use mysql;
#create table example_6( id int not null, name varchar(20) default '王大牛', sex char(10));
#describe example_6;
#create table example_7(id int not null, name char(10) unique,sex varchar(10) default 'man');
/*drop table example_7;
#唯一约束
create table example_7(id int not null, name char(10) ,
constraint uk_name unique(name)); #约束名:唯一简写_字段名
*/ #主键约束:非空+唯一
/*
create table example_7( id int primary key,name char(10),sex varchar(20));
describe example_7; #试图插入重复数据,报错脚本显示主建生效
insert into example_7(id,name,sex)values(1,"李兰","women");
insert into example_7(id,name,sex)values(1,"江山","man");
*/
/*
create table example_7(id int,name char(10),tempature bit(11),
constraint PK_id_tempature primary key(id,tempature) #联合主键
);
*/
/* #自动属性
drop table example_7;
#create table example_7(id int primary key auto_increment,name char(10),sex varchar(20));
describe example_7;
*/
use mysql;
create table company(
id int primary key,
name char(10),
login time
); #父表注册主键为id字段
create table employ(
pople varchar(10),
sex char(10),
id int primary key,
constraint FK_id foreign key(id) references company(id) #子表外键id字段继承父表id属性
);
describe employ;
describe company;
part-2:
/*
use mysql;
drop table index_test;
create table index_test(
id int,
name char(10),
index index_x(name) #在表的新建过程中引入索引到name字段
);
show create table index_test;
EXPLAIN #检验索引是不是有效
select * from index_test where name=1;
*/
#use mysql; #在原有表的基础上创建索引
#create index index_test_y on index_test(id);
#show create table index_test;
/*
alter table index_test add sex varchar(20); #通过修改表格时创建索引
alter table index_test add index index_test_z(sex);
explain
select * from index_test where sex=1;
*/
/*
create table index_test1(
id int,
name char(10),
unique index index_test_w(id) #唯一索引{指定自动索引和手动索引}
);
show create table index_test1;
*/
/*
create unique index index_test_q on index_test1(name); #在已存在的表上创建唯一索引
show create table index_test1;
*/
/*
alter table index_test1 add sex varchar(20);
alter table index_test1 add unique index index_test_r(sex); #修改表格的基础上映射sex字段唯一索引
explain
select * from index_test1 where sex=1;
*/
/*
create table index_test2(
id int,
name varchar(40),
fulltext index index_test2_x(name) #创建表格的同时为name字段设置全文索引
); #fulltext针对{char,varchar,test}
show create table index_test2;
*/
/*
explain
select * from index_test2 where name=1;
*/
/*
drop table index_test2;
create table index_test2(
id char(10) ,
name varchar(30)
);
#create fulltext index index_test2_y on index_test2(name); #在已有的表格上为name字段设置全文索引
#show create table index_test2;
alter table index_test2 add fulltext index index_test2_z(id); #修改时增加id字段全文索引
show create table index_test2;
*/
/*
create table index_test3(
id int,
name char(10),
index index_test3_x(id,name)
);
show create table index_test3; #在创建表格的同时,指定id和name字段关联为多列索引
*/
/*
create table index_test4(
id int,
name varchar(10)
);/*
create index index_test4_x on index_test4(id,name); #在已存在的表格上为id和name字段关联多列索引
show create table index_test4;
*/
/*
drop table index_test5;
create table index_test5( #修改字段的同时为id和name设置多列索引
id int,
name varchar(10)
);
alter table index_test5 add index index_test_5(id,name);
show create table index_test5;
*/
use mysql;
drop index index_test_5 on index_test5; #删除索引
use mysql;
drop view view_index5;
create view view_index5
as #把id,name字段看成别名作为视图的特定查询
select id,name from index_test5;
select * from view_index5;
describe view_index5;
#视图封装
drop view view_x;
create view view_x
as
select 3.1415926; #封装数据类型视图
select * from view_x;
insert into index_test5 values(1,"xm");
insert into index_test5 values(4,"wu");
insert into index_test5 values(3,"zl");
drop view view_y;
create view view_y #封装按统计id行数的视图
as
select count(id) from index_test5 ;
select * from view_y;
drop view view_z;
create view view_z
as
select * from index_test5 order by id asc; #desc; #封装按id字段升序|降序的视图
select * from view_z;
*/
drop table view_test_x;
create table view_test_x(
id int,
name char(10)
);
/*
insert into view_test_x values(1,"xm");
insert into view_test_x values(4,"zw");
insert into view_test_x values(10,"lt");
drop table view_test_y;
create table view_test_y(
number int,
sex char(10)
);
insert into view_test_x values(1,"man");
insert into view_test_x values(5,"women");
insert into view_test_x values(10,"women");
*/
# drop view view_f;
create view view_f
as
select * from view_test_x; #封装联合表view_test_x和view_test_y的视图
/*
union all
select * from view_test_y;
select * from view_f;
show tables; #可用来显示视图名
show table status from mysql like "view_f"; #来自mysql库内的视图状态
#like通配查询
*/
show create view view_f; #显示视图的详细信息
desc view_f;
show databases;
use information_schema;
desc views;
select * from views where TABLE_NAME="view_f"; #通过系统库的全局映射查询指定视图
PART-3
USE mysql;
#为库创建两张表
CREATE TABLE tb_x(
id INT,
NAME CHAR(10)
);
CREATE TABLE tb_y(
times DATE,
weather VARCHAR(20)
);
#创建触发器
CREATE TRIGGER tri_x
BEFORE INSERT
#在insert前触发
ON tb_x FOR EACH ROW #指定在下一条语句生效后立即触发
INSERT INTO tb_y VALUES(TIME(NOW()),"sun");
#试图向x表插入数据,以引起y表使用触发条件
INSERT INTO tb_x VALUES(1,"约翰");
#检验触发效果
SELECT * FROM tb_x;
SELECT * FROM tb_y;
SHOW TRIGGERS;
DELIMITER $$ #为语句的结束设置别名
CREATE TRIGGER tri_a #营造触发
BEFORE INSERT #在insert后触发
ON tb_x FOR EACH ROW #选择触发表
BEGIN #开始
INSERT INTO tb_y VALUES(NOW(),"杰克"); #多条触发条件
INSERT INTO tb_y VALUES(NOW(),"李向");
END$$ #结束
DELIMITER;
#在tb_x中insert以触发tri_a
INSERT INTO tb_x VALUES(10,"jk");
#检验触发
SELECT * FROM tb_y;
SHOW TRIGGERS; #全局显示触发器
SHOW DATABASES; #在系统表内显示触发器
USE information_schema;
SHOW TABLES;
SELECT * FROM TRIGGERS;
DROP TRIGGER tri_a; #删除出触发器
part-4:
USE mysql;
DROP TABLE test_x;
CREATE TABLE test_x
(
id INT,
NAME CHAR(10)
);
CREATE TABLE test_y
(
id INT,
NAME CHAR(10)
);
#多插
INSERT INTO test_x VALUES(NULL,NULL),(NULL,NULL),(NULL,NULL);
INSERT INTO test_y(id,NAME)
SELECT id,NAME FROM test_x;
#将查询结果插入记录
SELECT * FROM test_y;
UPDATE test_y
SET id=1
WHERE NAME="wd";
DELETE FROM test_y
WHERE id=2;
SELECT DISTINCT * FROM test_x;
#distinct 排除重复的字段
SELECT (id+10)/(10*9-1) AS math_id,NAME FROM test_x;
#支持四则运算,“AS”作为别名
SELECT CONCAT(id,NAME) total
FROM test_x; #同时为多列合并成新列
SELECT DISTINCT * FROM test_x #逻辑运算和比较运算符筛选字段
#where id=2 and name="wd";
#where id=2 && name="wd";
#where id=2 || name="wd";
#where id!=10 and id>2&&id<110;
#where id not between 5 and 10;#between | not between关键字筛选
#where id is not null; #null| not null关键字筛选
#where not id is null;
#where id not in(5,10); #in | not in:关键字查询
#where name like "_p%";
#where name not like "%t%";
/*like关键字模糊匹配{%:特定字符开头;
_:特定字符;
%char_name%:包含字符;
%%:《=》*;
*/
#where id in(2,10) order by id asc limit 2;#desc;
WHERE id BETWEEN 2 AND 10 LIMIT 2,3;
#排序{asc:升序;desc:降序};
#limit:限制显示字段的条目数量{数组起始值,步数};