zoukankan      html  css  js  c++  java
  • 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 
    );
    
    --查看表结构
    
    
    
    --创建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)
    );
    
    --查看表的创建
    
    
    --MyISAM与InnoDB区别
    --两种类型最主要的区别就是InnDB支持事物处理与外键和行级锁
    
    
    --修改表-添加字段
    --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;
  • 相关阅读:
    react ts axios 配置跨域
    npm run eject“Remove untracked files, stash or commit any changes, and try again.”错误
    java 进程的参数和list的线程安全
    帆软报表 大屏列表跑马灯效果JS
    帆软报表 快速复用数据集,避免重复劳动
    分析云 OA中部门分级思路和实现方法
    分析云 分段器 只显示一个块的数据
    分析云 更改服务默认的端口号
    分析云U8项目配置方法新版本(2)
    Oracle 创建时间维度表并更新是否工作日字段
  • 原文地址:https://www.cnblogs.com/fengzi7314/p/9923392.html
Copyright © 2011-2022 走看看