【mysql学习-1】
part-1:
#use mysql;
/*
show tables;
select * from user;
use mysql;
show databases;
#create database db1;
#drop database test;
#show engines\g; #存储引擎
#show engine\G; #"\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:限制显示字段的条目数量{数组起始值,步数};
【mysql学习-1】的更多相关文章
- 我的MYSQL学习心得(一) 简单语法
我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- 我的MYSQL学习心得(二) 数据类型宽度
我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- 我的MYSQL学习心得(三) 查看字段长度
我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- 我的MYSQL学习心得(四) 数据类型
我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(五) 运 ...
- 我的MYSQL学习心得(五) 运算符
我的MYSQL学习心得(五) 运算符 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...
- 我的MYSQL学习心得(六) 函数
我的MYSQL学习心得(六) 函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...
- 我的MYSQL学习心得(七) 查询
我的MYSQL学习心得(七) 查询 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...
- 我的MYSQL学习心得(八) 插入 更新 删除
我的MYSQL学习心得(八) 插入 更新 删除 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得( ...
- 我的MYSQL学习心得(九) 索引
我的MYSQL学习心得(九) 索引 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类 ...
- 我的MYSQL学习心得(十) 自定义存储过程和函数
我的MYSQL学习心得(十) 自定义存储过程和函数 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心 ...
随机推荐
- haproxy/nginx+keepalived负载均衡 双机热备 邮件报警 实战及常见问题
Haproxy 做http和tcp反向代理和负载均衡keepalived 为两台 Haproxy 服务器做高可用/主备切换.nginx 为内网服务器做正向代理,如果业务需求有变化,也可以部分替代 ...
- html跨域获取数据
a.com下的a.html,需要嵌入b.com下的b.html.这时建一个静态页面c.html将c.html放到a.com服务器中.b.html在嵌入c.html.这样,将参数值传输到c.html中, ...
- JSP初学者3
reponse代表服务器对客户端的响应.大部分时候,程序无须使用response来响应客户端请求,因为有更简单的响应对象——out,它代表页面输出流. 但out无法响应生成非字符内容(out是JspW ...
- PHP接口对方(C#)接收不到数据?ContentLength=-1
工作时遇到的问题.浏览器可以查看到json数据,但是对方死活收不到数据. 最后发现发现ContentLength=-1. 找到问题:为什么.NET程序下载获得的ContentLength=-1? 摘抄 ...
- ExecutorService的invokeAny方法
一.此方法获得最先完成任务的结果,即Callable<T>接口中的call的返回值,在获得结果时,会中断其他正在执行的任务 示例代码: import java.util.ArrayList ...
- java面试题之----super和this
super和this的异同: super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句) this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句) supe ...
- SSH免密钥互信及企业案例
SSH远程免密码: 原理简述:客户端 创建公钥和私钥,公钥是锁,私钥是自己留在客户端的钥匙. 客户端将公钥(锁)发出到服务端,以后自己就能用客户端本地的私钥进行登陆,不用输入密码! 1.客户端,和服 ...
- 关于Oracle死锁处理方法
关于数据库死锁的检查方法一. 数据库死锁的现象程序在执行的过程中,点击确定或保存按钮,程序没有响应,也没有出现报错.二. 死锁的原理当对于数据库某个表的某一列做更新或删 ...
- IOS 应用管理(九宫格) 总结笔记
1. 开发前的思路 ========================================1> 从mainBundle中加载Plist2> 按照plist中的数据数量先确定各个a ...
- bzoj 3028 生成函数
计算完后为 f(x): 根据我翻高数书,终于推倒出来了. (- ̄▽ ̄)-