簡單的SQL Server語句轉換爲Mysql。

以下爲自己學習中遇到的問題,爲了以免忘記做個筆記。
用戶表:

create table users
(
    userid bigint primary key AUTO_INCREMENT,
    username varchar(30) not null unique,
    truename varchar(30) not null,
    passwd varchar(30) not null,
    email varchar (40) not null,
    phone varchar(20) not null,
    address varchar(30) not null,
    postcode char(6) not null,
    grade int default 1

);

商品表:

create table goods
(
    goodsId SMALLINT UNSIGNED primary key AUTO_INCREMENT,
    goodsName varchar(40) not null,
    goodsIntro varchar(500) not null,
    goodsPrice float not null,
    goodsNum int not null,
    publisher varchar(40) not null,
    photo varchar(40) not null,
    type varchar(10)
);

商品訂單表:

create table orders(

      ordersId bigint auto_increment primary key , -- 訂單號
      userId   bigint references users(userid), -- 哪個用戶訂的
      orderDate datetime , -- 下訂單的時間
      orderDate timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      //設置默認時間值
      //如果是在navicat下操作的話,設置字段的類型爲timestamp,默認值寫上CURRENT_TIMESTAMP
      payMode varchar(20) default '貨到付款', -- 付款的方式
      isPayed bit , -- (0,表示還沒有付款 1:表示已經付款了)
      totalPrice float not null -- 總價格
     );

訂單細節表

CREATE TABLE orderDetail(
    orderIid BIGINT,
    FOREIGN KEY (orderIid) REFERENCES orders (ordersId) ,
    goodsId SMALLINT UNSIGNED,//unsigned也一定要寫上
    FOREIGN KEY (goodsId) REFERENCES goods (goodsid),
    num int not null
);

FOREIGN KEY:外鍵約束
REFERENCES:參照對象。

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