末学者笔记--MariaDB 数据库 二玄

Mariadb 数据库2

.MariaDB 数据类型                                                                          

MariaDB数据类型可以分为数字,日期和时间以及字符串值。

使用数据类型的原则:够用就行,尽量使用范围小的,而不用大的

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

 

 

 

二.sql语句-增、删、改                                                                      

1.显示当前时间

>select now();

 

2.创建class(id, name)

先自创一个数据库:

>create database feige character set=utf8;

>use feige;

建立表:

>create table class(

id tinyint unsigned primary key not null auto_increment,

name varchar(20)

age tinyint unsigned

);     --unsigned非负数,auto_increment:默认自增编号。

--查看表结构

>show create table class;

>desc  class;

 

3.创建students(id, name, age, high, gender, cls_id)

>create table students (

    id tinyint 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

);

--查看表的创建

>show create table students;

 

MyISAMInnoDB区别

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

 

4.alter 修改表(add,modify,change,drop

(1)--修改表-添加字段

--alter table 表名 add 列名 类型;

>alter table students add birthday datetime;

(2)-- 修改表-修改字段:不重命名版

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

>alter table students modify birthday date;

(3)-- 修改表-修改字段:重命名版

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

>alter table students change birthday birth date;

(4)-- 修改表-删除字段

-- alter table 表名 drop 列名;

>alter table students drop birthday;

5.删除表

-- drop table 表名;

>drop table students;

6.增删改查

1--增加

        --全列插入

        --insert into 表名 values(..)

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

        

        

        -- students表里插入 一个学生信息

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

        查看:

        >select * from students;

 

        --部分插入

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

        --部分插入(多条记录)

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

        --默认自增的规律:

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

        insert into students values (5,'小明',19,188.999,'', 1);

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

        Select * from students

 

>show create table students ;

 

2.修改

    --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;

3-删除

        -- 物理删除

        -- 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;

--------------------------————————————分割线-————————————————————

猜你喜欢

转载自www.cnblogs.com/feige2L/p/10883326.html