【數據庫】1庫、引擎、約束、表、字段、數據

一、庫

庫操作 DDL語言
創建 create database 庫名
刪除 drop database 庫名
查看 show databases
用庫 use 數據庫名

二、引擎

InnoDB:事務型數據庫首選引擎,支持事務安全表,支持行鎖定
MyISAM:較高的插入,查詢速度,不支持事物
MEMORY:將表中數據存儲到內存,默認

引擎操作
改引擎 my.ini→mysqld→default-storage-engine的值→重啓
alter table 表 engine = 更改後的引擎名;
展示 show engines;
show variable like ‘storage_engine’

三、約束

create table 表名(
sno int ①primary key ⑧auto_increment,
sname varchar(10)⑤not null,
sage int(4)⑥unique,
ssex varchar(20) ⑦default ‘男’,(default 111)
②primary key(son),
③foreign key(sno)references 另一個表(id);
⑨check (sno>0)
);

    ②可以多字聯合主鍵primary key(son,sname,sage)
序號 約束 功能
①② 主鍵 primary key 不可以重複,不能爲空,一個表只有一個主鍵
外鍵 foreign key 外鍵等於另一個表的主鍵
非空 not null
唯一約束 unique 不出現重複值
默認約束 default ‘默認數據’或數值 默認值
自動增加 auto_increment 初始值1只有一個只能在主鍵後面 賦值null
檢查 check(條件) 放在最後

約束
單列增加 alter table 表 modify 列名 數據類型 約束
取消非空 alter table 表名 modify 列名 數據類型 null
多列增加 alter table 表名 add 約束(列名)
刪除 alter table 表名 drop 約束名 約束字段名
刪除主鍵 alter table 表名 drop primary key
刪除外鍵 alter table 表名 drop foreign key 外鍵名

四、表

1、創建 creat

create table 表名(
sno int primary key auto_increment,
sname varchar(10)not null,
sage int(4)unique,
ssex varchar(20) default ‘男’,(default 111)
primary key(son),
foreign key(sno)references 另一個表(id)
);

2、 刪除表(先刪子,再刪父)
drop table 表名; drop table if exists 表名1,表名2
3、改表名
alter table 舊名 rename 新名;
4、查看錶
show tables;desc 表名;show create tables 表名/G;
5、別名
select * from 表 as 別名 where 別名.字段=條件;
select c.c_id,o.o_id from cus as c left join order as o on c.c_id=o.o_id;
6、另存爲表
create table 新名 as select * from 舊名;

五、字段

1、增
alter table 表名 add 列名 類型()not null;
alter table 表名 add 列名 類型() (first/after 已存在列名 );
2、 刪除
alter table 表名 drop 字段名;
3、改列名
alter table 表 change 舊名 新名 數據類型(舊名=新名只改變數據類型)
改數據類型
alter table 表modify 列名 數據類型
改位置
alter table 表 modify 列名 數據類型 first/after 列名2;
4、查
desc 表名;
5、別名 as
select f1.f_name as fruit_name,f1.f_price as fruit_price from fruit as f1 where f1.fprice<8;


六、數據

1、增
insert into 表名(列名1,列名2) value (‘據1’,’據2’);
insert into 表名(列名1,列名2) value (‘據1’,’據2’),(‘據3’,’據4’);

沒有寫列名,插入數據必須與表中所有字段一致,主鍵插入爲null
返回信息:
. Recorde插入記錄條數
. Duplicutes插入被忽略記錄(主鍵重複)
. Warning有問題數值(數據類型)

2、 刪除
delete from 表 where 條件;自動增長,不會從1開始
清空:truncate table 表;自動增長從1開始
刪除重複:delete distinct 類型 from 表名;
3、改 updatate set value
update 表 set 字段=值,字段=值 where 條件;
4、查詢結果插入表中
insert into 表1(列名)select 列名 from 表2;

發佈了47 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章