Skip to main content

Table

使用Workbench建立資料表

  • 建立:Schemas > 指定資料庫 > Tables右鍵 > Create Table
  • 刪除:指定Database右鍵 > Drop Schema

使用sql語法建立資料表

  • 指定要對哪個資料庫執行SQL語法
  • 使用USE db_name;指令
################# 語法如下:
USE db_name;
CREATE TABLE table_name(
column1 datatype,
column2 datatype
);

################# 範例如下:
use bookshop;
create table publisher(
publisher_id varchar(40) primary key not null,
publisher_name varchar(40) not null,
contact varchar(40),
phone varchar(40),
create_time timestamp default current_timestamp
);

建立外來鍵限制

  • 建立:點擊表格定義下方Foreign Keys頁籤 > 填入Foreign Key名稱,選擇Referenced Table(被參照表格) > 勾選Column(外來鍵欄位) Referenced Column(被參照欄位) > 點確認Apply按鈕
  • 刪除:指定Database右鍵 > Drop Schema
################# 語法如下:
CONSTRAINT foreignkeyname FOREIGN KEY (columnname) REFERENCES tablename (columnname)

################# 範例如下:
create table book(
isbn char(13) primary key not null,
book_name varchar(200) not null,
price decimal(8,2),
author varchar(200),
publisher_id varchar(40),
CONSTRAINT fk_book_publisher FOREIGN KEY (publisher_id) REFERENCES publisher (publisher_id)
)