Mariadb数据类型

MariaDB 数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值。 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的

  • 常用的数据类型
  1. 整数:int, bit
  2. 小数:decimal                                     #decimal(5,2)
  3. 字符串:varchar, char                         
  4. 日期时间:date, time, datetime
  5. 枚举类型(enum)
  • 约束
  1. 主键primary key:物理上存储的顺序
  2. 非空not null:此字段不能为空
  3. 唯一unique:此字段不允许重复
  4. 默认default:当不填写此值时会使用默认值,如果填写则已填写为准
  5. 外键foreign key:对关系字段进行约束,当为关系字段填写值时,会到关联的表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并抛出异常

 

 sql语句-增、删、改 –显示当前时间

select now();

–创建classes表(id, name)

create table zzzz(

    id int primary key not null auto_increment,

    name varchar(20),

    age int

);

–查看表结构

show create table students;

–创建students表(id, name, age, high, gender, cls_id)

create table students (

    id int unsigned not null auto_increment primary key,

    name varchar(20),

    age tinyint unsigned default 0,

    high decimal(5,2),

    gender enum(‘男‘, ‘女‘, ‘中性‘, ‘保密‘) default ‘保密‘,

    cls_id int unsigned

);

–创建classes表(id, name)

create table classes(

    id int unsigned not null auto_increment primary key,

    name varchar(20)

);

–查看表的创建

desc students;

–MyISAM与InnoDB区别

–两种类型最主要的区别就是InnoDB支持事物处理与外键和行级锁

–修改表-添加字段

–alter table 表名 add 列名 类型;

alter table students add birthday datetime;

— 修改表-修改字段:不重命名版

— alter table 表名 modify 列名 类型及约束;

alter table students modify birthday date;

— 修改表-修改字段:重命名版

— alter table 表名 change 原名 新名 类型及约束;

alter table students change birthday birth date;

— 修改表-删除字段

— alter table 表名 drop 列名;

alter table students drop birthday;

— 删除表

— drop table 表名;

drop table students;

–增删改查

    –增加

        –全列插入

        –insert into 表名 values(..)

        –主键字段 可以用0 null default 来站位

       

        — 向students表里插入 一个学生信息

        insert into students values (0,‘小明‘,19,188.999,‘男‘, 1);

       

        –部分插入

        insert into students(id, name, age) values (0,‘绿帽子‘,19);

        –部分插入(多条记录)

        insert into students(id, name, age) values (0,‘绿帽子‘,19),(0,‘小跳蚤‘,21);  

    –修改

    –update 表名 set 列1=值1, 列2=值2… where 条件;

    update students set age=100 where id=1;

    update students set age=100,cls_id=77 where id=1;

    –删除

        — 物理删除

        — delete from 表名 where 条件

        delete from students where cls_id=88;   

     — 逻辑删除

     — 用一条字段来表示 这条信息是否已经不能在使用了

     — 给students表添加一个is_delete字段 bit 类型

     alter table students add is_delete bit default 0;

     update students set is_delete=1 where id=6;