我正在尝试从表帐户中删除一行,但其中一列是另一个表的外键。
我试过跑:
ALTER TABLE depositor DROP FOREIGN KEY account_number;
但它随后吐出一个错误说:
ORA-00905: missing keyword
我错过了哪个关键字?
我不知道它是否有帮助,但创建表存放者的原始声明是:
create table depositor
(customer_name varchar(15) not null,
account_number varchar(15) not null,
primary key(customer_name, account_number),
foreign key(account_number) references account(account_number),
foreign key(customer_name) references customer(customer_name));
create table customer
(customer_name varchar(15) not null,
customer_street varchar(12) not null,
customer_city varchar(15) not null,
primary key(customer_name));
create table account
(account_number varchar(15) not null,
branch_name varchar(15) not null,
balance number not null,
primary key(account_number));
实际表中插入了这些表:
insert into customer values ('Jones', 'Main', 'Harrison');
insert into customer values ('Smith', 'Main', 'Rye');
insert into customer values ('Hayes', 'Main', 'Harrison');
insert into customer values ('Curry', 'North', 'Rye');
insert into customer values ('Lindsay', 'Park', 'Pittsfield');
insert into customer values ('Turner', 'Putnam', 'Stamford');
insert into customer values ('Williams', 'Nassau', 'Princeton');
insert into customer values ('Adams', 'Spring', 'Pittsfield');
insert into customer values ('Johnson', 'Alma', 'Palo Alto');
insert into customer values ('Glenn', 'Sand Hill', 'Woodside');
insert into customer values ('Brooks', 'Senator', 'Brooklyn');
insert into customer values ('Green', 'Walnut', 'Stamford');
insert into customer values ('Jackson', 'University', 'Salt Lake');
insert into customer values ('Majeris', 'First', 'Rye');
insert into customer values ('McBride', 'Safety', 'Rye');
insert into customer values ('Jones', 'Second', 'Bennington');
insert into account values ('A-101', 'Downtown', 500);
insert into account values ('A-215', 'Mianus', 1000);
insert into account values ('A-102', 'Perryridge', 400);
insert into account values ('A-305', 'Round Hill', 350);
insert into account values ('A-201', 'Perryridge', 900);
insert into account values ('A-222', 'Redwood', 100000);
insert into account values ('A-217', 'Brighton', 7500);
insert into account values ('A-333', 'Central', 8500);
insert into account values ('A-444', 'North Town', 6250);
insert into depositor values ('Johnson','A-101');
insert into depositor values ('Smith', 'A-215');
insert into depositor values ('Hayes', 'A-102');
insert into depositor values ('Hayes', 'A-101');
insert into depositor values ('Turner', 'A-305');
insert into depositor values ('Johnson','A-201');
insert into depositor values ('Jones', 'A-217');
insert into depositor values ('Lindsay', 'A-222');
insert into depositor values ('Majeris', 'A-333');
insert into depositor values ('Smith', 'A-444');
insert into depositor values ('Johnson','A-444');
答案 0 :(得分:2)
这是因为您在创建表时没有命名CONSTRAINT,因此它是自动生成的。
做一个:
SHOW CREATE TABLE depositor;
它将显示如下内容:
CREATE TABLE `depositor` (
`customer_name` varchar(15) NOT NULL,
`account_number` varchar(15) NOT NULL,
PRIMARY KEY (`customer_name`,`account_number`),
KEY `account_number` (`account_number`),
CONSTRAINT `depositor_ibfk_1` FOREIGN KEY (`account_number`) REFERENCES `account` (`account_number`),
CONSTRAINT `depositor_ibfk_2` FOREIGN KEY (`customer_name`) REFERENCES `customer` (`customer_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
其中depositor_ibfk_1是您可以使用命令删除的约束名称:
ALTER TABLE depositor DROP FOREIGN KEY depositor_ibfk_1;
答案 1 :(得分:0)
在mysql中创建外键时需要命名约束:
首先在alter table中添加约束 -
ALTER TABLE depositor ADD CONSTRAINT fk_account_number FOREIGN KEY ('account_number') REFERENCES 'account'('account_number') ON UPDATE RESTRICT ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;
然后,使用您编写的sql将fkey放入存款表
ALTER TABLE depositor DROP FOREIGN KEY fkey_account_number;