使用php在sql中插入具有相同主键的多行 [英] Insert multiple rows with same primary key in sql using php

查看:52
本文介绍了使用php在sql中插入具有相同主键的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要插入多个具有相同主记录的记录.我尝试使用相同的主键插入多条记录,但 sql 给出错误重复主键".可以插入带有 id 或主键的多行.?

Want to insert multiple records with same primary. i am try to insert multiple record with same primary key but the sql giving the error "duplicate primary key". it can possible to insert multiple rows with id or primary key.?

推荐答案

Primary Key 旨在唯一

Primary Key is intended to be unique

http://dev.mysql.com/doc/refman/5.5/en/optimizing-primary-keys.html

你可以有一个复合主键:

CREATE TABLE track(
  album CHAR(10) NOT NULL,
  dsk INTEGER NOT NULL,
  posn INTEGER NOT NULL,
  song VARCHAR(255),
  PRIMARY KEY (album, dsk, posn)
)

复合唯一键在您想要防止表中多个数据库字段中出现重复数据时非常有用.

Composite unique keys are very useful for when you want to prevent duplicate data across a number of database fields in a table.

http://www.alphadevx.com/a/382-Adding-a-composite-unique-key-to-an-existing-MySQL-table

这篇关于使用php在sql中插入具有相同主键的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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