MySQL基础操

一、自增补充

  1. desc (表名)t1 查看表格信息内容 表的信息
  2. show create table t1(表名):也是查看信息,还不多是横向查看
  3. show create table t1 \G; 竖向查看自增信息
  4. alter table t1 AUTO_INCREMENT=3; 可以修改自增

MySQL:自增步长

 基于会话级别:

  1. show session variables like auto_inc%;查看全局变量
  2. set session auto_increment_increment=2; 设置绘画步长
  3. set global auto_increment_offset=10; 表示自增长字段每次递增的量,其默认值是1;

基于全局级别:

  1. show global variables like 'auto_inc%'; 查看全局变量
  2. set global auto_increment_increment=2; 设置会话步长
  3. # set global auto_increment_offset=10;

补充主键:一张表只有一个主键,但主键可以有多列组成;  

  1. CREATE TABLE `t5` (
  2. `nid` int(11) NOT NULL AUTO_INCREMENT,
  3. `pid` int(11) NOT NULL,
  4. `num` int(11) DEFAULT NULL,
  5. PRIMARY KEY (`nid`,`pid`)
  6. ) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET= =utf8 //设置步长 及自动增加

二、唯一索引

唯一索引:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。

主键索引:不允许有空值。一般是在建表的时候同时创建主键索引。

unique 唯一索引名称 (列名,列名)//联合索引,  unique uq_u1 (user_id), //唯一索引 

  1. create table t1(
  2. id int ....,
  3. num int,
  4. xx int,
  5. unique 唯一索引名称 (列名,列名) //联合索引
  6. constraint ....
  7. )

三、外键的变种

a:用户表和部门表(一对多形式)

  1. 用户:
  2. 1 alex 1
  3. 2 root 1
  4. 3 egon 2
  5. 4 laoyao 3
  6.  
  7. 部门:
  8. 1 服务
  9. 2 保安
  10. 3 公关
  11. ===》 一对多

b:用户表和博客表(一对一形式) 

  1. 用户表:
  2. 1 alex
  3. 2 root
  4. 3 egon
  5. 4 laoyao
  6. 博客表:
  7. FK() + 唯一
  8. 1 /yuanchenqi/ 4
  9. 2 /alex3714/ 1
  10. 3 /asdfasdf/ 3
  11. 4 /ffffffff/ 2
  12.  
  13. ===> 一对一
  1. create table userinfo1(
  2. id int auto_increment primary key,
  3. name char(10),
  4. gender char(10),
  5. email varchar(64)
  6. )engine=innodb default charset=utf8;
  7.  
  8. create table admin(
  9. id int not null auto_increment primary key,
  10. username varchar(64) not null,
  11. password VARCHAR(64) not null,
  12. user_id int not null,
  13. unique uq_u1 (user_id), //唯一索引
  14. CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
  15. )engine=innodb default charset=utf8;

 c: 用户表(百合网) 相亲记录表

  1. 示例1
  2. 用户表
  3. 相亲表
  4.  
  5. 示例2
  6. 用户表
  7. 主机表
  8. 用户主机关系表
  9. ===》多对多
  10. create table userinfo2(
  11. id int auto_increment primary key,
  12. name char(10),
  13. gender char(10),
  14. email varchar(64)
  15. )engine=innodb default charset=utf8;
  16.  
  17. create table host(
  18. id int auto_increment primary key,
  19. hostname char(64)
  20. )engine=innodb default charset=utf8;
  21.  
  22. create table user2host(
  23. id int auto_increment primary key,
  24. userid int not null,
  25. hostid int not null,
  26. unique uq_user_host (userid,hostid), //联合唯一
  27. CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
  28. CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
  29. )engine=innodb default charset=utf8;

、SQL语句数据行操作补充

创建:

  1. create table tb12(
  2. id int auto_increment primary key,
  3. name varchar(32),
  4. age int
  5. )engine=innodb default charset=utf8;

增: 

  1. insert into tb11(name,age) values('alex',12); //单行插入
  2. insert into tb11(name,age) values('alex',12),('root',18); //多行插入
  3. insert into tb12(name,age) select name,age from tb11; //将tb11里面表的内容插入tb12;

删:

  1. delete from tb12;
  2. delete from tb12 where id !=2
  3. delete from tb12 where id =2
  4. delete from tb12 where id > 2
  5. delete from tb12 where id >=2
  6. delete from tb12 where id >=2 or name='alex'

改:

  1. update tb12 set name='alex' where id>12 and name='xx'
  2. update tb12 set name='alex',age=19 where id>12 and name='xx'

查:

  1. select * from tb12;
  2. select id,name from tb12;
  3. select id,name from tb12 where id > 10 or name ='xxx';
  4. select id,name as cname from tb12 where id > 10 or name ='xxx'; //as可以修改序列名;
  5. select name,age,11 from tb12; //查看另一张表 

其他:

  1. a:条件
  2. select * from tb12 where id != 1
  3. select * from tb12 where id in (1,5,12); //查看表id是1,5,12的
  4. select * from tb12 where id not in (1,5,12);//查看表id不是1,5,12的
  5. select * from tb12 where id in (select id from tb11)
  6. select * from tb12 where id between 5 and 12; //闭开间取范围的
  7.  
  8. b:通配符:
  9. select * from tb12 where name like "a%" // a开头的所有(%多个字符串)
  10. select * from tb12 where name like "a_" //a开头的(— 一个字符)
  11.  
  12. c:限制(分页):
  13. select * from tb12 limit 10; // 前10行
  14. select * from tb12 limit 0,10; //从1行开始的10行
  15. select * from tb12 limit 10,10; //从10行开始的10行
  16. select * from tb12 limit 10 offset 20; //从第10行开始的20行
  17.  
  18. d:排序
  19. select * from tb12 order by id desc; 大到小
  20. select * from tb12 order by id asc; 小到大
  21. select * from tb12 order by age desc,id desc; 优先级先第一个,如果数据相同在从第二个开始从大小排序
  22. 取后10条数据 (先排序在取数据)
  23. select * from tb12 order by id desc limit 10;
  24.  
  25. e:分组:****
  26. select count(id), part_id from userinfo5 group by part_id;
  27. count
  28. max
  29. min
  30. sum
  31. avg
  32. ******如果对于聚合函数结果进行第二次筛选时,必须使用having*****
  33. select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
  34. select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;
  35. 特别的:group by 必须在where之后,order by之前
  36.  
  37. f:连表:********
  38. select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
  39. select * from department5 left join userinfo5 on userinfo5.part_id = department5.id
  40. //usernfo5 左边全部显示
  41. //department5 左边全部显示
  42.  
  43. # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
  44. # department5右边全部显示
  45.  
  46. userinfo5表所有显示,如果department5中无对应关系,则值为null
  47.  
  48. select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
  49. 将出现null时一行隐藏

其他

MySQL基础操/下的更多相关文章

  1. Linux下MySQL基础及操作语法

    什么是MySQL? MySQL是一种开源关系数据库管理系统(RDBMS),它使用最常用的数据库管理语言-结构化查询语言(SQL)进行数据库管理.MySQL是开源的,因此任何人都可以根据通用公共许可证下 ...

  2. linux下mysql基础从安装到基本使用

    在LINUX下安装MYSQL #需要的安装包(按照先后顺序) libdbi-devel--2.1 libdbi--2.1 libdbi-drivers- perl-DBI-.el5 perl-DBD- ...

  3. Mysql基础代码(不断完善中)

    Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...

  4. 【夯实Mysql基础】MySQL性能优化的21个最佳实践 和 mysql使用索引

    本文地址 分享提纲: 1.为查询缓存优化你的查询 2. EXPLAIN 你的 SELECT 查询 3. 当只要一行数据时使用 LIMIT 1 4. 为搜索字段建索引 5. 在Join表的时候使用相当类 ...

  5. MYSQL基础操作

    MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...

  6. mysql 基础篇5(mysql语法---数据)

    6 增删改数据 -- ********一.增删改数据********* --- -- 1.1 增加数据 -- 插入所有字段.一定依次按顺序插入 INSERT INTO student VALUES(1 ...

  7. MySQL基础学习总结

    1.MySQL基础概念 mysql逻辑架构如下: 每个客户端连接都会在服务器中拥有一个线程,这个连接的查询只会在这个单独的线程中执行. MySQL是分层的架构.上层是服务器层的服务和查询执行引擎,下层 ...

  8. 【转载】20分钟MySQL基础入门

    原文:20分钟MySQL基础入门 这里持续更新修正 开始使用 MySQL 为关系型数据库(Relational Database Management System),一个关系型数据库由一个或数个表格 ...

  9. MySQL基础操作命令

    MySQL基础操作命令 1. 查看MySQL进程 ps -ef|grep mysql |grep -v grep 2. 查看MySQL端口 ss -lnt | grep 3306 3. MySQL的启 ...

随机推荐

  1. Online Judge(OJ)搭建——2、数据库,SQL语句

    数据库EER图 数据库表.字段.约束解释 users 用户: id 标识符,email 邮箱,password 密码,name 姓名,sex 性别,enabled 启用 ,role 角色 id pri ...

  2. windows下安装mongoDB以及配置启动

    1.下载MongoDB的windows版本,有32位和64位版本,根据系统情况下载,下载地址:http://www.mongodb.org/downloads 2.解压缩至D:/mongodb即可 3 ...

  3. flex布局简析

    最近开始对flex布局进行一个重新的认识. 首先. flex布局适用于所有元素 但是注意一点的就是,一旦父级元素设定flex布局的时候,子元素的传统布局属性, float,clear,vertical ...

  4. Algorithm --> DFS和BFS

    定义结点 struct MGraph { int vexs[MAXVEX]; //顶点数组 int arc[MAXVEX][MAXVEX]; //邻接矩阵 int numVertex, numEdge ...

  5. SQL Quick Reference From W3Schools

    SQL Statement Syntax AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition AL ...

  6. Linux下面如何用tcpdump抓包

    很多时候我们的系统部署在Linux系统上面,在一些情况下定位问题就需要查看各个系统之间发送数据报文是否正常,下面我就简单讲解一下如何使用tcpdump抓包 tcpdump是Linux下面的一个开源的抓 ...

  7. C# 7.0 观察者模式 以及 delegate 和 event

    观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, 它能从气象站获得这三个数据. 还 ...

  8. Leetcode 15——3Sum

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  9. 需求分析&原型设计

    需求分析&原型设计 需求分析 访问软件项目真实用户 首先本项目的用户是这个需要做简单四则运算的用户(我们团队通过对家里有三四年级小学生(需要做简单四则运算)的简单采访):反映了几个主要的问题: ...

  10. Cocoapods最全完整使用教程

    什么是cocoapods cocoapods是库管理工具. cocoapods的用途 解决库之间的依赖关系.如前文所述: 一个开源的项目可能是另一个项目的基础, A依赖B, B依赖C和D, D又依赖E ...