zoukankan      html  css  js  c++  java
  • mysql基本数据类型和约束条件

    mysql基本数据类型

    1.创建表的完整语法
    create table 表名(字段1 类型[(宽度) 约束条件],字段2 类型[(宽度) 约束条件]);
    # 注意:1.在同一张表中,字段名不能相同;2.宽度和约束条件可选,字段名和类型是必须的;
    # 类型:使用限制字段必须以什么样的数据类型传值
    # 约束条件:约束条件是在类型之外添加一种额外的限制
    
    2.整型
    类型 大小 范围(有符号) 范围(无符号) 用途
    tinyint 1字节 -128,127 0,255 小整型
    smallint 2字节 -32768,32767 0,65535 大整数
    mediumint 3字节 大整数
    int 4字节 0,2**32-1 大整数
    bigint 8字节 极大整数
    # 整型默认都是有符号的
    测试:create table t1(x tinyint);
    insert into t1 values(128),(-129);		# 可以存储负号
    # 修改为无符号
    create table t2(x tinyint unsigned);	# 即不能存负号
    # 整型的宽度对存储数据限制不起作用
    create table t3(x tinyint(1) unsigned);
    insert into t3 values(100);
    create table t4(x tinyint(2) unsigned zerofill);
    
    # 关键字查找变量
    show variables like "%mode%";
    
    # 设置严格模式:在该模式下,如果数据超出限制,则会立即报错
    set global sql_mode='strict_trans_tables'
    
    3.浮点型
    类型 大小 范围 范围 用途
    float 4字节 单精度
    double 8字节 双精度
    decimal 不固定 小数值
    # 三种浮点型:精确度不同,float最低,
    create table t5(x float(255,30));        # 255:总长度255位	30:小数位占30位
    create table t6(x double(255,30));		 # 255:总长度255位	30:小数位占30位
    create table t7(x decimal(65,30));		 # 65:总长度65位	30:小数位占30位
    
    
    # 相同点:都能存放30位小数
    # 不同点:
    		精确的不同,其中float的精确度最低
        	float 和 double 存放的整数位比decimal多
    
    4.字符串类型
    4.1 char:定长字符串
    create table t8(x char(4));		# 严格模式下,超过4个会报错,不够用空格补全
    insert into t8 values('aaaaa');		# 报错
    
    4.2 varchar:可变长字符串
    create table t9(x varchar(4));	# 严格模式,超过4个会报错,不够按实际个数存
    insert into t9 values('aa');	# 只存两个,不会用空格不全
    
    # char类型,因为存取都是固定长度,所以不需要管存储的数据的实际长度,在读取时,速度会更快
    # varchar类型,因为存储数据都是按数据的实际长度存储,所以更节省空间。
    
    5.日期类型
    date:1999-01-27
    time:11:11:11
    datetime:1999-01-27 11:11:11		8个字节
    timestemp:							4个字节
    year:1999
    
    # 测试
    create table student(id int,
                         name char(16),
                         born_year year,
                         birth date,
                        reg_time datetime);
    insert into student values(1,'aaa','2000','2000-01-27','08:30:00',
                              '2013-11-11 11:11:11');
    
    6.枚举和列表
    enum:多选一   # 枚举
    set:多选多	   # 集合
    
    # 测试
    create table teacher(id int,
    					 name char(16),
    					 sex enum('male','female','other'));
    			
    insert into teacher values(1,'aaa','xxx');		# 会报错
    insert into teacher values(1,'aaa','male');		# 正确
    
    create table teacher(id int,
    					 name char(16),
    					 sex enum('male','female','other',
                         hobbies set('1','2','3')));
    insert into teacher values(1,'aaa','male','1,2');		# 正确
    
    7.约束条件
    not null:	# 不能位空
    unique	key	# 唯一标识,但是可以为空	是一种键
    primary key # 主键,从约束角度看  等同于unique+not null的效果
    foreign key # 外键
    auto_increment	# 递增
    defult		# 默认
    补充:key是一种索引,在mysql中用来加速查询
    
    not null & unique
    # not null
    alter table t10 modify name char(16) not null;
    
    # not null + default
    create table t11(id int,name char(6) not null default 'male';
    
    # unique	单列唯一
    create table t12(id int unique,name char(6));
    # 联合唯一
    create table server(id int unique,
                       ip char(15),
                       port int,
                       unique(ip,port))
    insert into server values(1,'1.1.1.1.1',3306);
    
    premary key & auto_increment
    # 1.一张表中必须有,并且只能有一个主键
    innodb会以主键为树形结构来进行查询,没有主键,mysql会设置一个默认的主键
    create table t13(id int,
    				 name char(16),
    				 age int,
    				 sex char(6))engine=innidb;
    
    # 2.auto_increment:
    	通常与primary key 连用,而且通常是给id字段加
        只能给被定义成key的字段加,而且加了auto_increment 如果设置可以为空的话后可以不用传值
        
    create table t4(id int primary key auto_increment,name char(15))
    # 查看创建表时,id是否可以为空
    show create table t4;
    # 如果可以为空,则可以不传值
    # 如果是not null,则必须传值
    
    # 设置可以为字段可以为空
    aither table t4 modify id int;
    
    
    foreign key
    create table score(sid int primary key auto_increment,
    					s_id int,
    					c_id int,
    					number float,
    					foreign key(s_id) references student(sid) on update cascade on delete cascade,
    					foreign key(c_id) renferences course(cid) on update cascade on delete cascade);
    
    # foreign key(字段) references 表名(字段名)
    # on update cascade
    # on delete cascade	存在关联关系也可以直接进行删除和修改
    
  • 相关阅读:
    简单的理解原型链
    react->Context笔记
    工作上git指令小结
    vue 绑定事件如何传递参数的同时拿到事件对象
    vsCode卸载后重新安装,以前的插件有没有效果的解决方法
    mongo 分组 aggregation
    Redisson分布式锁原理
    Virtual server server already has a web module live-mix-1.0.2-t230 loaded at / therefore web module
    二进制中 1 的个数
    替换空格
  • 原文地址:https://www.cnblogs.com/hello-yuanjing/p/10014710.html
Copyright © 2011-2022 走看看