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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM