標籤:多表 單表 efault prim 返回結果 檔案 name 根據 分享
內容:
1.Mysql
程式:
--socket用戶端
2.根據IP和連接埠進行串連
3.發送指令(send):
xxxxx
7.接受結果
--socket服務端
1.運行起來
4.擷取指令(recv):
xxxxx
5.解析命令
檔案夾操作:
檔案操作:
檔案內容操作
6.返回結果
----------------------------------SQL語句---------------------------------
一、檔案夾操作(資料庫):
建立:
create database db1;
create database db1 default charset utf-8;
刪除:
drop database db1;
進入檔案夾:
use db1;
二、檔案操作(資料表):
建立表:
create table tb1(
id int,
name char(20),
age int,
gender char(1)
)
刪除表:
drop table tb1;
----------------------------------------------------------------------------------
1.列名
2.資料類型
3.是否可以為空白
4.預設值
create table tb1(
id int not null, # id列不可以為空白
name char(20),
age int default 18, # 年齡(age)預設為18
gender char(1)
)
5.自增(一個表只能有一個自增列)
create table tb1(
id int not null auto_increment, # auto_increment表示自增
name char(20),
age int default 18, # 年齡(age)預設為18
gender char(1)
)
6.主鍵:
約束:不可為空,不能重複
索引:加速尋找
7.外鍵:
使用外鍵建立資料表可以減少使用空間(一表對多表)
約束:只能是某個表中某列已經存在的資料
constraint xxxx foreign key (department_id) references deparment (id)
---------------------------------------建立表--單表--------------------------------
create table userinfo (
id int null auto_increment primary key, # id 為列名, auto_increment代表id自增, primary key表示為主鍵
name char(20),
age int default 18 ,
gender char(1)
) engine=innodb default charset=utf8; # engine=innodb引擎操作, 設定建立表時預設的字元集utf8
---------------------------------向表中插入資料---------------------------------
插入資料是忽略id列
age列可寫可不寫,不寫預設為18
insert into userinfo (name,age,gender) values(‘zhaosj‘,25,‘男‘);
#################################建立--多表#################################
1、建立多表的原因:
假設以上資料表存在硬碟上佔用了12M,表示大量的浪費了磁碟空間。
2、節省資源方式建立兩張表
第一張表:
語句:
create table userinfo(
id int not null auto_increment primary key,
name char(20),
age int default 18,
gender char(1),
department_id int,
constraint xxxx foreign key (department_id) references deparment (id) # 添加約束,防止出現亂的資料
) engine=innodb default charset=utf8;
添加約束說明:
這種約束稱之為:外鍵
constraint xxxx foreign key (department_id) references deparment (id) # 添加約束語句,表示當前建立表語句中的department_id 與 deparment表中的id進行關聯。其中的‘xxxx‘為約束的名字,可以隨意起。
第二張表:
語句:
create table deparment(
id int not null auto_increment primary key,
title char(32)
) engine=innodb default charset=utf8;
資料表操作:
向deparment表中插入資料:
insert into deparment (title) value (‘IT部‘);
insert into deparment (title) value (‘諮詢部‘);
向userinfo表中插入資料:
insert into userinfo (name,age,gender,department_id) values (‘alex‘,18,‘男‘,2); # 其中 department_id的值只能寫1或者2
三、檔案內容操作(資料行):
<11day>_資料庫操作-【Mysql】