首页 > 数据库技术 > 详细

MYSQL库,表,记录的基本操作

时间:2017-12-08 21:43:45      阅读:316      评论:0      收藏:0      [点我收藏+]

数据库操作

1、显示数据库

show databases;

默认数据库:

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

2、创建数据库

# utf-8
create database 数据库名称 default charset utf8 collate utf8_general_ci;
  
# gbk
create database 数据库名称 default character set gbk collate gbk_chinese_ci;

 3、使用数据库

use db_name;

显示当前使用的数据库中所有表:show tables;

4、删除数据库

drop database db_name;

 5、用户管理

技术分享图片
创建用户
    create user 用户名@IP地址 identified by 密码;
    create user xyp@192.168.1.1 identified by 123;    #账户名xyp,ip地址192.168.1.1,密码123可以使用该用户
    create user xyp@192.168.1.% identified by 123;    # %代表任意
    create user xyp@% identified by 123;
删除用户
    drop user 用户名@IP地址;
修改用户
    rename user 用户名@IP地址; to 新用户名@IP地址;;
修改密码
    set password for 用户名@IP地址 = Password(新密码)
   
PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)
View Code

6、授权管理

show grants for ‘用户‘@‘IP地址‘                  -- 查看权限
grant  权限 on 数据库.表 to   ‘用户‘@‘IP地址‘      -- 授权
revoke 权限 on 数据库.表 from ‘用户‘@‘IP地址‘      -- 取消权限
技术分享图片
all privileges  除grant外的所有权限
            select          仅查权限
            select,insert   查和插入权限
            ...
            usage                   无访问权限
            alter                   使用alter table
            alter routine           使用alter procedure和drop procedure
            create                  使用create table
            create routine          使用create procedure
            create temporary tables 使用create temporary tables
            create user             使用create user、drop user、rename user和revoke  all privileges
            create view             使用create view
            delete                  使用delete
            drop                    使用drop table
            execute                 使用call和存储过程
            file                    使用select into outfile 和 load data infile
            grant option            使用grant 和 revoke
            index                   使用index
            insert                  使用insert
            lock tables             使用lock table
            process                 使用show full processlist
            select                  使用select
            show databases          使用show databases
            show view               使用show view
            update                  使用update
            reload                  使用flush
            shutdown                使用mysqladmin shutdown(关闭MySQL)
            super                   ????使用change master、kill、logs、purge、master和set global。还允许mysqladmin????????调试登陆
            replication client      服务器位置的访问
            replication slave       由复制从属使用
对于权限
技术分享图片
对于目标数据库以及内部其他:
            数据库名.*           数据库中的所有
            数据库名.表          指定数据库中的某张表
            数据库名.存储过程     指定数据库中的存储过程
            *.*                所有数据库
对于数据库
技术分享图片
用户名@IP地址         用户只能在改IP下才能访问
            用户名@192.168.1.%   用户只能在改IP段下才能访问(通配符%表示任意)
            用户名@%             用户可以再任意IP下访问(默认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‘

 特殊的:

flush privileges,将数据读取到内存中,从而立即生效。
技术分享图片
# 启动免授权服务端
mysqld --skip-grant-tables

# 客户端
mysql -u root -p

# 修改用户名密码
update mysql.user set authentication_string=password(666) where user=root;
flush privileges;
忘记密码
技术分享图片
#外键绑定两个主键
create table db1(
    cid int not null auto_increment,
    id1 int not null,
    id2 int,
    primary key(cid,id1)
    )engine=innodb default charset=utf8;


create table db2(
    sid int not null auto_increment primary key,
    ic1 int,
    ic2 int,
    constraint db2_db1 foreign key(ic1,ic2) references db1(cid,id1)
    )engine=innodb default charset=utf8;



#外键 唯一 一对一演示
create table user_info(
    uid int not null auto_increment primary key,
    name varchar(32) not null,
    usertype int not null
    )engine=innodb default charset=utf8;


create table admain_info(
    id int not null auto_increment primary key,
    user_id int not null,
    unique admin_user (user_id),
    constraint admin_user foreign key(user_id) references user_info(uid)
    )engine=innodb default charset=utf8;



insert into user_info(name,usertype) values("alex",1),("egon",2),("tom",3);

insert into admain_info(user_id) values(1),(2),(3);




#外键 唯一 一对多演示

create table user(
    uid int not null auto_increment primary key,
    name varchar(32) not null,
    gender ENUM("","") not null
    )engine=innodb default charset=utf8;


create table host(
    hid int not null auto_increment primary key,
    name varchar(32) not null
    )engine=innodb default charset=utf8;


create table user_host(
    id int not null auto_increment primary key,
    uid int not null,
    hid int not null,
    unique uid_hid (uid,hid),
    constraint user_host_user foreign key(uid) references user(uid),
    constraint user_host_host foreign key(hid) references host(hid)
    )engine=innodb default charset=utf8;


insert into user(name,gender) values("alex",""),("egon",""),("tom","");

insert into host(name) values("host1"),("host2"),("host3");


insert into user_host(uid,hid) values(1,1),(1,2),(1,3);


insert into user_host(uid,hid) values(2,1),(2,2),(2,3);

insert into user_host(uid,hid) values(3,1),(3,2),(3,3);




#外键 一对多演示
create table user_info(
    uid int not null auto_increment primary key,
    name varchar(32) not null,
    usertype int not null
    )engine=innodb default charset=utf8;


create table admin_info(
    id int not null auto_increment primary key,
    user_id int not null,
    constraint admin_user foreign key(user_id) references user_info(uid)
    )engine=innodb default charset=utf8;



insert into user_info(name,usertype) values("alex",1),("egon",2),("tom",3);

insert into admin_info(user_id) values(1),(2),(3);
外键相关

数据表基本

1、创建表

create table t1(id int,name char(10)) default charset=utf8;
create table t1(id int,name char(10))engine=innodb default charset=utf8;
create table t3(id int auto_increment primary key,name char(10))engine=innodb default charset=utf8;  ***** #推荐使用
          
create table t1(
    列名 类型 null,
    列名 类型 not null,
    列名 类型 not null auto_increment primary key,
    id int,
    name char(10)
)engine=innodb default charset=utf8;
 
# innodb 支持事务,原子性操作。即操作中断后不会丢失数据,会返回中断前数据。
# myisam    mysql默认myisam,数据会丢失。所以一般设置模式为innodb
             
auto_increment 表示:自增1。写入内容为空时,默认从1,2,3...往下填充写入表格中。
primary key:  表示约束(不能重复且不能为空); 加速查找
not null: 不为空

desc class; 查看表class信息

技术分享图片

 show create table class;  查看表class创建信息

技术分享图片

show create table class \G;  换个方向,竖向查看表class创建信息

技术分享图片

alter table class AUTO_INCREMENT=20;   设置自增起始为20

技术分享图片

2、删除表

drop table 表名

 3、清空表

delete from t1;    #当创建表时设置auto_increment primary key自增时,表清空后自增不会从1开始,从之前删掉的序号后开始自增

delete from t1 where ID=5;   从T1表中删除ID为5的记录

truncate table t1;    #当创建表时设置auto_increment primary key自增时,表清空后自增从1开始

 4、修改表

添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名
修改列:
        alter table 表名 modify column 列名 类型;  -- 类型
        alter table 表名 change 原列名 新列名 类型; -- 列名,类型
   
添加主键:
        alter table 表名 add primary key(列名);
删除主键:
        alter table 表名 drop primary key;
        alter table 表名  modify  列名 int, drop primary key;
   
添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键:alter table 表名 drop foreign key 外键名称
   
修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

表内容操作

1、增

insert into 表 (列名,列名...) values (值,值,值...)
insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
insert into 表1 (列名,列名...) select (列名,列名...) from 表2    #将表2中选中的列添加到表1中

 2、删

delete from 表
delete from 表 where id=1 and name=‘alex‘  #and也可为or

 3、改

update 表 set name = ‘alex‘ where id>1

 4、查

select * from 表  # *代表查看表中的全部内容
select * from 表 where id > 1  #查看表中id>1的全部内容
select nid,name,gender as gg from 表 where id > 1

 表的其他操作

技术分享图片
a、条件
    select * fromwhere id > 1 and name != alex and num = 12;
  
    select * fromwhere id between 5 and 16;
  
    select * fromwhere id in (11,22,33)
    select * fromwhere id not in (11,22,33)
    select * fromwhere id in (select nid from 表)
  
b、通配符
    select * fromwhere name like ale%  - ale开头的所有(多个字符串)
    select * fromwhere name like ale_  - ale开头的所有(一个字符)
  
c、限制
    select * from 表 limit 5;            - 前5行
    select * from 表 limit 4,5;          - 从第4行开始的5行
    select * from 表 limit 5 offset 4    - 从第4行开始的5行
  
d、排序
    select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    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 fromwhere nid > 10 group by num,nid order nid desc
    select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
  
    select num from 表 group by num having max(id) > 10
  
    特别的:group by 必须在where之后,order by之前

 聚合函数(经常作用于分组查询配合使用)
    SUM(字段)        -- 求和
    
    COUNT(字段)      -- 次数统计
    
    AVG(字段)        -- 平均值
    
    MAX(字段)        -- 最大
    
    MIN(字段)        -- 最小
  
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 inner 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表所有显示,如果B中无对应关系,则值为null
    select A.num, A.name, B.name
    from A right join B
    on A.nid = B.nid
  
g、组合
    组合,自动处理重合
    select nickname
    from A
    union
    select name
    from B
  
    组合,不处理重合
    select nickname
    from A
    union all
    select name
    from B
View Code
技术分享图片
=      -- 等于
<>     -- 不等于。注释:在 SQL 的一些版本中,该操作符可被写成 !=
>      -- 大于
<      -- 小于
>=     -- 大于等于
<=     -- 小于等于
BETWEEN-- 在某个范围内
LIKE   -- 搜索某种模式
IN     -- 指定针对某个列的多个可能值
where字句中的条件

以上都只是单表性的查询,例如模拟在实际生活中,会有一张员工表,而员工会有其归属的部门,那么相应的也会有一张部门表.在其中相应的俩者之间会有一种相应的关联,那么这里引申了外键及多表查询

MYSQL库,表,记录的基本操作

原文:http://www.cnblogs.com/bypp/p/8006942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!