简体   繁体   English

mysql语句不会插入

[英]mysql statement wont insert

Hey guys I've searched for answers through the forums but to no avail so I'm using MySql and I'm trying to insert statements for certain tables and they aren't going into the tables and I'm getting errors like "Msg 8152, Level 16, State 14, Line 1 String or binary data would be truncated. The statement has been terminated." 大家好,我已经在论坛上搜索了答案,但无济于事,所以我使用的是MySql,并且我试图为某些表插入语句,而这些表并没有进入表中,并且出现诸如“ Msg”的错误8152,级别16,状态14,行1字符串或二进制数据将被截断。该语句已终止。” These are the statements I'm having problems with.`INSERT INTO Course VALUES 这些是我遇到的问题。将INSERT INTO Course VALUES

INSERT INTO Course VALUES (12345, 'DatabaseManagement', '2015-2-1', '2014-5-9');
INSERT INTO Course VALUES (12346, 'Calculus', '2015-1-12', '2015-5-9');
INSERT INTO Course VALUES (12347, 'Biology', '2015-1-3', '2015-5-9');
INSERT INTO Course VALUES (12348, 'Chemistry', '2015-1-2', '2015-5-9');

INSERT INTO Grade VALUES (10, 12345, 012, 'A');
INSERT INTO Grade VALUES (11, 12346, 013, 'B');
INSERT INTO Grade VALUES (12, 12347, 014, 'C');
INSERT INTO Grade VALUES (13, 12348, 015, 'D');
INSERT INTO Grade VALUES (14, 12345, 016, 'B');

INSERT INTO Student VALUES (54321, 'Rachel', 'Cotterel', '2013-4-15', '2016-3-4');
INSERT INTO Student VALUES (54320, 'John', 'Smith', '2012-1-23', NULL);
INSERT INTO Student VALUES (54319, 'Johny', 'Depp', '2010-5-12', '2012-10-10');
INSERT INTO Student VALUES (54318, 'Orlando', 'Bloom', '2014-6-24', NULL);
INSERT INTO Student VALUES (54317, 'Linda', 'Jacob', '2015-4-4', '2019-8-6');

Try using this: 尝试使用此:

INSERT INTO table1 (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

These are the field types: 这些是字段类型:

CREATE TABLE Course
(
CourseID int,
Description varchar(20) NOT NULL,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
CONSTRAINT [PK_CourseID] PRIMARY KEY (CourseID)
);  

CREATE TABLE Grade
(
GradeID integer(10) NOT NULL,
CourseID integer(10) NOT NULL,
StudentID integer(10) NOT NULL,
Grade varchar (10) NULL,
CONSTRAINT [PK_GradeID] PRIMARY KEY (GradeID),
CONSTRAINT [FK_CourseID] FOREIGN KEY (CourseID) REFERENCES Course(CourseID),
CONSTRAINT [FK_StudentID] FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);

CREATE TABLE Student
(
StudentID integer(10) NOT NULL,
FirstName varchar(45) NOT NULL,
LastName varchar(45) NOT NULL,
RegistrationDate varchar (45) NOT NULL,
GraduationDate DATE NULL,
CONSTRAINT [PK_StudentlID] PRIMARY KEY (StudentID)
);

String or binary data would be truncated The reason that you get this message should be that you are trying to insert some value to some field to which you haven't assigned enough size to hold the value. 字符串或二进制数据将被截断。之所以收到此消息,是因为您试图将某些值插入某些您尚未为其分配足够大小以容纳该值的字段。 Can you send what the exact error message you get? 您可以发送确切的错误消息吗?

I tried to do it myself.But the error I got was from you insertion query to Grade table foreign key fails which refer Student table because you are trying to insert Student_IDs which are not there in you Student table 我尝试自己做。但是我得到的错误是从您插入查询到成绩表外键失败,该错误引用学生表,因为您试图插入学生表中不存在的学生ID

I didn't get any error for insert into Course statements. 插入Course语句时没有任何错误。 I got error for INSERT INTO Grade statements. INSERT INTO Grade语句出错。 Its because there is no reference available for StudentID 012,013 etc in Student table. 这是因为学生表中没有适用于StudentID 012,013等的引用。 And you are trying to add them in grade table. 您正在尝试将它们添加到成绩表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM