【问题标题】:SQL data insertion to a column errorSQL 数据插入列错误
【发布时间】:2018-05-29 08:13:14
【问题描述】:

我在 SQL 中创建了下表。然后输入数据。然后我添加了另一列(电子邮件)。接下来我需要将数据插入到电子邮件列。但是有一个错误。

CREATE TABLE Library_books (
    ISBN VARCHAR(15),
    Book_name VARCHAR(20),
    subject TEXT(12),
    student_id VARCHAR(4),
    borrow_date DATE,
    PRIMARY KEY (ISBN),
    FOREIGN KEY (student_id)
    REFERENCES student (SID)
);


INSERT INTO Library_books VALUES  ('1-22-5567-4'    ,'Vectors'            ,'Mathematics', 'S021', '2015-04-23'),
                              ('978-14840-0'    ,'The C Programming ' ,'Computer'   , 'S005', '2016-03-01'),
                              ('567-2-13-145-3' ,'Code complete'      ,'Computer'   , 'S005', '2016-06-04'),
                              ('345-6-7889-5'   ,'How to solve it'    ,'Mathematics', 'S090', '2016-08-12'),
                              ('34-22-34556-4'  ,'Real Analysis'      ,'Mathematics', 'S021', '2016-10-22'),
                              ('3-445-7-8'      ,'Python'             ,'Computer'   , 'S021', '2016-09-11');



INSERT INTO student (email) VALUES   ('Bob@gmail.com'),
                                 ('John@gmail.com'),
                                 ('Albert@gmail.com'), 
                                 ('Sam@gmail.com'), 
                                 ('Tom@gmail.com'),
                                 ('Liz@gmail.com') ;

CREATE table student (SID varchar(4) NOT NULL,
                  student_name varchar(10) NOT NULL,
                  address varchar(12) NOT NULL,
                  age int NOT NULL,
                  telephoneNo varchar(12) NOT NULL,
                  primary key (SID));

结果:

错误 1062 (23000): 键 'PRIMARY' 的重复条目 ''

【问题讨论】:

  • 你遇到了什么错误?
  • 我认为你不应该使用 ISBN 作为主键。它留在数据库中,当你尝试插入相同的数据时,就会出现这个错误。
  • @SureshPrajapati "ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'"
  • 能否请您包括学生表的架构,似乎错误与电子邮件的插入有关。目前还不清楚错误与哪个语句有关。
  • @deHaar 谢谢!,您有什么建议吗?

标签: mysql sql sql-server database sql-insert


【解决方案1】:
INSERT INTO student VALUES (S01, 'name',...)

用于在创建表后将完整的值集(完整行)插入到表中。

但是要将数据输入到使用 ALTER TABLE 语句创建的新列中 你必须使用 UPDATE,

UPDATE student
SET email = 'bob@gmail.com'
WHERE SID = 'S01';

要使用不同(不同)数据值更新多行,您需要多次使用 UPDATE 语句,如下所示,

UPDATE student SET email = 'Bob@gmail.com' WHERE SID = 1;
UPDATE student SET email = 'John@gmail.com' WHERE SID = 1;
UPDATE student SET email = 'Albert@gmail.com.com' WHERE SID = 1;
UPDATE student SET email = 'Sam@gmail.com' WHERE SID = 1;
UPDATE student SET email = 'Tom@gmail.com' WHERE SID = 1;
UPDATE student SET email = 'Liz@gmail.com' WHERE SID = 1;

但如果所有记录的新数据都相同,那么您可以使用以下方式。

UPDATE student
SET email = 'bob'@gmail.com
WHERE SID IN ('S01', 'S02', 'S03', 'S04', 'S05');

仅当更新相同的值时才有效。

【讨论】:

    【解决方案2】:

    您需要使用update 语句query 而不是再次插入。 尝试下面的查询,每封电子邮件使用学生 ID 作为 where 子句。

    UPDATE student set email='Bob@gmail.com' where SID='S021';
    

    【讨论】:

    • student_id 应该是 SID。
    • 是的,没错,我只是在示例中添加了它,您可以放入 SID,现在已编辑并更改为 SID
    • 我需要将数据添加到多行中。你有什么建议吗?
    • 如果您必须在多列中添加数据,请使用以下查询
    • 我有 6 列
    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多