gpt4 book ai didi

mysql - SQL数据插入列错误

转载 作者:搜寻专家 更新时间:2023-10-30 22:26:40 24 4
gpt4 key购买 nike

我在 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));

结果:

ERROR 1062 (23000): Duplicate entry '' for key 'PRIMARY'

最佳答案

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');

只有在更新相同的值时才有效。

关于mysql - SQL数据插入列错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50579438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com