MySQL資料庫2 建立表

2021-09-30 17:53:47 字數 3274 閱讀 9189

1.建立表的語法形式:

create table 表名(屬性名 資料型別 [完整性約束條件],

屬性名 資料型別 [完整性約束條件],

…屬性名 資料型別 );

表名表示所要建立表的名稱,屬性名表示表中字段的名稱,資料型別表示指定

欄位的資料型別,

注:1)表名不能為sql關鍵字,如create,updata,order,乙個表名可以有乙個或

多個屬性,定義時,字母大小寫均可,各屬性之間用逗號隔開,最後乙個屬性

不用加逗號。

2)在建立表之前需要使用use 來選擇資料庫,基本語法為:use 資料庫名

例:建立新錶

create table example1 (id int,

name varchar(20),

*** boolean);

約束條件為如下:

primary key: 將指定的列標為主鍵

foreign key :將指定的列標為外來鍵,其作用為關聯其他表的主鍵

not null:將指定的列標記為非空

unique:將指定的列標記為唯一的

auto_increment:將指定的列的值設定為自動增加

default:將指定的列設為預設值。

2.設定表的主鍵

1)主鍵是表的特殊字段,能唯一的標識該錶的每條資訊,

2)主鍵的主要目的是幫助mysql以最快的速度查詢到表中的某一條資訊

3)主鍵必須是唯一的,表中任意兩條記錄的主鍵字段值不能相同

4)主鍵非空

5)主鍵可以是單一字段,也可以是多字段的組合

第一種:單字段的主鍵:

語法:列名 資料型別 primary key

例:

create table example 2(stu_id int primary key,

stu_name varchar(20),

stu_*** boolen

);

第二種:多欄位主鍵:

語法:primary key(列名1,列名2…列名n)

例:

create table example3(stu_id int,course_id int,grade float,primary key(stu_id,course_id));
3.設定表的外來鍵:

1)外來鍵是表的特殊字段,若sno是表a的列名,且依賴於表b的主鍵,那麼b為父表,a為子表,sno為表的外來鍵

2)設定外來鍵的原則:必須依賴於資料庫中已存在父表的主鍵

3)外來鍵可以為空

4)外來鍵的作用:建立子表與父表的聯絡,父表中刪除某資訊時,子表中與之對應的資訊也必須有相應的改變,如stu_id 是student表中的主鍵,stu_id是grade表的外來鍵,當stu_id為任同學退學了,需要從student表中刪除該學生的資訊,那麼,grade表中stu_id為任同學的所有的資訊也該同時刪除

設定外來鍵的語法:

constraint 外來鍵別名 foreign key (列名1.1,列名1.2,…列名1.n)

references 表名(列名2.1,列名2.2,…,列名2.n)

外來鍵別名是外來鍵的代號,列名1是子表中設定的外來鍵,表名是指父表的名稱,列

名2是父表的主鍵。

例:

create table example4(id int primary key,stu_id int,course_id int,constraint c_fk foreign key(stu_id,course_id)  references example3(stu_id,course_id));
example4包含3個列名,列名id是主鍵,stu_id和course_id為外來鍵,c_fk是外來鍵的別名,example3是example4的父表;example4的外來鍵依賴於父表example4的主鍵 stu_id和course_id。

4.設定非空約束

語法:列名 資料型別 notnull

例:

create table example5(id int notnull primary key,name varchar(20) not null,stu_id int,constraint d_fk foreign key(stu_id) references example2(stu_id));
example5中包括3個列名。id為主鍵,id和name欄位非空,stu_id為外來鍵;d_fk

為外來鍵的別名;example2為example5的父表;example5的外來鍵依賴於父表的

主鍵stu_id

5.設定表的唯一性約束

唯一性是指所有記錄中該字段的值不能重複出現

語法: 列名 資料型別 unique

例:

create table example 6(id int primary key,stu_id int unique,name varchar(20) not null);
example6 中有3個列名,id為主鍵,stu_id是唯一值,name非空

6.設定表的值的自動增加

auto_increment主要用於為表中插入新的記錄自動生成唯一的id,乙個表

中只能有乙個字段使用自動增長約束,且必須為主鍵的一部分,一般情況下,

從1開始自增。

語法規則:列名 資料型別 auto_increment

例:

create table example7(id int primary key auto_increment,stu_id int unique,name varchar(20)  not null);
example7表中有3個列名,id為主鍵,且每插入一條新記錄id的值會自動增加,stu_id是唯一值,該欄位不能重複,name為非空字段

7.設定表屬性的預設值

語法:列名 資料型別 default 預設值

例:

create table example 8 (id int primary key  auto_increment,stu_id int unique,name varchar (20) not null,engilsh varchar(20) default 'zero',

math float default 0, computer float defuat 0);

example8中有6個列名,id為主鍵,每插入一條記錄id的值會自動增加;stu_id

欄位是唯一值,name為非空字段,english預設值為zero,math 和computer

字段預設值為0.

mysql 建立資料庫建立表

建立資料庫表 create database if not exists my db default charset utf8 collate utf8 general ci 注意後面這句話 collate utf8 general ci 大致意思是在排序時根據 utf8 變碼格式來排序 那麼在這個...

資料庫Mysql 2 建立表 刪表

1.建立表 create table table name 列名1 列名屬性,列名2 列名屬性,列名3 列名屬性 2.刪除表 drop table table name 當你不需要該錶時,可以用drop 也就是說包括表名和資料啥都沒有了 當你仍要保留該錶,但要刪除所有記錄時,用 truncate 仍...

MySQL 2 MySQL 建立資料庫和表

create database 語句用於在 mysql 中建立資料庫。create database database name 為了讓 php 執行上面的語句,我們必須使用mysql query 函式 用於向 mysql 連線傳送查詢或命令 create table 用於在 mysql 中建立資料...