库增删该查,表增删该查,记录增删该查,表与表关系(多对多,多对一,一对一),mysql用户管理
表增删该查
进入库名
use db1
select databases() 创建表
create table t1(name char(20),age int(10)); 查看表
show tables
show create table t1; desc(ribe) t2 : 查看表结构 删除
drop table t1 修改
alter table t2 modify name char(20) 修改字段名
alter table t2 rename t1 修改表明 ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型;
alter table t1 change name user char(16) 修改字段属性
字段增删该查
增加
insert into t1 (usr, age) values ("aa",18 ),("bb",8); 查
select * from t1 改
update t1 set age=28 where usr="aa" 删除
delete from t1 where age>8
表的详细操作
拷贝表
create table t1 like t2; 拷贝空表
create table t1 select * from t2; (结构+数据) 连同表记录一起拷贝过来,不复制表约束 truncate tt; 清空表,自增字段重置 alter table t1 engine=innodb charset=gbk; 修改表的引擎,字符编码集
表的字段操作
修改字段信息
alter table 表名 modify 字段名 类型[(宽度) 约束]; 修改字段信息
alter table 表名 modify 字段名 类型[(宽度) 非键约束] first; 移动字段到最前面
alter table 表名 modify 字段名 类型[(宽度) 非键约束] after 指定字段前; 移动到字段到指定字段后面 修改字段名及信息
alter table 表名 change 旧字段名 新字段名 类型[(宽度) 约束条件] 末尾添加
alter table 表名 add 字段名 类型[(宽度) 约束], ..., add 字段名 类型[(宽度) 约束];
alter table t1 add name char(5), add sex enum('male','femail','other') 首行添加
alter table 表名 add 字段名 类型[(宽度) 约束] first; 指定字段的后面添加
alter table 表名 add 字段名 类型[(宽度) 约束] after 旧字段名; 删除字段名
alter table 表名 drop 字段名;
表关系
多对一 (关键字设置在多的那一方)
建表规则:
先建立主表,再建立从表,在从表中设置主表的唯一字段(通常为主键)作为外键
建表语法
建主表:
create table 主表(
id int primary key auto_increment,
...
); create table dep(
id int primary key auto_increment,
name varchar(16),
work varchar(16)
);
建从表
create table 从表(
id int primary key auto_increment,
...
主表_id int, # 只是在从表中起了一个名字, 该名字和主表主键对应,所有起了个见名知义的名字
foreign key(主表_id) references 主表(唯一字段名id)
on update cascade
on delete cascade
); create table emp(
id int primary key auto_increment,
name varchar(16),
salary float,
dep_id int,
foreign key(dep_id) references dep(id) 外键设置在多的那一方
on update cascade # 设置级联
on delete cascade
);
多对多(关系确立在第三张表上)
建表规则:
新建第三张表,通过两个外键形成多对多关系 建表语法:
create table 表1(
id int primary key auto_increment,
...
);
create table book(
id int primary key auto_increment,
name varchar(16),
price int
);
create table 表2(
id int primary key auto_increment,
...
);
create table author(
id int primary key auto_increment,
name varchar(16)
);
建第三张表建立关系
create table 关系表(
id int primary key auto_increment,
表1_id int,
表2_id int,
foreign key(表1_id) references 表1(id)
on update cascade
on delete cascade,
foreign key(表2_id) references 表2(id)
on update cascade
on delete cascade
);
create table book_author(
id int primary key auto_increment,
book_id int,
author_id int,
foreign key(book_id) references book(id) 第三张表与第一张表建立关系
on update cascade
on delete cascade,
foreign key(author_id) references author(id) 第三张表与第一张表建立关系
on update cascade
on delete cascade );
一对一(关系确立在第一张表上)
建表规则:
未存放外键的表被依赖,称之为左表;存放外键的表示依赖表,称之为右表;先操作左边再操作右表
建表语法:
create table 左表(
id int primary key auto_increment,
...
);
create table husband(
id int primary key auto_increment,
name varchar(16)
);
create table 右表(
id int primary key auto_increment,
...
左表_id int unique, # 一对一的外键需要唯一性
foreign key(左表_id) references 左表(id)
on update cascade
on delete cascade
);
create table wife(
id int primary key auto_increment,
name varchar(16),
husband_id int unique, # 一对一的外键需要唯一性
foreign key(husband_id) references husband(id)
on update cascade
on delete cascade
);
mysql用户管理
-特殊表: (mysql.user)
查看当前登陆用户 select user(); 重要字段: Host | User | Password
select Host,User,Password from user; 新建用户
create user 用户名@主机名 identified by '密码' 设置用户权限
grant create on db1.* to zero@localhost with grand option 给用创建表的权限 设置权限是如果没有当前用户,会自动创建用户 (重点)
grant all on db1.* to owen@localhost identified by 'owen'; # (创建用户)设置权限 撤销权限
revoke 权限名 on 数据库名.表名 from 用户名@主机名;
revoke delete on db1.* from owen@localhost; 修改密码
set password for 用户名@主机名 = password('新密码');
set password for owen@localhost = password(''); 删除用户
drop user 用户名@主机名;
库增删该查,表增删该查,记录增删该查,表与表关系(多对多,多对一,一对一),mysql用户管理的更多相关文章
- 【MM系列】SAP 簇表 A017 物料信息记录 (指定工厂) 包含的透明表
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 簇表 A017 物料信息记录 ...
- MySQL 用户管理——权限表
权限表 权限表存放在mysql数据库中 user表结构 用户列:Host.User.Password 权限列:*priv 资源控制列:max* 安全列:其余 db表 存储了用户对某个数据库的操作权 ...
- MySQL用户、库、表(单/多)操作
用户及权限操作: 管理员登录:mysql -uroot -p 用户设置密码:set password=password(密码); 查看数据库所有用户:select * from mysql.user; ...
- EF里单个实体的增查改删以及主从表关联数据的各种增删 改查
本文目录 EF对单个实体的增查改删 增加单个实体 查询单个实体 修改单个实体 删除单个实体 EF里主从表关联数据的各种增删改查 增加(增加从表数据.增加主从表数据) 查询(根据主表找从表数据.根据从表 ...
- mysql数据库、表、字段、记录:增、删、改、查
/* 结构:数据库.表.字段.记录 操作:增删改查 */ -- 1.数据库:增删改查 create datebase if not exists jkxy; drop database if exis ...
- Django中ORM表的创建以及基本增删改查
Django作为重量级的Python web框架,在做项目时肯定少不了与数据库打交道,编程人员对数据库的语法简单的还行,但过多的数据库语句不是编程人员的重点对象.因此用ORM来操作数据库相当快捷.今天 ...
- 第二百七十七节,MySQL数据库-数据表、以及列的增删改查
MySQL数据库-数据表.以及列的增删改查 1.创建一个表 CREATE(创建) TABLE(表) ENGINE(引擎) ENGINE=INNODB(引擎)还有很多类引擎,这里只是简单的提一下INNO ...
- Django框架表关系外键-多对多外键(增删改查)-正反向的概率-多表查询(子查询与联表查询)
目录 一:表关系外键 1.提前创建表关系 2.目前只剩 书籍表和 书籍作者表没创建信息. 3.增 4.删 5.修改 二:多对多外键增删改查 1.给书籍绑定作者 2.删 3.修改 4.清空 三:正反向的 ...
- 【hbase】——Java操作Hbase进行建表、删表以及对数据进行增删改查,条件查询
1.搭建环境 新建JAVA项目,添加的包有: 有关Hadoop的hadoop-core-0.20.204.0.jar 有关Hbase的hbase-0.90.4.jar.hbase-0.90.4-tes ...
随机推荐
- 在Visual Studio 2017上配置并使用OpenGL
在Visual Studio 2017上配置并使用OpenGL 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 首先在Windows下安装Visual ...
- Python操作MySQL:pymysql模块
连接MySQL有两个模块:mysqldb和pymysql,第一个在Python3.x上不能用,所以我们学pymysql import pymysql # 创建连接 conn = pymysql.con ...
- 修改json对象的每一个值
function fun1(obj){ var names={}; /*for in 可以用于数组或者对象*/ for(var name in obj){ names[name] = obj[name ...
- vue2.0阻止事件冒泡
<!--picker弹窗--><transition name="fade"> <div class="picker_wrap" ...
- springboot中,页面访问不到静态资源
例一,静态资源放在默认的目录,如:resources/static或resources/templates 访问静态资源的时候,路径不应带上默认目录,因为springboot默认从这些目录下开始加载, ...
- Linux内存管理 (4)分配物理页面
专题:Linux内存管理专题 关键词:分配掩码.伙伴系统.水位(watermark).空闲伙伴块合并. 我们知道Linux内存管理是以页为单位进行的,对内存的管理是通过伙伴系统进行. 从Linux内存 ...
- 迷茫<第二篇:回到老家湖南长沙>
2014年8月初,我买了回老家的火车票,当时没有买到坐票,卧铺贵了买不起,所以我就选择了站票,准备站回老家.我现在还记得我当时买的是T1列火车,北京西站到长沙火车站,全程16个小时.当时我就在火车上站 ...
- 小小知识点(六)——算法中的P问题、NP问题、NP完全问题和NP难问题
转自CSDN默一鸣 https://blog.csdn.net/yimingsilence/article/details/80004032 在讨论算法的时候,常常会说到这个问题的求解是个P类问题,或 ...
- JEECG 3.8宅男优化版本发布
1024程序员节宅男节日快乐 -- JAVA快速开发平台,JEECG 3.8宅男优化版本发布 - JEECG开源社区 - CSDN博客https://blog.csdn.net/zhangdaisco ...
- Python中的正则表达式教程
本文http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 正则表达式经常被用到,而自己总是记不全,转载一份完整的以备不时之需. 1. ...