2018/12/6 星期四 19:34:07

authot by dabaine

数据库注释;

 -- 这就是注释
/*.....*/ 这也是注释

创建库;

create databse [if not exists] dabaine [character set "utf8"];

查看所有数据库;

show databses;

查看数据库结构:

show create database dabaine;

查看当前数据库;

select database();

修改数据库;

alter database dabaine [character set "gbk"];

删除数据库;

drop database [if exists] dabaine;

使用数据库;

use database;

创建表;

create table dabaine(
id smallint(10) primary key not null auto_increment,
name varchar(25) not null,
gender boolean not null
);

删除表;

drop table dabaine;

查看表结构;

eg1:show create table dabaine;
eg2:show columns from dabaine;

查看表的全部信息;

desc dabaine;

修改表结构;

增加字段:
alter table dabaine add [column],add [column]......;
修改类型:
alter table dabaine modify colum_name attribute [first|after column_name] colum_name;
修改列名:
alter table dabaine change column_name new_column_name type [约束条件];
删除字段:
alter table dabaine drop [column];
重命名:
rename table table_name to new_table_name;

修改表内容;

插入:
eg1:insert into dabaine (id, name) values(1,"dabaine");
eg2:insert into dabaine set id = 2,name="dabaine";
更新:
update dabaine set name="cody" where name="dabaine";
删除:
eg1:delete from dabaine where name = "cody";
eg2:truncate table dabaine; --把表摧毁,重新创建一张新表;

查询顺序;

select [distinct] *|field ... from dabaine
where (不分组筛选)
group by field
having (分组后筛选)
order by field
limit

查询别名;

selct distinct id + 10 as id from dabaine;

执行顺序;

from,where,select,group by,having, order by

聚合函数;

select name, sum(grade) from dabaine group by name;
ifnull(grade,0) --如果grade为空,则给它定为0;

外键约束;

创建主表:
create table class(
id int(10) primary key auto_increment,
name varchar(20),
age int(5)
);
主表添加数据(多条):
insert into class(name,age) values
("cody",18),
("solider",19),
("guan",21),
("lee",22),
("strong",28),
("pig",38);
创建子表:
create table student(
id int(10) primary key auto_increment,
name varchar(20),
age int(5),
teacher_id int(10), --绑定外键的字段要和主表中的字段类型保持一致;
constraint dabaine --给外键命名大白讷
foreign key (teacher_id) --给子表的属性选择外键绑定
references class(id) --映射主表的属性(追随主表的id字段)
);
子表添加数据:
insert into student(name,age,teacher_id) values
("cody",18,1),
("solider",19,2),
("guan",21,3),
("lee",22,4),
("strong",28,5),
("pig",38,6);
这时,主表和子表已经有关联了,不可以随便删除主表的记录;
增加外键:
alter table son_table_name add constraint cody
foreign key(son_table_field)
references primary_table(field);
删除外键:
alter table son_table_name drop foreign key cody;

级联删除(cascade);

create table studentNew(
id int(10) primary key auto_increment,
name varchar(20),
age int(5),
teacher_id int(10),
constraint cody foreign key (teacher_id)
references class(id)
on delete cascade --级联删除
);
constraint cody foreign key (teacher_id)
references class(id)
on delete set null --主表删除后,子表记录设置为空值,且子表的字段属性不能设置为not null;
on delete restrict --拒绝对主表进行更新删除操作;
on delete no action --类似于restrict

多表查询;

笛卡尔积连接:
A表中的全部数据m条 * B表中的全部数据n条;
连接查询~内连接:
inner join
eg1:select tableA.id,tableA.name,tableB.name from
tableA,tableB where tableA.id = tableB.tableA_id
eg2:select tableA.id,tableA.name,tableB.name from tableA
inner join tableB on tableA.id = tableB.tableA_id
+---------+----+---------+
| name | id | name |
+---------+----+---------+
| cody | 1 | cody |
| solider | 2 | solider |
| guan | 3 | guan |
| cody | 4 | lee |
| strong | 5 | strong |
| lee | 6 | pig |
+---------+----+---------+
连接查询~左外连接(左连接):
left join
select tableA.id,tableA.name,tableB.name from tableA
left join tableB on tableA.id = tableB.tableA_id
--左连接以左表为主,select所选择的字段,左表中的记录会全部显示,而右表会去匹配左表里的记录,没有的则显示空值;
+----+---------+---------+
| id | name | name |
+----+---------+---------+
| 1 | cody | cody |
| 2 | solider | solider |
| 3 | guan | guan |
| 4 | lee | cody |
| 5 | strong | strong |
| 6 | pig | lee |
+----+---------+---------+
连接查询~右外连接(右连接):
right join
类似左连接,以右表为主;
+------+---------+---------+
| id | name | name |
+------+---------+---------+
| 1 | cody | cody |
| 4 | lee | cody |
| 2 | solider | solider |
| 3 | guan | guan |
| 6 | pig | lee |
| 5 | strong | strong |
| NULL | NULL | pig |
+------+---------+---------+

嵌套;

查询嵌套:
select * from table_name where field in (select field from table_name);
复制表:
create table new_table(select * from old_table); --原表中的约束不会复制过来,需要重新添加
selcet * from table_name where exists
(selcet field from table_name where....)
--exists 后面的语句会返回一个布尔值,true则执行前面的select语句,
flase 则返回空值;

索引;

	unique(唯一索引),fulltext(全局索引),spatial(空间索引),index|key(普通索引)
添加索引:
eg1:create
[unique|fulltext|spatial] index|key
index_name on table_name (字段名[(长度)] [asc|desc]);
eg2:alter table table_name
add [unique|fulltext|spatial] index|key index_name (字段名[(长度)] [asc|desc]);
删除索引:
drop index index_name on table_name;
unique:唯一索引的字段不能重复;
多列索引:给多个字段添加索引 (field1,field2...)

事务;

     start transaction; --开启事务
Rollback; --回滚事务(撤销)
Commit; --提交事务;
savepoint; 保留点,事务处理中的临时占位符; savepoint name;
rollback to svaepoint_name;

存储过程;

MySql的基操勿六的更多相关文章

  1. Mysql的基操

    创建一个数据库   (myschool是数据库名) create database myschool; 删除数据库 drop database myschool 创建一个表:(Student是 表名) ...

  2. 我的MYSQL学习心得(十六) 优化

    我的MYSQL学习心得(十六) 优化 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  3. mysql基操

    创建数据表: create table tt1( id int, name varchar(20), age int,sex boolean ); insert into tt1 values(1,& ...

  4. 替小白整理的 linux基操命令 切勿扣6 不用感谢

    Linux --------小白必会的基本命令 命令行提示字符[root@localhost ~]#[当前登录系统的用户@主机名称 当前所在的目录]## 表示为管理员登录$ 表示为普通用户登录   切 ...

  5. MySQL数据库学习笔记(六)----MySQL多表查询之外键、表连接、子查询、索引

    本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...

  6. MySql:SELECT 语句(六) CONCAT() 函数的使用

    一.计算字段 为什么要用计算字段? 1)想要在一个字段中既显示公司地址,又显示公司名称,但是往往这两个都不在一个字段中 2)列数据是大小写混合的,但是报表程序需要把他们全部按大写形式展示出来 3)需要 ...

  7. MySQL高级知识(十六)——小表驱动大表

    前言:本来小表驱动大表的知识应该在前面就讲解的,但是由于之前并没有学习数据批量插入,因此将其放在这里.在查询的优化中永远小表驱动大表. 1.为什么要小表驱动大表呢 类似循环嵌套 for(int i=5 ...

  8. Redis基操

    Redis key-value类型的缓存数据库 指定IP和端口连接redis: ./redis-cli -h ip -p port Redis基本操作命令 命令 返回值 简介 ping PONG 测试 ...

  9. MongoDB基操

    基本概念 database 数据库 包含多个collection collection 集合 包含多个文档document(类JSON对象) document 文档 一个文档对象中包含多个key-va ...

随机推荐

  1. 解决IIS中运行TopJUI左侧菜单不显示的问题

    TopJUI演示系统中,模拟数据保存在.json文件中,目前发现有部分用户的IIS容器默认情况下是不支持.json文件的请求的,因此需要配置一下,可参考下文配置解决: 一.IIS 6 1. MIME设 ...

  2. TTM-To the moon

    传送门 查询历史版本,回到历史版本,这个题目显然是用主席树,好像就没了! 但是这里的修改是区间修改,众所周知主席树的空间复杂度是\(nlog(n)\)的,区间修改会导致主席树的开点到达一个相当恐怖的数 ...

  3. mysql_innodb引擎

    innodb概括 1.Innodb是一种事务性存储引擎 2.完全支持事务的ACID特性 3.实现事务特性的原理: 使用Redo Log和Undo Log,Undo Log用于帮助未提交事务进行回滚,R ...

  4. 给ACM newer的编程技巧

    一.复杂度 1.1什么是复杂度? 在设计满足问题要求的算法时,复杂度的估算是非常重要的.我们不可能把每个想到的算法实现一遍看看是否足够快.应当通过估计算法的复杂度来判断所想的算法是否足够高效. 1.2 ...

  5. 洛谷 P4503 [CTSC2014]企鹅QQ

    暴力枚举不同的一位即可.. 主要是常数问题 1.统计答案时用sort速度快于用tr1/unordered_map,后者又快于map (tr1/unordered_map完全达不到理论复杂度上的O(1) ...

  6. python转换已转义的字符串

    python转换已转义的字符串 有时我们可能会获取得以下这样的字符串: >>> a = '{\\"name\\":\\"michael\\"} ...

  7. Linux 批量杀进程的命令

    使用awk批量杀进程的命令: ps -ef | grep firefox | grep -v grep | awk '{print "kill -9 "$2}'|sh #列出了当前 ...

  8. table表格字母无法换行

    在项目中,用到的table比较多,本来布局挺好的,后来在td内写入英文字母,整个布局就乱了,会撑的很宽,不换行,后来才知道:一般字母的话会被浏览器默认是一个字符串或者说一个单词,所以不会自动换行. 于 ...

  9. Highcharts在IE8中不能一次性正常显示的一种解决办法

    由于客户要求必须在IE浏览器下兼容图表,故选用了兼容性较好的Highcharts.另外说一句,博主尝试过ichartjs.ECharts.YUI,兼容性都没有Highcharts给力(所有的兼容性问题 ...

  10. RunTests.sh && RunIPhoneSecurityd.sh

    https://github.com/gh-unit/gh-unit/blob/master/Scripts/RunTests.sh     #!/bin/sh   # If we aren't ru ...