zoukankan      html  css  js  c++  java
  • mysql数据类型 完整性约束 054

    创建用户和授权用户权限:

    # 1 .创建用户:
    # 指定ip 192.168.15.109的fgf用户登录
    create user 'fgf'@'192.168.15.109' identified by '12';
    # 指定ip 192.168.15.开头的fgf用户登录
    create user 'fgf'@'192.168.15.%' identified by '123';
    # 指定任何ip的fgf用户登录
    create user 'fgf'@'%' identified by '123';
    # 2 .删除用户
    drop user '用户名'@'IP地址';
    # 3 .修改用户
    rename user '用户名'@'IP地址' to '新用户名'@'IP地址';
    # 4 .修改密码
    set password for '用户名' @'IP地址'=password('新密码');
    
    对当前用户的授权管理
    查看权限
    show grants for '用户名'@'IP地址'
    # 授权 fgf 用户仅仅对 db1.t1文件有查询 插入 和 更新的操作
    grant select ,insert ,update on db1 to 'fgf'@'%';
    # 表示所有的权限 , 除了 grant 这个命令 这个命令是root才有的,fgf用户对db1下的t1文件有任意操作权限;
    grant all privileges on db1.t1 to 'mjj'@'%';
    #fgf用户对db1数据库中的文件执行任何操作
    grant all privileges on db1.* to 'fgf'@'%';
    # fgf 用户对所有数据库文件有任何操作 
    grant all previleges on *.* to 'fgf'@'%';

    复制表

    #即复制表结构 又复制记录
    create table t2 select * from db1.t1;
    
    # 只复制表结构,不复制记录
    create table t2 select * from db1.t1 where 1>3;
    create table t2 like db1.t1;

    数据类型: 整型 默认有符号 

      数据类型 无符号(unsigned) 和 有符号 用 0 填充 zerofill

      约束的作用 : 保证数据的完整性 和一致性

        tinyint[-128~127]小整数 两边区域都能取到

        int 整数

        bigint 极大整数

    # 加了unsigned 表示从0开始 不再从负数开始
    create table t1(id int(4) unsigned,name char(20));

    浮点型 

      float 单精度 随着小数位数的增多 会变得越来越不准确

        float[(M,D)] [UNSIGNED] [ZEROFILL]

        参数解释 : 单精度浮点数(非准确小数值) , M是全长 ,D是小数点后个数 M最大值为255,D最大值为30

      double 双精度 随着小数的增加 也会越来越不准确 但是要比float 准确点

        DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL] 字母的含义与float 一样.

      decimal 小数 精准的小数

        decimal[(m[,d])] [unsigned] [zerofill]

          #参数解释:准确的小数值,M是整数部分总个数(负号不算),D是小数点后个数。 M最大值为65,D最大值为30。

            #精确度: **** 随着小数的增多,精度始终准确 ****

                 对于精确数值计算时需要用此类型

                 ecaimal能够存储精确值的原因在于其内部按照字符串存储。

    日期类型: DATE TIME DATETIME TIMESTAMP YEAR

        作用 存储用户注册时间 文章发布时间 员工入职时间 出生时间 过期时间等

        YEAR 年份 (1901~2155)

        DATE 年月日 

        TIME 时分秒 

        now() sql 语言中自带的内容函 : 获取当前的事时间(根据数据类型)

        create table t10(born_year year,intClass datetime);

    字符类型 

        char 定长,简单粗暴 , 浪费空间 但是存取速度快 

        varchar 变长 精准 节省空间 但是存取速度慢

    length():查看字节数
    char_length():查看字符数

      枚举和集合

     create table consumer(
         id int,
         name varchar(50),
         sex enum('male','female','other') default 'male',
         level enum('vip1','vip2','vip3','vip4'),#在指定范围内,多选一
         fav set('play','music','read','study') #在指定范围内,多选多
        );

        注意:在sql中使用tinyint(1)来表示boolean类型

    完整性约束

      not null 与 default

        如果单独设置not null 不能插入空值

        如果既设置了not null 又指定default 可以插入空值 会执行default

      unique key

        单列唯一

    create table t4(
        id int not null,
        name char(20) unique
    );
    
    create table t4(
        id int not null,
        name char(20),
        unique(name)
    );
    insert into t4(id,name) values(1,'alex');
    insert into t4(id,name) values(1,'wusir');

        多列唯一   只要有一列相同 就不能插入(类似于且)

    create table t5(
        id int,
        name char(20),
        unique(id),
        unique(name)
    );

        联合唯一   全部相同时 不能插入(类似于或)

    create table t6(
        id int,
        name char(20),
        unique(id,name) 
    );

        应用场景 选课系统 一个学生可以选择多个课程 一个课程可以被多个学生选择

    primary key

      化学反应 : not null + unique   

      单列主键 不能为空 并且是唯一

    # primary key 索引(针对于大量数据) 查询速度要快
    create table t7(
        id int primary key,
        name varchar(10) unique
    );
    
    create table t8(
        id int not null unique,
        name varchar(10) unique
    );

      联合主键  

    create table t9(    
        id int,    
        name varchar(10),
        primary key(id,name)
    );

    auto_increment  约束 : 约束的字段为自动增长 约束的字段必须同时被key约束

    create table student(
        id int primary key auto_increment,
        name varchar(20) not null,
        sex enum('male','female') default 'male', 
        ip varchar(20) unique
    );
    
    insert into student(name,sex,ip) values 
    ('alex','female','127.0.0.5'),
    ('wusir','male','173.45.32.1');

      清空表 区分delete 和 truncate的差异:

        delete from t1 : 如果有自增的id 信息鞥的数据 仍然是以删除前的最后一样作为起始

        truncate table t1: 适用于删除数据量比较大 , 删除速度也比上一条快 且 直接从零开始.

    foreign key

      外键  即关联两个表格 

    # 创建主表
    create table dep(
        # 主表id 设置成可以自动增长
        id int primary key auto_increment,
        # 设置部门名称为单列唯一
        name char(10) unique,
        # 部门描述 不能为空
        dep_desc varchar(50) not null
    );
    
    # 创建从表
    create table emp(
        eid int primary key auto_increment,
        name char(10) not null,
        age int not null,
        dep_id int,
        constraint fk_dep foreign key(dep_id) references dep(id)
        on delete cascade
        on update cascade,
    );
    
    
    
    create table emp(
        eid int primary key auto_increment,
        name char(10) not null,
        age int not null,
        dep_id int,
        constraint fk_dep foreign key(dep_id) references dep(id) 
        on delete cascade 
        on update cascade,
    );
    
    insert into dep(name,dep_desc) values
        ('董事部','管理公司部门'),
        ('公关部','公关管理部门'),
        ('IT部门','IT管理部门'),
        ('财务部','财务管理部门');
        
    insert into emp(name,age,dep_id) values
        ('alex',18,1),
        ('wusir',30,2),
        ('吴老板',20,3),
        ('马老板',18,4),
        ('邱老板',20,2),
        ('女神',16,3);
  • 相关阅读:
    C语言 realloc为什么要有返回值,realloc返回值具体解释/(解决随意长度字符串输入问题)。
    opencv中的vs框架中的Blob Tracking Tests的中文注释。
    Java实现 蓝桥杯VIP 算法提高 棋盘多项式
    Java实现 蓝桥杯VIP 算法提高 棋盘多项式
    Java实现 蓝桥杯VIP 算法提高 棋盘多项式
    Java实现 蓝桥杯VIP 算法提高 棋盘多项式
    Java实现 蓝桥杯VIP 算法提高 分苹果
    Java实现 蓝桥杯VIP 算法提高 分苹果
    Java实现 蓝桥杯VIP 算法提高 分苹果
    Java实现 蓝桥杯VIP 算法提高 分苹果
  • 原文地址:https://www.cnblogs.com/f-g-f/p/9998820.html
Copyright © 2011-2022 走看看