mysql的外键问题

hsbljyy 2007-10-24 10:19:53
这是我的sql语句,不知道我这样创建外键对吗?(可以在数据库里执行。)

create table test
(id int primary key,
username varchar(20) not null,
unique(username));

create table test1
(id int,
username varchar(20),
pid int,
primary key(id,username));

alter table test1 add constraint foreign key(username)
references test(username) on delete cascade
on update cascade;

可是插入数据的时候只能把数据插入test表,而test1表始终插入不了。

mysql> insert into test
-> values(1,'snow');
Query OK, 1 row affected (0.02 sec)

mysql> inser into test1
-> values(1,'snow',1);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'inser
into test1
values(1,'snow',1)' at line 1

请问:这是什么问题?(我数据库基础很差!)

...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hsbljyy 2007-10-24
  • 打赏
  • 举报
回复
真的是我输入错误了。。。太粗心了。谢谢了!
hsbljyy 2007-10-24
  • 打赏
  • 举报
回复
- -!
好的。。。我检查一下。
谢谢!
懒得去死 2007-10-24
  • 打赏
  • 举报
回复

注意检查你的拼写是否正确
mysql > insert into test
- > values(1, 'snow ');
Query OK, 1 row affected (0.02 sec)

mysql > insert into test1
- > values(1, 'snow ',1);

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧