2015-09-26 41 views
-2

SQL查询SQL约束错误:(匹配的主键)

create table wine 
wine_Id NVARCHAR2(8), 
wine_name NVARCHAR2(100), 
wine_vintage Smallint, 
wine_price NVARCHAR2(8), 
retailer_ID NVARCHAR2(8), 
constraint wine_Id_pk1 primary key(wine_ID)); 
Foreign Key(retailer_ID) References retailer(retailer_ID)); 

Table created 

SP2-0734: unknown command beginning "Foreign Ke..." - rest of line ignored. 

然后我运行下面的插入物:

insert into wine values('101', 'Grange', '2010', '750', '1001'); 

    insert into wine values('102', 'Grange', '2006', '700', '1001'); 

1 row created. 

    insert into wine values('103', 'Reserve Shiraz', '2013', '10', '1001'); 

1 row created. 

    insert into wine values('104', 'Grey Label Shiraz', '2012', '35', '1001'); 

1 row created. 

    insert into wine values('105', 'Patricia Shiraz', '2009', '50', '1001'); 
1 row created. 

    insert into wine values('106', 'Ten Acres Shiraz', '2012', '25', '1001'); 
1 row created. 

    insert into wine values('107', 'Double Barrel Shiraz', '2012', '15', '1001'); 
1 row created. 

    insert into wine values('108', 'Platinum Label Shiraz', '2006', '170', '1001'); 
1 row created. 

    insert into wine values('103', 'Reserve Shiraz', '2013', '9', '1002'); 
    insert into wine values('103', 'Reserve Shiraz', '2013', '9', '1002') 

ERROR at line 1: 
ORA-00001: unique constraint (RYSENEVI.WINE_ID_PK1) violated` 


    insert into wine values('104', 'Grey Label Shiraz', '2012', '33', '1002'); 

ERROR at line 1: 
ORA-00001: unique constraint (RYSENEVI.WINE_ID_PK1) violated.` 

    insert into wine values('105', 'Patricia Shiraz', '2009', '44', '1002'); 

insert into wine values('105', 'Patricia Shiraz', '2009', '44', '1002') 
    * 
    ERROR at line 1: 
    ORA-00001: unique constraint (RYSENEVI.WINE_ID_PK1) violated` 


    insert into wine values('106', 'Ten Acres Shiraz', '2012', '22', '1002'); 

insert into wine values('106', 'Ten Acres Shiraz', '2012', '22', '1002') 
    * 
    ERROR at line 1: 
    ORA-00001: unique constraint (RYSENEVI.WINE_ID_PK1) violated` 


    insert into wine values('107', 'Double Barrel Shiraz', '2012', '12', '1002'); 

ERROR at line 1: 
    ORA-00001: unique constraint (RYSENEVI.WINE_ID_PK1) violated` 

我无法添加我的值代入表,因为相同的主键,所以我使用了约束方法,但它仍然不起作用,请帮忙?

+0

请清理你的问题。 –

+0

尽管我们使用了约束,但我们不能添加主键值 –

+0

哪个RDBMS用于此目的?请添加一个标签来指定您是使用'mysql','postgresql','sql-server','oracle'还是'db2' - 或者其他的东西。 –

回答

1

首先,您没有用外键约束创建表。

create table wine 
wine_Id NVARCHAR2(8), 
wine_name NVARCHAR2(100), 
wine_vintage Smallint, 
wine_price NVARCHAR2(8), 
retailer_ID NVARCHAR2(8), 
constraint wine_Id_pk1 primary key(wine_ID)); <=====ended the create table statement 


Foreign Key(retailer_ID) References retailer(retailer_ID));<===unknown command 

试试这个:

create table wine (
wine_Id NVARCHAR2(8), 
wine_name NVARCHAR2(100), 
wine_vintage Smallint, 
wine_price NVARCHAR2(8), 
retailer_ID NVARCHAR2(8), 
constraint wine_Id_pk1 primary key(wine_ID), 
constraint retailer_ID_fk1 foreign key(retailer_ID) 
references retailer(retailer_ID)); 

你的问题,其余的似乎从后来创建不正确的约束干。

在上面提供的表格定义中,您只能有一个Wine_ID,但您可以拥有尽可能多的retailer_ID,因为retailer_ID存在于零售商表中。

相关问题