MySQL基础操/下
MySQL基础操
一、自增补充
- desc (表名)t1; 查看表格信息内容 表的信息
- show create table t1(表名):也是查看信息,还不多是横向查看
- show create table t1 \G; 竖向查看自增信息
- alter table t1 AUTO_INCREMENT=3; 可以修改自增
MySQL:自增步长
基于会话级别:
- show session variables like “auto_inc%;查看全局变量
- set session auto_increment_increment=2; 设置绘画步长
- set global auto_increment_offset=10; 表示自增长字段每次递增的量,其默认值是1;
基于全局级别:
- show global variables like 'auto_inc%'; 查看全局变量
- set global auto_increment_increment=2; 设置会话步长
- # set global auto_increment_offset=10;
补充主键:一张表只有一个主键,但主键可以有多列组成;
- CREATE TABLE `t5` (
- `nid` int(11) NOT NULL AUTO_INCREMENT,
- `pid` int(11) NOT NULL,
- `num` int(11) DEFAULT NULL,
- PRIMARY KEY (`nid`,`pid`)
- ) ENGINE=InnoDB AUTO_INCREMENT=4, 步长=2 DEFAULT CHARSET= =utf8 //设置步长 及自动增加
二、唯一索引
唯一索引:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。
主键索引:不允许有空值。一般是在建表的时候同时创建主键索引。
unique 唯一索引名称 (列名,列名)//联合索引, unique uq_u1 (user_id), //唯一索引
- create table t1(
- id int ....,
- num int,
- xx int,
- unique 唯一索引名称 (列名,列名) //联合索引
- constraint ....
- )
三、外键的变种
a:用户表和部门表(一对多形式)
- 用户:
- 1 alex 1
- 2 root 1
- 3 egon 2
- 4 laoyao 3
- 部门:
- 1 服务
- 2 保安
- 3 公关
- ===》 一对多
b:用户表和博客表(一对一形式)
- 用户表:
- 1 alex
- 2 root
- 3 egon
- 4 laoyao
- 博客表:
- FK() + 唯一
- 1 /yuanchenqi/ 4
- 2 /alex3714/ 1
- 3 /asdfasdf/ 3
- 4 /ffffffff/ 2
- ===> 一对一
- create table userinfo1(
- id int auto_increment primary key,
- name char(10),
- gender char(10),
- email varchar(64)
- )engine=innodb default charset=utf8;
- create table admin(
- id int not null auto_increment primary key,
- username varchar(64) not null,
- password VARCHAR(64) not null,
- user_id int not null,
- unique uq_u1 (user_id), //唯一索引
- CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)
- )engine=innodb default charset=utf8;
c: 用户表(百合网) 相亲记录表
- 示例1:
- 用户表
- 相亲表
- 示例2:
- 用户表
- 主机表
- 用户主机关系表
- ===》多对多
- create table userinfo2(
- id int auto_increment primary key,
- name char(10),
- gender char(10),
- email varchar(64)
- )engine=innodb default charset=utf8;
- create table host(
- id int auto_increment primary key,
- hostname char(64)
- )engine=innodb default charset=utf8;
- create table user2host(
- id int auto_increment primary key,
- userid int not null,
- hostid int not null,
- unique uq_user_host (userid,hostid), //联合唯一
- CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),
- CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)
- )engine=innodb default charset=utf8;
四、SQL语句数据行操作补充
创建:
- create table tb12(
- id int auto_increment primary key,
- name varchar(32),
- age int
- )engine=innodb default charset=utf8;
增:
- insert into tb11(name,age) values('alex',12); //单行插入
- insert into tb11(name,age) values('alex',12),('root',18); //多行插入
- insert into tb12(name,age) select name,age from tb11; //将tb11里面表的内容插入tb12;
删:
- delete from tb12;
- delete from tb12 where id !=2
- delete from tb12 where id =2
- delete from tb12 where id > 2
- delete from tb12 where id >=2
- delete from tb12 where id >=2 or name='alex'
改:
- update tb12 set name='alex' where id>12 and name='xx'
- update tb12 set name='alex',age=19 where id>12 and name='xx'
查:
- select * from tb12;
- select id,name from tb12;
- select id,name from tb12 where id > 10 or name ='xxx';
- select id,name as cname from tb12 where id > 10 or name ='xxx'; //as可以修改序列名;
- select name,age,11 from tb12; //查看另一张表
其他:
- a:条件
- select * from tb12 where id != 1
- select * from tb12 where id in (1,5,12); //查看表id是1,5,12的
- select * from tb12 where id not in (1,5,12);//查看表id不是1,5,12的
- select * from tb12 where id in (select id from tb11)
- select * from tb12 where id between 5 and 12; //闭开间取范围的
- b:通配符:
- select * from tb12 where name like "a%" // a开头的所有(%多个字符串)
- select * from tb12 where name like "a_" //a开头的(— 一个字符)
- c:限制(分页):
- select * from tb12 limit 10; // 前10行
- select * from tb12 limit 0,10; //从1行开始的10行
- select * from tb12 limit 10,10; //从10行开始的10行
- select * from tb12 limit 10 offset 20; //从第10行开始的20行
- d:排序
- select * from tb12 order by id desc; 大到小
- select * from tb12 order by id asc; 小到大
- select * from tb12 order by age desc,id desc; 优先级先第一个,如果数据相同在从第二个开始从大小排序
- 取后10条数据 (先排序在取数据)
- select * from tb12 order by id desc limit 10;
- e:分组:****
- select count(id), part_id from userinfo5 group by part_id;
- count
- max
- min
- sum
- avg
- ******如果对于聚合函数结果进行第二次筛选时,必须使用having*****
- select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
- select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;
- 特别的:group by 必须在where之后,order by之前
- f:连表:********
- select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
- select * from department5 left join userinfo5 on userinfo5.part_id = department5.id
- //usernfo5 左边全部显示
- //department5 左边全部显示
- # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
- # department5右边全部显示
- userinfo5表所有显示,如果department5中无对应关系,则值为null
- select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
- 将出现null时一行隐藏
其他
MySQL基础操/下的更多相关文章
- Linux下MySQL基础及操作语法
什么是MySQL? MySQL是一种开源关系数据库管理系统(RDBMS),它使用最常用的数据库管理语言-结构化查询语言(SQL)进行数据库管理.MySQL是开源的,因此任何人都可以根据通用公共许可证下 ...
- linux下mysql基础从安装到基本使用
在LINUX下安装MYSQL #需要的安装包(按照先后顺序) libdbi-devel--2.1 libdbi--2.1 libdbi-drivers- perl-DBI-.el5 perl-DBD- ...
- Mysql基础代码(不断完善中)
Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...
- 【夯实Mysql基础】MySQL性能优化的21个最佳实践 和 mysql使用索引
本文地址 分享提纲: 1.为查询缓存优化你的查询 2. EXPLAIN 你的 SELECT 查询 3. 当只要一行数据时使用 LIMIT 1 4. 为搜索字段建索引 5. 在Join表的时候使用相当类 ...
- MYSQL基础操作
MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...
- mysql 基础篇5(mysql语法---数据)
6 增删改数据 -- ********一.增删改数据********* --- -- 1.1 增加数据 -- 插入所有字段.一定依次按顺序插入 INSERT INTO student VALUES(1 ...
- MySQL基础学习总结
1.MySQL基础概念 mysql逻辑架构如下: 每个客户端连接都会在服务器中拥有一个线程,这个连接的查询只会在这个单独的线程中执行. MySQL是分层的架构.上层是服务器层的服务和查询执行引擎,下层 ...
- 【转载】20分钟MySQL基础入门
原文:20分钟MySQL基础入门 这里持续更新修正 开始使用 MySQL 为关系型数据库(Relational Database Management System),一个关系型数据库由一个或数个表格 ...
- MySQL基础操作命令
MySQL基础操作命令 1. 查看MySQL进程 ps -ef|grep mysql |grep -v grep 2. 查看MySQL端口 ss -lnt | grep 3306 3. MySQL的启 ...
随机推荐
- Online Judge(OJ)搭建——2、数据库,SQL语句
数据库EER图 数据库表.字段.约束解释 users 用户: id 标识符,email 邮箱,password 密码,name 姓名,sex 性别,enabled 启用 ,role 角色 id pri ...
- windows下安装mongoDB以及配置启动
1.下载MongoDB的windows版本,有32位和64位版本,根据系统情况下载,下载地址:http://www.mongodb.org/downloads 2.解压缩至D:/mongodb即可 3 ...
- flex布局简析
最近开始对flex布局进行一个重新的认识. 首先. flex布局适用于所有元素 但是注意一点的就是,一旦父级元素设定flex布局的时候,子元素的传统布局属性, float,clear,vertical ...
- Algorithm --> DFS和BFS
定义结点 struct MGraph { int vexs[MAXVEX]; //顶点数组 int arc[MAXVEX][MAXVEX]; //邻接矩阵 int numVertex, numEdge ...
- SQL Quick Reference From W3Schools
SQL Statement Syntax AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition AL ...
- Linux下面如何用tcpdump抓包
很多时候我们的系统部署在Linux系统上面,在一些情况下定位问题就需要查看各个系统之间发送数据报文是否正常,下面我就简单讲解一下如何使用tcpdump抓包 tcpdump是Linux下面的一个开源的抓 ...
- C# 7.0 观察者模式 以及 delegate 和 event
观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, 它能从气象站获得这三个数据. 还 ...
- 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 ...
- 需求分析&原型设计
需求分析&原型设计 需求分析 访问软件项目真实用户 首先本项目的用户是这个需要做简单四则运算的用户(我们团队通过对家里有三四年级小学生(需要做简单四则运算)的简单采访):反映了几个主要的问题: ...
- Cocoapods最全完整使用教程
什么是cocoapods cocoapods是库管理工具. cocoapods的用途 解决库之间的依赖关系.如前文所述: 一个开源的项目可能是另一个项目的基础, A依赖B, B依赖C和D, D又依赖E ...