库增删该查,表增删该查,记录增删该查,表与表关系(多对多,多对一,一对一),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 ...
随机推荐
- 001_ jQuery的表格插件dataTable详解
一. 1.启用id为"datatable1"标签的html的表格jQuery库 $("#datatable1").dataTable( ) Reference: ...
- ESP8266产品ID
ESP.getChipId() https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/Chip ...
- eclipse下将maven项目打包为jar(1.不带第三方jar,2.带第三方jar)
由于项目需要讲maven项目打包为jar包,由于之前没类似经验,百度找例子走了不少弯路,这边随手记录下,网上说的 开发工具:eclipse jar包管理:maven 一般打包出来的jar包分为两种 一 ...
- .net core EF的简单使用
1.在mysql中新建一个表 2.在控制台中装个EF包 Install-Package Pomelo.EntityFrameworkCore.MySql 3.新建一个Person类 4.创建DbCo ...
- C# FileSystemWatcher 并发
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- [第二届构建之法论坛] 预培训文档(Java版)
本博客是第二届构建之法论坛暨软件工程培训活动预培训文档中[适用于结对编程部分的Java版本],需要实验者有一部分Java基础. 目录 Part0.背景 Part1.配置环境 配置JDK Linux 平 ...
- BugPhobia开发篇章:Beta阶段第X次Scrum Meeting
0x01 :Scrum Meeting基本摘要 Beta阶段第十次Scrum Meeting 敏捷开发起始时间 2015/12/29 00:00 A.M. 敏捷开发终止时间 2016/01/01 23 ...
- JAVA获取计算机CPU、硬盘、主板、网络等信息
通过使用第三方开源jar包sigar.jar我们可以获得本地的信息 1.下载sigar.jar sigar官方主页 sigar-1.6.4.zip 2.按照主页上的说明解压包后将相应的文件copy到j ...
- Spring 使用AOP——xml配置
目录 AOP介绍 Spring进行2种实现AOP的方式 导入jar包 基于schema-based方式实现AOP 创建前置通知 创建后置通知 修改Spring配置文件 基于schema-based方式 ...
- CodeSmith如何生成实体类 ,完善版
<%-- Name: Database Table Properties Author: Paul Welter Description: Create a list of properties ...