将外键插入SQL表

时间:2016-09-20 04:04:08

标签: sql sql-server database

根据下表,我试图回答这个问题,我不知道如何回答:

"显示每种类型的所有者的工作小时数,但不包括具有ExperienceLevel of Junior的员工的服务,并排除任何少于三名成员的类型。"

CREATE TABLE OWNER
(
    OwnerID     Int             NOT NULL IDENTITY (1,1)     PRIMARY KEY,
    OwnerName   Char(30)        NOT NULL,
    OwnerEmail  VarChar(100)    NULL,
    OwnerType   Char(15)        NOT NULL,
);

CREATE TABLE PROPERTY
(
    PropertyID      Int         NOT NULL IDENTITY(1,1)      PRIMARY KEY,
    PropertyName    Char(30)    NOT NULL,   
    Street          Char(20)    NOT NULL,
    City            Char(25)    NOT NULL,
    State           Char(10)    NOT NULL,   
    Zip             Char(5)     NOT NULL,
    OwnerID         Int         NOT NULL,

    CONSTRAINT  PROP_OWN_FK FOREIGN KEY(OwnerID)
                REFERENCES OWNER(OwnerID),
);

CREATE TABLE GG_EMPLOYEE
(
    EmployeeID      Int         NOT NULL IDENTITY(1,1)      PRIMARY KEY,
    LastName        Char(35)    NOT NULL,
    FirstName       Char(35)    NOT NULL,
    CellPhone       Char(20)    NOT NULL,
    ExperienceLevel Char(25)    NOT NULL,
);

CREATE TABLE SERVICE 
(
    PropertyID      Int         NOT NULL,
    EmployeeID      Int         NOT NULL,
    ServiceDate     Char(15)    NOT NULL,
    HoursWorked     Char(5)     NOT NULL,

    CONSTRAINT  SERVICE_PK  PRIMARY KEY(PropertyID, EmployeeID, ServiceDate),
    CONSTRAINT  SER_PRO_FK FOREIGN KEY(PropertyID)
                REFERENCES PROPERTY(PropertyID),                        
    CONSTRAINT  SER_GG_EMP_FK FOREIGN KEY(EmployeeID)
                REFERENCES GG_EMPLOYEE(EmployeeID),
);

INSERT INTO OWNER 
VALUES ('Mary Jones', 'Mary.Jones@somewhere.com', 'Individual');

INSERT INTO OWNER 
VALUES('DT Enterprises', 'DTE@dte.com', 'Corporation');

INSERT INTO OWNER 
VALUES('Sam Douglas', NULL, 'Individual');

INSERT INTO OWNER 
VALUES('UNY Enterprises', 'UNYE@unye.com', 'Corporation');

INSERT INTO OWNER 
VALUES('Doug Samuels', 'Doug.Samuels@somewhere.com', 'Individual');

INSERT INTO PROPERTY 
VALUES('Eastlake Building', '123 Eastlake', 'Seattle', 'WA', '98119');

INSERT INTO PROPERTY 
VALUES('Elm St Apts', '4 East Elm', 'Lynwood', 'WA', '98223');

INSERT INTO PROPERTY 
VALUES('Jefferson Hill', '42 West 7th St', 'Bellevue', 'WA', '98007');

INSERT INTO PROPERTY 
VALUES('Lake View Apts', '1265 32nd Avenue', 'Redmond', 'WA', '98054');

INSERT INTO PROPERTY 
VALUES('Kodak Heights Apts', '65 32nd Avenue', 'Rochester', 'NY', '14604');

INSERT INTO PROPERTY 
VALUES('Private Residence', '1456 48th St', 'Bellevue', 'WA', '98007');

INSERT INTO PROPERTY 
VALUES('Private Residence', '1567 51st St', 'Bellevue', 'WA', '98007');

INSERT INTO PROPERTY 
VALUES('Private Residence', '718 151st St', 'Rochester', 'NY', '14604');

INSERT INTO GG_EMPLOYEE 
VALUES('Smith', 'Sam', '206-254-1234', 'Master');

INSERT INTO GG_EMPLOYEE 
VALUES('Evanston', 'John', '206-254-2345', 'Senior');

INSERT INTO GG_EMPLOYEE 
VALUES('Murray', 'Dale', '206-254-3456', 'Junior');

INSERT INTO GG_EMPLOYEE 
VALUES('Murphy', 'Jerry', '585-545-8765', 'Master');

INSERT INTO GG_EMPLOYEE 
VALUES('Fontaine', 'Joan', '206-254-4567', 'Senior');

INSERT INTO SERVICE VALUES('2015-05-05', '4.50');
INSERT INTO SERVICE VALUES('2015-05-08', '2.75');
INSERT INTO SERVICE VALUES('2015-05-08', '4.50');
INSERT INTO SERVICE VALUES('2015-05-19', '3.00');
INSERT INTO SERVICE VALUES('2015-05-12', '7.50');
INSERT INTO SERVICE VALUES('2015-05-10', '2.50');
INSERT INTO SERVICE VALUES('2015-05-19', '2.50');
INSERT INTO SERVICE VALUES('2015-05-15', '2.75');

如果有人能帮助我,那就太好了!

3 个答案:

答案 0 :(得分:2)

OwnerID表格中的Property列以及PropertyId表格中的EmployeeIDService正在为您制造麻烦。

以下是原因。

  

1)所有提到的列都设置为NOT NULL。所以这些专栏   总是期望每个插页的价值。

     

2)此外,在“服务”表中,列PropertyIdEmployeeID是   复合主键的一部分。主键不允许NULL值。

     

3)一旦你处理了第1点和第2点,你就可以进行插入   在INSERT查询中提及列,如下所示

INSERT INTO PROPERTY (PropertyName,Street,City,[State],Zip) 
VALUES('Eastlake Building', '123 Eastlake', 'Seattle', 'WA', '98119');

INSERT INTO [SERVICE] (ServiceDate,HoursWorked) 
VALUES('2015-05-05', '4.50');

enter image description here

答案 1 :(得分:0)

http://sqlfiddle.com/#!6/3ea54

你需要说明谁是所有者,插入物不知道。

这里我将每个属性分配到owner = 1

INSERT INTO PROPERTY VALUES('Eastlake Building', '123 Eastlake', 'Seattle', 'WA', '98119', 1);
INSERT INTO PROPERTY VALUES('Elm St Apts', '4 East Elm', 'Lynwood', 'WA', '98223', 1 );
INSERT INTO PROPERTY VALUES('Jefferson Hill', '42 West 7th St', 'Bellevue', 'WA', '98007', 1);
INSERT INTO PROPERTY VALUES('Lake View Apts', '1265 32nd Avenue', 'Redmond', 'WA', '98054', 1);
INSERT INTO PROPERTY VALUES('Kodak Heights Apts', '65 32nd Avenue', 'Rochester', 'NY', '14604', 1);
INSERT INTO PROPERTY VALUES('Private Residence', '1456 48th St', 'Bellevue', 'WA', '98007', 1);
INSERT INTO PROPERTY VALUES('Private Residence', '1567 51st St', 'Bellevue', 'WA', '98007', 1);
INSERT INTO PROPERTY VALUES('Private Residence', '718 151st St', 'Rochester', 'NY', '14604', 1);

答案 2 :(得分:0)

嘿,luke问题是在插入查询值中,count与表的列数不匹配。 如果你想利用自动增量属性写入插入查询与列名称。 和列' OwnerID'在PROPERTY表和' EmployeeID'因为您在表创建中受限制,所以在SERVICE表中不能为空。