資料庫之建立表

2021-10-04 20:50:29 字數 1711 閱讀 1537

一.定義資料庫:

1.規則:

create schema 《模式名》 authorization 《使用者名稱》[《表定義子句》|《檢視定義子句》|《授權定義子句》]

2.舉例:

為使用者wang定義乙個學生-課程模式s-t

create schema 「s-t」 authorization wang;

注意:上面是sqlserver資料庫,而mysql資料庫跟這個規則有區別,mysql資料庫直接在命令視窗寫create database s-t;

二.建立表

規則:create table 《表名》(《列名》 《資料型別》[ 《列級完整性約束條件》 ]

[,《列名》 《資料型別》[ 《列級完整性約束條件》] ]

… [,《表級完整性約束條件》 ]);

《表名》:所要定義的基本表的名字

《列名》:組成該錶的各個屬性(列)

《列級完整性約束條件》:涉及相應屬性列的完整性約束條件

《表級完整性約束條件》:涉及乙個或多個屬性列的完整性約束條件

舉例:

mysql> creat table customer(列名cid 資料型別int(11) 列級完整性約束條件not null primary key comment 『顧客id』);

注意:*表名customer後面還要有()。

*create不要少了最後的e。

三.渲染表

1.欄位是引用的外碼:

(1)表級約束條件時:

例一:create table if not exists orders(

oid int(11) not null primary key comment 『訂單號』 auto_increment,

pid int(11) not null comment 『產品id』,

cid int(11) not null comment 『顧客id』,

foreign key(pid) references product(pid),

foreign key(cid) references customer(cid),

ocount int(11) not null comment 『物品數量』,

oprice decimal(8,2) not null comment 『物品單價』,

odate datetime not null comment 『訂單時間』

);例二:

create table sc

(sno char(9),

cno char(4),

grade smallint,

primary key (sno,cno),

/* 主碼由兩個屬性構成,必須作為表級完整性進行定義*/

foreign key (sno) references student(sno),

/* 表級完整性約束條件,sno是外碼,被參照表是student /

foreign key (cno)references course(cno)

/ 表級完整性約束條件, cno是外碼,被參照表是course*/

);

MYSQL資料庫之建立資料庫表

每個表都應有乙個主鍵字段。主鍵用於對錶中的行進行唯一標識。每個主鍵值在表中必須是唯一的。此外,主鍵字段不能為空,這是由於資料庫引擎需要乙個值來對記錄進行定位。主鍵字段永遠要被編入索引。這條規則沒有例外。你必須對主鍵字段進行索引,這樣資料庫引擎才能快速定位給予該鍵值的行。下面的例子把 personid...

python之建立資料庫表

安裝以下模組 pip3 install flask mysqldb pip3 install flask pip3 install flask sqlalchemy from flask import flask from flask sqlalchemy import sqlalchemy cla...

mysql之建立資料庫,建立資料表

專案中用到mysql資料庫,之前也沒用過mysql,今天就學下mysql的常用的語法,發現跟sql server的語法極其相似。用起來還是蠻簡單的。1 建立乙個名為school的資料庫。1 建立乙個學生資訊表 學生id 自增,主鍵 姓名,年齡,性別,籍貫,入學時間,所屬班級id 外來鍵 2 建立乙個...