插入值而不设置主键 [英] insert value without setting primary key

查看:43
本文介绍了插入值而不设置主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了最小的数据库

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  `pass` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
  `reg_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=40 ;

然后我尝试插入新用户,只设置用户名/电子邮件/密码

then i try to insert new user, setting only username/email/pass

"INSERT INTO users SET name='user_name', email='users_email', `pass = 'users_top_secret_pass."';

"INSERT INTO users SET name='user_name', email='users_email', `pass = 'users_top_secret_pass."';

或者每次我需要插入新的 id 时,例如:

or every time i ned insert new id like:

INSERT INTO `users` (`id`, `name`, `email`, `pass`, `reg_time`) VALUES
(1, 'user', 'user@server.com', 'pass', '2016-01-31 03:10:13');

姓名和电子邮件是唯一的,时间是默认的当前时间戳,我不需要测试它们.这很好,但我如何插入 ID,我需要它吗?

name and email are UNIQUE, time is default current timestamp and i no need to test them. This is good but how i can be insert id, and does i need it?

在本地 phpmyadmin 上测试没有给我任何新东西.

testing on local phpmyadmin gives me nothing new.

upd: 删除了带有 php 函数的过时代码,只有 sql.

upd: removed outdated code with php functions, only sql.

推荐答案

omg,只要设置为 null 就行了!!!!

omg, just setting null, and it good works!!!!

INSERT INTO `users` (`id`, `name`, `email`, `pass`, `reg_time`) VALUES
(null, 'user', 'user@server.com', 'pass', null);

这篇关于插入值而不设置主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆