mysql创建表的字段时为什么不能用primary key和not null放在一起?

pak001 2010-01-21 10:36:28
下面的命令错哪里了呢?
create table user(
serial int unsigned not null primary key,
);
...全文
1245 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
liucui5201013 2012-03-02
  • 打赏
  • 举报
回复
那这个语句现在 该怎么写啊?
foolbirdflyfirst 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cqiangren 的回复:]
引用 5 楼 foolbirdflyfirst 的回复:
但是问题又来了
create table user1(
serial int unsigned primary key auto_increment,
);
这样就不行啊???
===================
这个问题 是因为你多了个,
语句出错

SQL codecreatetable user1(
serialint unsignedprimarykey auto_increment
);
这样mysql还是能正确解析语句的。



嗯,好像auto_increment,primary key和not null都没有冲突,只是我在末尾多了个逗号
是不是这样的呢???
[/Quote]
你执行下不就知道了。详细点的应该多看mysql文档。

column_definition:
col_name type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string'] [reference_definition]
Siramizu 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 cqiangren 的回复:]
嗯,好像auto_increment,primary key和not null都没有冲突,只是我在末尾多了个逗号
是不是这样的呢???
[/Quote]
闭合括号前面那行没有逗号
pak001 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 foolbirdflyfirst 的回复:]
但是问题又来了
create table user1(
serial int unsigned primary key auto_increment,
);
这样就不行啊???
===================
这个问题 是因为你多了个,
语句出错

SQL codecreatetable user1(
serialint unsignedprimarykey auto_increment
);
这样mysql还是能正确解析语句的。
[/Quote]


嗯,好像auto_increment,primary key和not null都没有冲突,只是我在末尾多了个逗号
是不是这样的呢???
jaxio 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jaxio 的回复:]
CREATE TABLE `tb_account1` (
  `fd_account_id` int(10) NOT NULL auto_increment PRIMARY KEY ,
  `fd_account_no` varchar(50) NOT NULL default '',
  `fd_account_accountno` varchar(50) NOT NULL default '',
  `fd_account_name` varchar(50) NOT NULL default '',
  `fd_account_bank` varchar(50) NOT NULL default '',
  `fd_account_money` float(12,2) NOT NULL default '0.00',
  `fd_account_principal` varchar(20) NOT NULL default '',
  PRIMARY KEY  (`fd_account_id`)
)
正确用法。。。
[/Quote]
不好意思,这个是错误的,我想用来检测有什么错误 结果返回

[Err] 1068 - Multiple primary key defined
jaxio 2010-01-21
  • 打赏
  • 举报
回复
CREATE TABLE `tb_account1` (
`fd_account_id` int(10) NOT NULL auto_increment ,
`fd_account_no` varchar(50) NOT NULL default '',
`fd_account_accountno` varchar(50) NOT NULL default '',
`fd_account_name` varchar(50) NOT NULL default '',
`fd_account_bank` varchar(50) NOT NULL default '',
`fd_account_money` float(12,2) NOT NULL default '0.00',
`fd_account_principal` varchar(20) NOT NULL default '',
PRIMARY KEY (`fd_account_id`)
)
正确用法。。。
jaxio 2010-01-21
  • 打赏
  • 举报
回复
CREATE TABLE `tb_account1` (
`fd_account_id` int(10) NOT NULL auto_increment PRIMARY KEY ,
`fd_account_no` varchar(50) NOT NULL default '',
`fd_account_accountno` varchar(50) NOT NULL default '',
`fd_account_name` varchar(50) NOT NULL default '',
`fd_account_bank` varchar(50) NOT NULL default '',
`fd_account_money` float(12,2) NOT NULL default '0.00',
`fd_account_principal` varchar(20) NOT NULL default '',
PRIMARY KEY (`fd_account_id`)
)
正确用法。。。
foolbirdflyfirst 2010-01-21
  • 打赏
  • 举报
回复
但是问题又来了
create table user1(
serial int unsigned primary key auto_increment,
);
这样就不行啊???
===================
这个问题 是因为你多了个,
语句出错

create table user1(
serial int unsigned primary key auto_increment
);

这样mysql还是能正确解析语句的。
foolbirdflyfirst 2010-01-21
  • 打赏
  • 举报
回复
http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.

create table user1(
`serial` SERIAL
);
pak001 2010-01-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 luciferstar 的回复:]
primary key本身应该是not null的吧?
[/Quote]

下面这样就行,是不是primary key 默认是null啊?
create table user1(
serial int unsigned primary key auto_increment,
birthday date
);
但是问题又来了
create table user1(
serial int unsigned primary key auto_increment,
);
这样就不行啊???

到底primary key,auto_increment和not null这三个有什么关系啊?要怎么用呢?



LuciferStar 2010-01-21
  • 打赏
  • 举报
回复
primary key本身应该是not null的吧?
jaxio 2010-01-21
  • 打赏
  • 举报
回复
CREATE TABLE `tb_user` (
`id` int(10) NOT NULL auto_increment,
.......
}

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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