二、mysql的基本操作

本文详细介绍了MySQL数据库中如何设置和修改root密码,处理忘记密码的情况,以及创建、删除和管理用户账号。同时,讲解了如何为用户分配特定权限,包括查看、创建、删除和修改表的权限。此外,还涵盖了数据库和数据表的基本操作,如创建、删除数据库和数据表,以及数据类型的选择。最后,讨论了数据行的增删改查操作以及外键的概念和管理。
摘要由CSDN通过智能技术生成

一、关于mysql账号设置

1. 设置和修改root密码

在windows系统中模块默认 root 账户是没有密码的,如果想要为账户设定密码,可以在利用root账户登录成功之后,执行:
在这里插入图片描述

2. 忘记root密码

如果你忘记了MySQL账户的密码。

  • 修改配置文件,在 [mysqld] 节点下添加 skip-grant-tables=1

    [mysqld]
    ...
    skip-grant-tables=1
    ...
    
  • 重启MySQL,再次登录时,不需要密码直接可以进去了

    • windows重启

      net stop mysql57
      net start mysql
      

    重启后,无序密码就可以进入。

    >>> mysql -u root -p
    
  • 进入数据库后执行修改密码命令

    use mysql;
    update user set authentication_string = password('新密码'),password_last_changed=now() where user='root';
    
  • 退出并再次修改配置文件,删除 [mysqld] 节点下的 skip-grant-tables=1

    [mysqld]
    ...
    # skip-grant-tables=1
    ...
    
  • 再次重启,以后就可以使用新密码登录了。

3.账号授权
  • 如果有多个程序的数据库在同一个mysql中, 如果程序都有用root账户就存在风险

  • 解决办法

    在mysql中支持创建账户, 并给账户分配权限

3.1用户管理

在mysql的默认数据库 mysql中的user表中存储着所有的账户信息(含账户、权限等)

  • 查询账号

    select user, authentication_string, host from mysql.user;

3.2创建和删除用户
  • 创建用户

    create user ‘用户名’ identified by ‘密码’;

  • 删除账户

    drop user ‘用户名’;

  • 修改用户名

    rename user ‘用户名’ to ‘新用户名’;

  • 修改m密码

    set password for 'test01' = Password('新密码');
    
3.3具体权限

grant 权限 on 数据库.表 ‘用户名’;

  • 具体权限

    -- 所有权限
    grant all privileges on *.* to 'test01';
    -- db01所有权限
    grant all privileges on db01.* to 'test01';
    -- 给与某个库中的某个表所有权限
    grant all privileges on db01.l3 to 'test01';
    -- 给与某个库中的某个表查询权限
    grant select on db01.l3 to 'test01';
    -- 给与某个库所有表查询和插入权限
    grant select, insert on db01.* to 'test01';
    
  • 权限设置详情

    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       由复制从属使用
    
3.4 查看授权
show grants for '用户名';
3.5 取消授权
revoke all privileges on db01.* from '用户名';

二、数据库管理

  • 查看所有数据库

    -- 创建默认数据集
    show databases;
    -- 创建符合中文的数据库
    create database db01 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
    
  • 删除数据库

    drop databse 库名
    
  • 进入数据库

    use  数据库名
    
  • 退出

    exit;
    

三、数据表管理

3.1表的基本操作
  • 查看表

    show tables;
    
  • 创建表

    -- 语法
    create table 表名(
        列名 类型,
        列名 类型
    )default charset=utf8;
    
    -- 示例一
    create table tb2(
    	id int,
        name varchar(16) not null,   -- 不允许为空
        email varchar(32) null,      -- 允许为空(默认)
        age int
    )default charset=utf8;
    
    -- 示例二
    create table tb3(
    	id int,
        name varchar(16) not null,   -- 不允许为空
        email varchar(32) null,      -- 允许为空(默认)
        age int default 3            -- 插入数据时,如果不给age列设置值,默认值:3
    )default charset=utf8;
    
    -- 示例三, 
    create table tb4(
    	id int primary key,			 -- 主键(不允许为空、不能重复)
        name varchar(16) not null,   -- 不允许为空
        email varchar(32) null,      -- 允许为空(默认)
        age int default 3            -- 插入数据时,如果不给age列设置值,默认值:3
    )default charset=utf8;
    
    -- 主键一般用于表示当前这条数据的ID编号, 需要我们自己来维护一个重复不变的值, 比较繁琐,'
    -- 所以在数据库中一般会将主键和"自增"结合使用
    -- 示例
    create table l2(
        id int not null auto_increment primary key,
        name varchar(16),
        email varchar(32),
        age int default 3
    )default charset=utf8;
    
  • 添加列

    alter table 表名 add 列名 类型;
    alter table 表名 add 列名 类型 DEFAULT 默认值;
    alter table 表名 add 列名 类型 not null default 默认值;
    alter table 表名 add 列名 类型 not null primary key auto_increment;
    
  • 删除列

    alter table 表名 drop column 列名;
    
  • 修改列类型

    alter table 表明 modify column 列名 类型;
    
  • 修改列名加名称

    alter table 表名 change 原列名 新列名 新类型;
    
    alter table  tb change id nid int not null;
    alter table  tb change id id int not null default 5;
    alter table  tb change id id int not null primary key auto_increment;
    
    alter table  tb change id id int; -- 允许为空,删除默认值,删除自增。
    
  • 修改列默认值

    alter table l1 alter age set default 5;
    
  • 删除列默认值

    alter table l1  alter age drop default;
    
  • 添加主键

    alter table l1 add primary key(id);
    
  • 删除主键

    -- 注意: 主键有自增需要先使用change修改在删除主键
    alter table 表名 drop primary key;
    
3.2表数据的基本类型
  • int类型

    int				表示有符号,取值范围:-21474836482147483647
    int unsigned	表示无符号,取值范围:04294967295
    int(5)zerofill	仅用于显示,当不满足5位时,按照左边补0,例如:00002;满足时,正常显示。
    
  • tinyint

    有符号,取值范围:-128127.
    无符号,取值范围:0255
    
  • bigint

    有符号,取值范围:-92233720368547758089223372036854775807
    无符号,取值范围:018446744073709551615
    
  • decimal

    准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
    
    例如:
    create table L2(
    	id int not null primary key auto_increment,
    	salary decimal(8,2)
    )default charset=utf8;
    
  • FLOAT

    单精度浮点数,非准确小数值,m是数字总个数,d是小数点后个数。
    
  • char

    定长字符串,m代表字符串的长度,最多可容纳255个字符。
    定长的体现:即使内容长度小于m,也会占用m长度。例如:char(5),数据是:yes,底层也会占用5个字符;如果超出m长度限制(默认MySQL是严格模式,所以会报错)。
    注意:默认底层存储是固定的长度(不够则用空格补齐),但是查询数据时,会自动将空白去除。 如果想要保留空白,在sql-mode中加入 PAD_CHAR_TO_FULL_LENGTH 即可。
    查看模式sql-mode,执行命令:show variables  like 'sql_mode';
    
  • varchar(m)

    变长字符串,m代表字符串的长度,最多可容纳65535个字节。
    
    变长的体现:内容小于m时,会按照真实数据长度存储;如果超出m长度限制((默认MySQL是严格模式,所以会报错)。
        如果在配置文件中加入如下配置,
            sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
        保存并重启,此时MySQL则是非严格模式,此时超过长度则自动截断(不报错)。
    
  • text

    text数据类型用于保存变长的大字符串,可以组多到65535 (2**161)个字符。
    
    一般情况下,长文本会用text类型。例如:文章、新闻等。
    
  • datetime

    YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59
  • timestamp

    YYYY-MM-DD HH:MM:SS(1970-01-01 00:00:00/2037年)
    对于TIMESTAMP,它把客户端插入的时间从当前时区转化为UTC(世界标准时间)进行存储,查询时,将其又转化为客户端当前时区进行返回。
    
    对于DATETIME,不做任何改变,原样输入和输出。
    
  • date

    YYYY-MM-DD(1000-01-01/9999-12-31
  • time

    HH:MM:SS('-838:59:59'/'838:59:59'

四、数据行

当速记和数据表创建完成之后, 就要需要对数据表的内容进行:增删改查

  • 新增一条数据

    insert into 表名 (列名,列名,列名) values(对应列的值,对应列的值,对应列的值);
    
  • 删除数据

    delete from 表明 where 条件';
    delete from l1 where id='4';
    
  • 修改数据

    update 表名 set 列名=; -- 不安全的
    update 表名 set 列名=where 条件;
    
  • 查询数据

    select * from 表名;
    select 列名,列名,列名 from 表名;
    select 列名,列名 as 别名,列名 from 表名;
    select * from 表名 where 条件;
    

五、外键

  • 创建表时添加外键
    create table module_record(
    id int not null auto_increment primary key,
    user_id int not null,
    module_id int not null,
    constraint fk_user_id foreign key module_record(user_id) references info(id),
    constraint fk_module_id foreign key module_record(module_id) references module(id)
    )default charset=utf8;

  • 新增外键
    alter table db01.info
    add constraint fk_depart_id
    foreign key info(depart_id)
    references depart(id);

  • 删除外键
    alter table db01.info drop foreign key fk_depart_id;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值