博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库操作(一)
阅读量:6984 次
发布时间:2019-06-27

本文共 6525 字,大约阅读时间需要 21 分钟。

显示数据库

show databases;

  默认数据库:

  mysql - 用户权限相关数据
  test - 用于用户测试数据
  information_schema - MySQL本身架构相关数据

创建数据库

create database 数据库名称 default charset utf8;

使用数据库

use db_name; #显示当前使用的数据库中所有的表show tables; #查看表的创建过程 show create table 表名 show create table 表名 \G

 删除数据库

drop db_name;

转储SQL文件

#备份:数据表结构+数据mysqldump -u root  数据库名 > 文件路径  -p#备份:  数据表结构mysqldump -u root  -d 数据库名 > 文件路径  -p #导入数据库 mysqdump -u root 数据库名 < 文件路径  -p

用户管理

#创建用户create user '用户名'@'IP地址' identified by '密码';#删除用户drop user '用户名'@'IP地址';#修改用户rename user '用户名'@'IP地址';to '新用户'@'IP地址'’#修改密码set password for '用户名'@'IP地址'=password('新密码');

授权管理

#查看权限show grants for '用户'@'地址';#授权grant 权限 on 数据库.表 to 用户'@'地址';#取消权限revoke 权限 on 数据库.表 from 用户'@'地址'

对于权限:

all privileges   #除grant外的所有权限select  #仅查权限select,insert   #查和插入的权限

对于数据库 --对于目标数据库和内部其他;

数据库.*    #数据库中的所有数据库.表    #数据库中的具体的一张表

对于用户和IP:

用户名@IP地址      #用户只能在该IO下访问用户名@192.168.1.%    #用户只能在该IP段下访问 (通配符%表示任意)用户名@%;        #用户可以在任意IP下访问

示例:

grant all privileges on db1.tb1 to  '用户名'@'IP地址'grant select on db1.* to  '用户名'@'IP地址'grant select  insert  on *.*  to   '用户名'@'IP地址'revoke  select  on db1.tb1  from  '用户名'@'IP地址'
View Code

数据类型

整形:

一.整形 1.tinyint2.int 3.bigint 二.浮点型
4.float 5.double 6.decimal    #精确的小数

字符串:

char(10)     #速度快varchar(10)  #节省空间 text     #主要用于存储大的字符串
PS:  创建数据表时,定长的列往前面放!      上传文件时,文件存硬盘,文件的路径存数据库中.

时间类型:

datetime  #年-月-日-时-分-秒
enum --枚举型create table shirts(     name varchar(40),     size    enum('x-small','small','medium','large','large','x-large'));insert into shirts(name,size) values ('xxx','large')#插入数据时,只能从enum中选择一个set --集合类型create table   myset(    name  varchar(40),    col   set('a','b','c','d')   );insert into myset(name,col) values ('a',b')#插入数据时,可以是set集合中的元素的任一组合(只能是集合中的元素)

创建表

create table 表名(     nid int signed not null auto_increment primary key,           name  char(10))engine=innodb default charset=utf8;  #innodb  --支持事务,原子型操作#auto_increment  --表示:自增#primary key   --表示:约束(不能重复且不能为空);加速查找#not null   --是否为空 #signed  --有无符号
自增步长:
MYSQL:自增步长1、对于自增列,必须是索引(含主键)2、对于自增可以设置步长和起始值基于会话级别:show session variables like 'auto_inc%';        查看全局遍变量set session auto_increment_increment=2;     设置会话步长set session auto_increment_offset=10;          设置起始值基于全局级别:show global  variables like 'auto_inc%';set global auto_increment_increment=2;set global auto_increment_offset=10;
View Code

主键

#一个表只能有一个主键,一个主键可以由多列组成!
#主键的创建方法一   --  一列为主键create table(    nid int not null auto_increment  primary key,    pid int  not null,    num  int) engine=innodb default charset=utf-8;#主键的创建方法二   --  多列为主键create table(    nid int not null auto_increment ,    pid int  default null,    num  int  default null,    primary key  (nid,pid)) engine=innodb default charset=utf-8;

唯一索引

唯一索引create table t1(    id int ...,    name int ,    num  int,    unique  uq1 (name,num))ps :唯一!约束不能重复(可以为空,主键不能为空)加速查找

外键

#一列外键的创建方法

create table t2(

  id int auto_increment primary key,

  name char(10),

  id1 int,
  constraint fk_t2_t1 foreign key(id1) references t1(nid)

create table t1(

  nid int not null auto_increment primary key,
  pid int not null,
  num int
) engine=innodb default charset=utf-8;

#多列为外键的创建方法 create table t2(  id int auto_increment primary key,  name char(10),  id1 int,  constraint fk_t2_t1 foreign key(id1,id2) references t1(nid,pid)create table t1(    nid int not null auto_increment,    pid int  not null,    num  int    primary key  (nid,pid)) engine=innodb default charset=utf-8; PS:以多列为外键时,被连接的表的主键需为多列组成的!

示例:外键的变种

#一对一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_ad_use foreign key (user_id)references userinfo1(id)    )engine=innodb default charset=utf8;#多对多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;
View Code

删除表

drop table   表名;

清空表

delete from 表名;         #随着原来的数据自增 truncate table  表名;    #重新开始自增

修改表

#添加列alter table 表名  add 列名 类型;#删除列alter table 表名   drop  column  列名;#修改列alter table 表名   change  原列名  新列名  类型;#添加主键alter table  表名   add primary key  列名;#删除主键alter table  表名   drop primary key; #修改自增 alter table 表名 auto_increment=20

表内容操作

 
增:insert into 表(列名,列名...) values (值,值...)insert into 表(列名,列名...) values (值,值...), (值,值...)insert into 表(列名,列名...) select (列名,列名...) from 表删:delete from 表;delete  from 表  where  id=1 and  name='alex';改:update 表 set  name='alex' where  id>1查:select * from 表; select * from 表 where id>1; select nid,name,gender as gg from 表 where id>1; 其他: a.条件: select * from 表 where id>1 and name !='alex' and num=12; select * from 表 where id between 5 and 16; select * from 表 where id in/not in (11,22,33) select * from 表 where id in (select nid from 表) b.通配符 select * from 表 where name like 'ale%'/'ale_' #ale开头的所有(多个字符串)/ale开头的所有(一个字符) c.限制 select * from 表 limit 5; #前五行 select * from 表 limit 4,5 #从第4行开始的5行 select * from 表 limit 5 offset 4 #从第4行开始的5行 d.排序 select * from 表 order by 列 asc/dasc #根据'列'从小到大排序/从大到小排序 select * from 表 order by 列1 desc,列2 asc #根据'列1'从大到小排序,如果相同,按照列2从小到大排序 e.分组 select num from 表 group by num select num,nid from 表 group by num,nid select num,nid from 表 where id>10 group by num,nid order nid desc select count(id),id from 表 group by id; #计数分组 select count(id),id from 表 group by id having count(id)>1; #如果对于聚合函数进行二次筛选时,必须使用having关键字  f.连表  #无对应关系则不显示 select A.num,A.name,B.name from A,B where A.nid=B.nid #无对应关系则不显示 select A.num,A.name,B.name from A innoer join B on A.nid=B.nid #A表所有显示,如B中无对应关系,则值为NULL
 select  A.num,A.name,B.name
 from A left join B 
 on A.nid=B.nid
#B表所有显示,如A中无对应关系,则值为NULL 
  select A.num,A.name,B.name 
  from A reight join B 
  on A.nid=B.nid
组合  处理重合
select nickname 
from A 
union 
select name 
from B 
组合 不处理重合 
select nickname 
from A 
union all 
select name 
from B
 

转载于:https://www.cnblogs.com/zhangj0918/p/10513583.html

你可能感兴趣的文章
【poi xlsx报错】使用POI创建xlsx无法打开
查看>>
UNIX环境高级编程笔记之文件I/O
查看>>
DIV+CSS规范命名
查看>>
我的2013 Q.E.D
查看>>
2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】...
查看>>
4.5. Rspamd
查看>>
ArcMap中的名称冲突问题
查看>>
(转) 一张图解AlphaGo原理及弱点
查看>>
美联邦调查局 FBI 网站被黑,数千特工信息泄露
查看>>
掉电引起的ORA-1172错误解决过程(二)
查看>>
在网站建设过程中主要在哪几个方面为后期的网站优打好根基?
查看>>
【MOS】RAC 环境中最常见的 5 个数据库和/或实例性能问题 (文档 ID 1602076.1)
查看>>
新年图书整理和相关的产品
查看>>
Struts2的核心文件
查看>>
Spring Boot集成Jasypt安全框架
查看>>
GIS基础软件及操作(十)
查看>>
HDOJ 2041 超级楼梯
查看>>
1108File Space Bitmap Block损坏能修复吗2
查看>>
遭遇DBD::mysql::dr::imp_data_size unexpectedly
查看>>
人人都会设计模式:03-策略模式--Strategy
查看>>