For the following SQL I get the error:
Data truncation occurred on a write of column 0Data was 0 bytes long and 0 bytes were transferred.
Code:
CREATE TABLE faculty_members
(
fid INTEGER AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
rank VARCHAR(25) NOT NULL,
office CHAR(8),
phone CHAR(12),
dob DATE NOT NULL,
salary DECIMAL(8,2),
CONSTRAINT faculty_members_pk PRIMARY KEY(fid),
CONSTRAINT faculty_members_ck UNIQUE(first_name,last_name,dob),
CONSTRAINT valid_salary CHECK(salary > 0)
);
And these are the INSERT statements
INSERT INTO faculty_members(first_name,last_name,rank,office,phone,dob,salary) VALUES
('Charles', 'Xavier', 'Professor', 'HSC 641', '563-555-6020', '11/09/1942', 125000),
('Bruce', 'Banner', 'Associate Professor', 'FO1 120', '563-555-8212', '06/20/1969', 87000),
('Hank', 'McCoy', 'Professor', 'FO1 120', '563-555-8212', '06/20/1972', 95000),
('Jeane', 'Grey', 'Assistant Professor', 'ECS 547', '563-555-8239', '03/19/1975', 95000),
('Erik', 'Lehnsherr', 'Professor', 'HSC 641', '563-555-6020', '11/09/1940', 115000),
('Diana', 'Prince', 'Associate Professor', 'HSC 400', '563-555-8212', '07/28/1967', 105100),
('Logan', 'Wolverine', 'Assistant Professor', 'ECS 540', '563-555-8100', '02/27/1964', 82000),
('Ororo', 'Storm', 'Assistant Professor', 'ECS 540', '563-555-8101', '05/01/1973', 82500);
Your dob values are incorrect. MySQL's date format is yyyy-mm-dd. Most like you're getting the default 0000-00-00 stored and the data truncation error from that.
In our case, we were adding a value exceeding the column size.
Related
I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below
CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));
CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));
Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams
INSERT INTO Teams (Principal)
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);
In your INSERT statement
INSERT INTO Teams (Principal)
-- ^
you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:
Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL
these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.
You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.
INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...
Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help
I have this
CREATE TABLE TEST_CAR (
CARID CHAR(36) NOT NULL,
DATE_NEW TIMESTAMP,
DATE_EDIT TIMESTAMP,
USER_NEW VARCHAR(63),
USER_EDIT VARCHAR(63),
MANUFACT VARCHAR(50),
MODEL VARCHAR(50),
MILEAGE INTEGER,
PURCHDATE TIMESTAMP,
BATCH VARCHAR(50),
FUELTYPE INTEGER,
PRIMARY KEY (CARDID));
and it still returns
Error code 30000, SQL state 42X93: Table 'TEST_CAR' contains a
constraint definition with column 'CARDID' which is not in the table.
Line 1, column 1
CREATE TABLE TEST_LIST (
LISTID CHAR (36) NOT NULL,
CAR_ID CHAR (36) NOT NULL,
DRIVER_ID CHAR (36) NOT NULL,
DATE_EDIT TIMESTAMP,
DATE_NEW TIMESTAMP,
USER_EDIT VARCHAR (63),
USER_NEW VARCHAR (63),
F_FROM VARCHAR (50),
T_TO VARCHAR (50),
KM INTEGER,
DESCRIPTION VARCHAR (50),
DATE_FROM TIMESTAMP,
DATE_TO TIMESTAMP,
PRIMARY KEY (ID));
CREATE INDEX ON TEST_LIST (CAR_ID ASC);
Error code 30000, SQL state 42X93: Table 'TEST_LIST' contains a
constraint definition with column 'ID' which is not in the table. Line
1, column 1 Error code 30000, SQL state 42X01: Syntax error:
Encountered "ON" at line 1, column 14. Line 17, column 1
You are setting CARDID as primary key, whilst you named your column CARID.
Change
PRIMARY KEY (CARDID));
to
PRIMARY KEY (CARID));
Solution to your second problem
You define ID as primary key even though ID is not in your table
i´m trying to insert some values in mysql but i get error code 1136, i verified and one of the values is auto-increment so i don't have to enter that one and the rest give a total of 18 which are exactly the total values i'm writting, could somebody help me?
This is the table im using:
FIELD TYPE NULL KEY DEFAULT EXTRA
id_display_detail int(11) NO PRI auto_increment
amount double NO
amount_lc double NO
exchange double NO
company varchar(10) NO
date datetime NO
description varchar(100) NO
document_number varchar(20) NO
document_type varchar(2) NO
posting_key varchar(3) NO
special_gl varchar(1) NO
status int(11) NO
voucher_number varchar(40) NO
year int(11) NO MUL
id_currency int(11) NO MUL
id_employee int(11) NO MUL
credit bit(1) YES
card_type varchar(45) NO
line_item int(11) YES
And this is my code:
INSERT INTO display_detail VALUES (300,300,0,'2001','2016-04-11',
'Downpayment ZM00080621','2000010802','ZP','29','R',0,
'GCCTEA 8062130',2016,1,1561,0,NULL,1);
Am i missing something?
and one of the values is auto-increment so i don't have to enter that one
That doesn't change the fact that the number of values in your VALUES clause has to match the number of columns.
You need to either specify NULL as the value for the auto_increment column - or specify a column list after INSERT first.
You missing the column name
(Because the id is automatic the values you provided don't math the number of column so must declare the column name)
INSERT INTO display_detail ( amount,
amount_lc ,
exchange ,
company ,
date ,
description ,
document_number,
document_type ,
posting_key ,
special_gl ,
status ,
voucher_number ,
year ,
id_currency ,
id_employee ,
credit ,
card_type ,
line_item ) VALUES (300,300,0,'2001','2016-04-11',
'Downpayment ZM00080621','2000010802','ZP','29','R',0,
'GCCTEA 8062130',2016,1,1561,0,NULL,1);
It appears that you aren't listing the columns in your INSERT statement. A MySQL query typically looks like this:
INSERT INTO table
(column1, column2, ... )
VALUES
(expression1, expression2, ... ),
(expression1, expression2, ... ),
...;
(Taken from: http://www.techonthenet.com/mysql/insert.php)
The final query would be something like this:
INSERT INTO display_detail
(amount, amount_lc, exchange, company, date, description,
document_number, document_type, posting_key,special_gl,status,voucher_number,year,
id_currency, id_employee, credit, card_type, line_item)
VALUES (300,300,0,'2001','2016-04-11',
'Downpayment ZM00080621','2000010802','ZP','29','R',0,
'GCCTEA 8062130',2016,1,1561,0,NULL,1);
So I´ve looked trough the web in search for basic date help in sql and nobody seems to be able to help my codes goes like this
create table Hotel
(
id int not null primary key auto_increment,
Name varchar(255)
);
create table Gestur
(
id int not null primary key auto_increment,
nafn varchar(255),
heimili varchar(255),
simi char(7),
netfang varchar(255)
);
create table Bokun
(
id int not null primary key auto_increment,
ID_hotel_fk int references Hotel(id),
ID_gestur_fk int references Gestur(id),
dags_inn date null,
dags_ut date null,
tegund_herbergis char(1)
);
and I can´t seem to get this part right
insert into Bokun
(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values
(1,3, 2015-10-25,2016-12-26,"1"),
(2,5, 2015-04-01, 2016-8-24,"3"),
(3,4, 2014-02-24, 2016-12-08,"1"),
(4,2, 2015-04-26, 2016-12-24,"2"),
(5,4, 2015-07-14, 2016-04-23,"1"),
(6,2, 2015-12-12, 2016-09-12,"3"),
(7,3, 2015-12-26, 2016-05-03,"2"),
(8,2, 2013-09-12, 2014-06-10,"1"),
(9,1, 2015-05-26, 2016-12-28,"1"),
(10,5, 2015-03-30, 2016-06-07,"4");
I only get the error
1292 - Incorrect date value: '1980' for column 'dags_inn' at row 1
You need to quote dates with ' and qoute "1" as '1':
insert into Bokun(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values(1,3, '2015-10-25','2016-12-26','1');
2015-10-25 is treated as 1980 (aritmetic operation substraction)
SqlFiddleDemo
insert into Bokun
(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values
(1,3, '2015-10-25','2016-12-26',"1"),
(2,5, '2015-04-01', '2016-8-24',"3"),
(3,4, '2014-02-24', '2016-12-08',"1"),
(4,2, '2015-04-26', '2016-12-24',"2"),
(5,4, '2015-07-14', '2016-04-23',"1"),
(6,2, '2015-12-12', '2016-09-12',"3"),
(7,3, '2015-12-26', '2016-05-03',"2"),
(8,2, '2013-09-12', '2014-06-10',"1"),
(9,1, '2015-05-26', '2016-12-28',"1"),
(10,5, '2015-03-30', '2016-06-07',"4");
you missed '(quotes).Thanks this helps..
I need to auto_increment the primary key in a mysql database using a trigger. Unfortunately, I am not quite sure how to do this. In the sample code I have provided, I need the employee table primary key to auto_increment beginning with an empty table and a starting value of 200. Then, I need each new insert to increment by 1.Thanks for looking and I hope you are able to help me.
CREATE TABLE department (
dept_name VARCHAR(50) NOT NULL Primary Key
);
CREATE TABLE employee (
emp_id INT(6) unsigned Default 0 Not NULL
, last_name VARCHAR(25) NOT NULL
, first_name VARCHAR(40) NOT NULL
, dept_name VARCHAR(50) NOT NULL
, PRIMARY KEY(emp_id, dept_name)
, FOREIGN KEY(dept_name) REFERENCES department (dept_name)
);
There are several things you need to do:
Declare the emp_id column as AUTO_INCREMENT;
Set the value of AUTO_INCREMENT property of the table to 200;
Do not provide any value for column emp_id when you INSERT rows in table employee.
Change the table creation as below:
CREATE TABLE employee (
emp_id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
last_name VARCHAR(25) NOT NULL,
first_name VARCHAR(40) NOT NULL,
dept_name varchar(50) NOT NULL
PRIMARY KEY(emp_id),
FOREIGN KEY(dept_name) REFERENCES department_tbl(dept_name)
) AUTO_INCREMENT=200;
If the table has an AUTO_INCREMENT column then it must be the PRIMARY KEY of the table. I removed dept_name from the definition of the PK above. I also removed the default value 0 from the emp_id column. It's default value is generated by MySQL using the AUTO_INCREMENT policy.
When you INSERT a new record into the employee table all you have to do is to not provide any value for the emp_id column:
INSERT INTO employee (last_name, first_name, dept_name)
VALUES ('Doe', 'John', 'accounting');
Then use the LAST_INSERT_ID() MySQL function to retrieve the value of the emp_id generated on insertion.
The language or the library you use to develop the client application probably has a function that wraps LAST_INSERT_ID() and returns its value.