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基础操/下的更多相关文章

  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. websocket使用指南

    前言 最近在一个项目中,需要使用到websocket,之前对websocket不是很了解,于是就花了一点时间来熟悉websocket. 在浏览器与服务器通信间,传统的 HTTP 请求在某些场景下并不理 ...

  2. 常用Markdown公式整理 && 页内跳转注意 && Markdown preview

    目录: 常用Markdown公式及注意事项 标题 列表 链接 区块 代码块 / 引用  粗体和斜体 文字块 图片 表格 横线 页内跳转注意事项 其他重要需注意 Markdown preview 前提: ...

  3. MYSQL数据库学习七 视图的操作

    7.1 视图 视图使程序员只关心感兴趣的某些特定数据和他们所负责的特定任务.提高了数据库中数据的安全性. 视图的特点如下: 视图的列可以来自不同的表,是表的抽象和在逻辑意义上建立的新关系. 视图是由基 ...

  4. 【Python】随机模块random & 日期时间のtime&&datetime

    ■ random 顾名思义,random提供了python中关于模拟随机的一些方法.这些方法都一看就懂的,不多说了: random.random() 返回0<n<=1的随机实数 rando ...

  5. 测试驱动开发实践2————从testList开始

    内容指引 1.测试之前 2.改造dto层代码 3.改造dao层代码 4.改造service.impl层的list方法 5.通过testList驱动domain领域类的修改 一.测试之前 在" ...

  6. C#基础(二)拆箱与装箱,循环与选择结构,枚举

    一.装箱和拆箱 装箱是将值类型转换为引用类型 eg: Int a=5; Object  o=a; 拆箱是将引用类型转换为值类型 eg: Int a=5; Object  o=a; Int b=(int ...

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

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

  8. C语言博客作业—数据类型

    一.PTA实验作业 题目1: 1. 本题PTA提交列表 2. 设计思路 (2)if(输入的n为奇数){ for(行数小于n/2+1时){ for(空格数等于n-2*k+1) printf(" ...

  9. 团队作业6——展示博客(Alpha版本)

    Deadline: 2017-12-3  23:00PM,以博客发表日期为准   评分基准 按时交 - 有分,检查的项目包括后文的两个方面 团队成员介绍 Alpha阶段进展 团队合作,各成员分工 Be ...

  10. 201621123057 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答: (普通方法 / 构造函数)重载. static . final.继承与多态.extends.object类.abstrac ...