2016-09-25 87 views
0
CREATE TABLE Customer (
    customerid varchar(10) NOT NULL, 
    FirstName varchar(20) NOT NULL, 
    LastName varchar(20) NOT NULL, 
    StreetAddress varchar (30) NOT NULL, 
    City varchar (20)NOT NULL, 
    State varchar (20) NOT NULL, 
    Zip int(5) NOT NULL, 
    Hphone int (10) NOT NULL, 
    Mphone int (10) NOT NULL, 
    Ophone int (10) NOT NULL, 
    PRIMARY KEY (customerid)); 

CREATE TABLE OrderTable (
    donutorderid varchar(10) NOT NULL, 
    customerid varchar (10) NOT NULL, 
    oderdate datetime (6) NOT NULL, 
    PRIMARY KEY (donutorderid) 
); 

CREATE TABLE Donut (
    donutid varchar(10), 
    donutname varchar(20), 
    description varchar(30), 
    unitprice numeric, 
    PRIMARY KEY (donutid) 
); 

CREATE TABLE OrderLine (
    donutorderid varchar (30), 
    donutid varchar (30), 
    qty int (10), 
    PRIMARY KEY (donutorderid,donutid) 
); 

ALTER TABLE OrderTable ADD INDEX checks (customerid), 
ADD CONSTRAINT checks FOREIGN KEY (customerid) REFERENCES Customer (customerid); 
ALTER TABLE OrderLine ADD INDEX has (donutorderid), 
ADD CONSTRAINT has FOREIGN KEY (donutorderid) REFERENCES OrderTable (donutorderid); 
ALTER TABLE OrderLine ADD INDEX available_in (donutid), 
ADD CONSTRAINT available_in FOREIGN KEY (donutid) REFERENCES Donut (donutid); 

create view CustInfo 
    AS Select customerid, concat(FirstName,LastName) as FullName from customer; 

insert into customer 
    (customerid, FirstName, LastName, StreetAddress, 
    City, State, Zip, Hphone, Mphone, Ophone) 
values ('123','John','Doe', 'one hoover lane', 'las vegas', 
     'nevada', 89104, 702555122, 702441111, 702332222); 

數據截斷:暫時爲列「Hphone」範圍值中的第1行數據截斷誤差和int數據類型

當我有9位數字在我的電話例如702551212 vs 7025551212它的工作原理和Hphone,Mphone,Ophone都有int(10)。

+0

請,*請*,格式化這個職位! –

+3

另外 - 不要使用整數來存儲電話號碼。僅僅因爲它們包含數字不會使它們成爲數字(提示 - 不是0的數字通常不以「0」開頭)。使用varchar數據類型來代替... –

+2

郵政編碼不是最好的數字存儲爲 – RiggsFolly

回答

1

你已經聲明你的電話號碼是整數。這是一個非常非常糟糕的主意。聲明他們是字符串:

CREATE TABLE Customer (
    customerid varchar(10) NOT NULL, 
    FirstName varchar(20) NOT NULL, 
    LastName varchar(20) NOT NULL, 
    StreetAddress varchar (30) NOT NULL, 
    City varchar (20)NOT NULL, 
    State varchar (20) NOT NULL, 
    Zip char(5) NOT NULL, 
    Hphone varchar(32) NOT NULL, 
    Mphone varchar(32) NOT NULL, 
    Ophone varchar(32) NOT NULL, 
    PRIMARY KEY (customerid) 
); 

爲什麼電話號碼是我不清楚 - 等欄目 - 聲明NOT NULL。並非每個人都有三個電話號碼。事實上,有些人的數量更少,還有更多,這表明電話號碼應該放在一個單獨的表格中。

我也將郵編編碼爲一個字符串。畢竟,前導零對郵政編碼非常重要。

但是,對於立即解決問題,然後用單引號把數字作爲字符串:

insert into customer(customerid, FirstName, LastName, StreetAddress, 
    City, State, Zip, Hphone, Mphone, Ophone) 
values ('123','John','Doe', 'one hoover lane', 'las vegas', 
     'nevada', '89104', '702555122', '702441111', '702332222');