mariadb(第二章)增刪改 MariaDB 數據類型

MariaDB 數據類型

MariaDB數據類型能夠分爲數字,日期和時間以及字符串值。sql

使用數據類型的原則:夠用就行, 儘可能使用範圍小的,而不用大的code

  • 經常使用的數據類型
  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:對關係字段進行約束,當爲關係字段填寫值時,會到關聯的表中查詢此值是否存在,若是存在則填寫成功,若是不存在則填寫失敗並拋出異常

image

 image

image

 sql語句-增、刪、改

--顯示當前時間
select now();

--建立classes表(id, name)

create table zzzz(
    id int primary key not null auto_increment,
    name varchar(20),
    age int 
);

--查看錶結構
desc haha(表名)

--建立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 classes

--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;
相關文章
相關標籤/搜索