Unknown column 'Age' in 'field list' - mysql

I am trying to create a procedure for an update into a table but keep running into an "Unknown column 'Age' in 'field list'" error. I created an almost identical procedure just before this one, which is what has me so confused on why this is not working. Below is my procedure code, and the table layout.
Code for procedure
DELIMITER $$
create procedure NewChild(IN FirstName varchar(50),
IN LastName varchar(50),
IN Age int(2),
IN Sex varchar(7),
IN UserID int(8),
IN Allg_Notes varchar(500)
)
BEGIN
INSERT INTO Client (FirstName,LastName,Age,Sex,UserID,Allg_Notes)
values (FirstName,LastName,Age,Sex,UserID,Allg_Notes);
End;$$
DELIMITER ;
Code for table
CREATE TABLE `Child` (
`ChildID` int(8) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(50) NOT NULL,
`LastName` varchar(50) NOT NULL,
`Age` int(2) NOT NULL,
`Sex` varchar(7) NOT NULL,
`UserID` int(8) NOT NULL,
`Allg_Notes` varchar(500) DEFAULT NULL,
PRIMARY KEY (`ChildID`),
KEY `UserID` (`UserID`),
CONSTRAINT `Child_ibfk_1` FOREIGN KEY (`UserID`) REFERENCES `Client`(`UserID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Calling Example
Call NewChild('Jack','Person',2,'male',3,'none');

Okay
change this:
INSERT INTO Client (FirstName,LastName,Age,Sex,UserID,Allg_Notes)
To this
INSERT INTO Child (FirstName,LastName,Age,Sex,UserID,Allg_Notes)

You are using the Field names as variables.
So instead of Field name mysql will use the inserted value

Related

Unknown column 'dob' in 'field list'

I am getting error Unknown column 'dob' in 'field list'
I know that dob is in my table and have called it many times and in my code the insert is ordered correctly. I am just learning MySQL so I'm completely lost
insert into users (userid, firstname, username,dob)
values ('gg', 'greg', 'greg2', '1980-01-01');
here is the table I am trying to insert it into.
CREATE TABLE `users` (
`userid` varchar(50) NOT NULL DEFAULT '',
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`middleName` varchar(100) DEFAULT NULL,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`dob` date DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`occupationId` int(11) DEFAULT NULL,
`userStatusId` int(11) DEFAULT NULL,
`userTypeId` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `username` (`username`),
KEY `userStatusId` (`userStatusId`),
KEY `userTypeId` (`userTypeId`),
KEY `occupationId` (`occupationId`),
KEY `lastname` (`lastname`),
CONSTRAINT `users_ibfk_1` FOREIGN KEY (`userStatusId`) REFERENCES `userStatus` (`userStatusId`),
CONSTRAINT `users_ibfk_2` FOREIGN KEY (`userTypeId`) REFERENCES `userType` (`userTypeId`),
CONSTRAINT `users_ibfk_3` FOREIGN KEY (`occupationId`) REFERENCES `occupation` (`occupationId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I am doing all my work in Sequel Pro
insert into users (userid, firstname, username, dob)
values ('gg', 'greg', 'greg2', '1980-01-01');
i tried it this was as well and get the same error, then i tried it like this
insert into users (userid,firstname,username,dob)
values ('gg','greg','greg2','1980-01-01');
here is the code I ran before my insert
delimiter $$
create trigger usersInsert
before insert on users
for each row begin
set new.created = now();
set new.age = floor(datediff(now(),dob)/365);
end $$
delimiter ;
I was trying to see if my trigger was working so I inserted a new user into the user table to make sure it was working properly
i found where my error was. it was in my trigger
delimiter $$
create trigger usersInsert
before insert on users
for each row begin
set new.created = now();
set new.age = floor(datediff(now(),new.dob)/365);
end $$
delimiter ;
I wasn't using 'new.dob' when setting a value to dob
Thank You for all of your help!

How can I create a trigger for table logs using mysql?

Can you help me with my code? Because I am creating a table logs for my project. So I include a TRIGGER in my table but there's an error with my sql code. Here's my sql codes.
CREATE TABLE `sales_category` (
`salescatid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`salescatname` VARCHAR(128) NOT NULL,
`salescatdesc` VARCHAR(512) NOT NULL,
UNIQUE INDEX `salescatname` (`salescatname`),
UNIQUE INDEX `salescatid` (`salescatid`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
CREATE TABLE `category_log` (
`action` ENUM('CREATE','UPDATE','DELETE') NULL DEFAULT NULL,
`id` INT(10) UNSIGNED NOT NULL,
`salescatname` VARCHAR(255) NOT NULL,
`salescatdesc` VARCHAR(255) NOT NULL,
INDEX `id` (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
//Here's the error: SQL error 1054: Unknown column 'id' in 'NEW'
DELIMITER #
CREATE TRIGGER ai_category
AFTER INSERT ON sales_category
FOR EACH ROW
BEGIN
INSERT INTO category_log(action,id,salescatname,salescatedesc)
VALUES('CREATE',NEW.id,NEW.salescatname,NEW.salescatdesc);
END;#
Please help me with this thanks. I can't spot where did I go wrong with my code.
Ok i found my error. I just rename the fields in category_log exactly the sa me as my sales_category table.

MYSQL #1005 - Can't create table (errno: 150)

CREATE TABLE IF NOT EXISTS `userinit` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`usn` varchar(20) NOT NULL,
`pwd` varchar(20) NOT NULL,
`cmnd` int(9) NOT NULL,
`dienthoai` varchar(11) NOT NULL,
PRIMARY KEY (`id`,`usn`,`cmnd`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
This is the first table i've created in mysql, then i make a query to create my second table userinfo, below
create table userinfo(
cmnd int(9) primary key not null,
ngaycapcmnd date,
noicap varchar(50) character set utf8,
hoten varchar(50) character set utf8 not null,
ngaysinh date not null,
quequan varchar(50) character set utf8,
diachi varchar(100) character set utf8 not null,
email varchar(100) unique not null,
constraint fk_userinfo_userinit foreign key (cmnd) references userinit(cmnd)
)
Then it says when i press OK: #1005 - Can't create table 'xbook.userinfo' (errno: 150).
I use the lastest xampp. Can you guys help me solve this
It because you are referencing to a column which does not have a separate key, although it is part of a compound key.
Try adding a KEY in it, eg
CREATE TABLE IF NOT EXISTS `userinit`
(
......
KEY (cmnd), -- <<=== HERE
PRIMARY KEY (`id`,`usn`,`cmnd`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
SQLFiddle Demo

Mysql statement insert, if inserted, insert another

I have the following statement:
INSERT INTO `Properties`(`Id`, `Url`, `BrokerId`, `LastFound`) VALUES
(#Id,#Url,#BrokerId,#LastFound)
ON DUPLICATE KEY UPDATE LastFoundOn = #LastFoundOn;
INSERT INTO `Events`(`Id`, `Type`, `DateTime`, `PropertyId`) VALUES
(#EventId,#EventType,#Now,#Id);
There is a foreign key constraint between Properties.Id and Events.PropertyId. And the Url is unique.
This works - almost. When a recod is not inserted, but updated because of duplicate key (Url), then the insert into event will fail, because the foreign key simply doesn't exist. Like this:
Eg:
exists: 1 | http://test1.com | 2 | 2013-03-13
to insert: 2 | http://test2.com | 2 | 2013-03-14
When trying to insert, it updates instead, because of the unique url. When afterwards trying to insert the event, a foreign key (2) doesn't exist in the Properties table. How can I make an if then statement to handle this scenario?
Something like (?):
INSERT INTO `Properties`(`Id`, `Url`, `BrokerId`, `LastFound`) VALUES
(#Id,#Url,#BrokerId,#LastFound)
ON DUPLICATE KEY UPDATE LastFoundOn = #LastFoundOn;
IF LastInserted = #Id THEN
INSERT INTO `Events`(`Id`, `Type`, `DateTime`, `PropertyId`) VALUES
(#EventId,#EventType,#Now,#Id);
END IF;
UPDATE:
A trigger might be the solution, but I'm struggeling making it work. What's wrong here?
DELIMITER $$
CREATE TRIGGER Event_Submitted_Trigger AFTER INSERT ON Properties
FOR EACH ROW
BEGIN
INSERT INTO Events VALUES(SELECT(UUID()), 'PropertySubmitted', SELECT(NOW()), new.Id);
END$$
I get the following error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT(NOW()), new.Id); END$$' at line 4
Best regards,
Søren
UPDATE:
Here is my schema:
CREATE TABLE IF NOT EXISTS `Events` (
`Id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Type` enum('PropertySubmitted','PropertyChanged','PropertyRemoved') NOT NULL,
`DateTime` datetime NOT NULL,
`Attribute` varchar(128) NOT NULL,
`From` varchar(512) NOT NULL,
`To` varchar(512) NOT NULL,
`PropertyId` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`Id`),
KEY `IX_FK_PropertyEvent` (`PropertyId`),
KEY `DateTimeIndex` (`DateTime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `Properties`
--
CREATE TABLE IF NOT EXISTS `Properties` (
`Id` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Url` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Type` varchar(64) NOT NULL,
`ExtractedAddress` varchar(192) NOT NULL,
`ExtractedPostcode` varchar(8) NOT NULL,
`ExtractedCity` varchar(64) NOT NULL,
`StreetName` varchar(128) DEFAULT NULL,
`StreetNumber` varchar(8) DEFAULT NULL,
`Floor` varchar(8) DEFAULT NULL,
`Side` varchar(8) DEFAULT NULL,
`DoorNo` varchar(8) DEFAULT NULL,
`Postcode` int(4) DEFAULT NULL,
`City` varchar(64) DEFAULT NULL,
`Latitude` double DEFAULT NULL,
`Longitude` double DEFAULT NULL,
`ImageUrl` varchar(512) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
`Price` int(8) NOT NULL,
`Payout` int(8) NOT NULL,
`GrossPrice` int(6) NOT NULL,
`NetPrice` int(6) NOT NULL,
`Area` int(5) NOT NULL,
`GroundArea` int(5) NOT NULL,
`Rooms` int(2) NOT NULL,
`Year` int(4) NOT NULL,
`PriceChange` int(11) NOT NULL,
`FirstFoundOn` datetime NOT NULL,
`SubmittedOn` datetime NOT NULL,
`LastFoundOn` datetime NOT NULL,
`FoundAt` varchar(256) DEFAULT NULL,
`Validated` tinyint(1) NOT NULL,
`BrokerId` char(36) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
`Archived` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`Id`),
UNIQUE KEY `Url` (`Url`),
KEY `IX_FK_PropertyBroker` (`BrokerId`),
KEY `UrlIndex` (`Url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Triggers `Properties`
--
DROP TRIGGER IF EXISTS `Event_Submitted_Trigger`;
DELIMITER //
CREATE TRIGGER `Event_Submitted_Trigger` AFTER INSERT ON `Properties`
FOR EACH ROW BEGIN
INSERT INTO `Events` VALUES(UUID(), 'PropertySubmitted', NOW(), NEW.Id);
END
//
DELIMITER ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `Events`
--
ALTER TABLE `Events`
ADD CONSTRAINT `Events_ibfk_1` FOREIGN KEY (`PropertyId`) REFERENCES `Properties` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `Properties`
--
ALTER TABLE `Properties`
ADD CONSTRAINT `Properties_ibfk_2` FOREIGN KEY (`BrokerId`) REFERENCES `Brokers` (`Id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
Assuming the following structure:
CREATE TABLE Properties (
id INT,
url VARCHAR(100),
lastFound DATETIME,
UNIQUE (url)
) ;
CREATE TABLE Events (
id VARCHAR(36),
type VARCHAR(20),
t DATETIME,
propertyId INT
) ;
Here is a working trigger:
DELIMITER $$
CREATE TRIGGER Event_Submitted_Trigger AFTER INSERT ON Properties
FOR EACH ROW BEGIN
INSERT INTO Events VALUES( UUID(), 'PropertySubmitted', NOW(), new.Id);
END $$
DELIMITER ;
See it in action here. Notice the NOW()+SLEEP(1) hack, only meant to delay execution in order to get a significant result (SLEEP() returns 0 if not interrupted).

MySql - can BEFORE INSERT TRIGGER insert into 2 columns?

Can this trigger be changed so that the sortorder table gets 2 column values (sortOrderId, sortOrder) inserted?
How is the value of sortOrder found?
If it is known and can be inserted into image table then can it also be inserted into the sortorder table?
-- Trigger DDL Statements
DELIMITER $$
USE `nextcart`$$
CREATE
DEFINER=`root`#`localhost`
TRIGGER `nextcart`.`insert_sortorderid`
BEFORE INSERT ON `nextcart`.`image`
FOR EACH ROW
BEGIN
INSERT INTO sortorder SET sortOrderId = NULL, sortOrder = NEW.sortOrder;
SET NEW.sortOrderId = (SELECT LAST_INSERT_ID());
END;
$$
CREATE TABLE sortorder:
delimiter $$
CREATE TABLE `sortorder` (
`sortOrderId` int(11) NOT NULL AUTO_INCREMENT,
`sortOrder` tinyint(4) NOT NULL,
PRIMARY KEY (`sortOrderId`),
KEY `sort_order` (`sortOrderId`,`sortOrder`),
CONSTRAINT `fk_sortOrderId` FOREIGN KEY (`sortOrderId`) REFERENCES `image` (`imageId`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8$$
CREATE TABLE image:
delimiter $$
CREATE TABLE `image` (
`imageId` int(11) NOT NULL AUTO_INCREMENT,
`imageFileName` varchar(45) DEFAULT NULL,
`imagePath` varchar(255) DEFAULT NULL,
`imageTitle` varchar(100) DEFAULT NULL,
`imageAlt` varchar(100) DEFAULT NULL,
`imageWidth` int(11) DEFAULT NULL,
`imageHeight` int(11) DEFAULT NULL,
`classId` int(11) DEFAULT NULL,
`imageSizeId` tinyint(4) NOT NULL,
`isImageEnabled` bit(1) DEFAULT b'0',
`sortOrderId` int(11) DEFAULT NULL,
PRIMARY KEY (`imageId`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8$$
ERROR MESSAGE:
Error 1054: Unknown column 'sortOrder' in 'NEW' SQL Statement:
CREATE TRIGGER insert_sortorderid BEFORE INSERT ON image FOR EACH
ROW BEGIN INSERT INTO nextcart.sortorder SET sortOrderId = NULL,
sortOrder = NEW.sortOrder; SET NEW.sortOrderId = ( SELECT
LAST_INSERT_ID()); END; Error when running failback script. Details
follow. Error 1050: Table 'image' already exists SQL Statement: CREATE
TABLE image ( imageId int(11) NOT NULL AUTO_INCREMENT,
imageFileName varchar(45) DEFAULT NULL, imagePath varchar(255)
DEFAULT NULL, imageTitle varchar(100) DEFAULT NULL, imageAlt
varchar(100) DEFAULT NULL, imageWidth int(11) DEFAULT NULL,
imageHeight int(11) DEFAULT NULL, classId int(11) DEFAULT NULL,
imageSizeId tinyint(4) NOT NULL, isImageEnabled bit(1) DEFAULT
b'0', sortOrderId int(11) DEFAULT NULL, PRIMARY KEY (imageId)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
There is no column named sortOrder in the image table.
So, the reference to NEW.sortOrder (on the insert statement in the trigger) is invalid.
To answer your first question: No. Since there is no value supplied for that in the INSERT statement (which fires the BEFORE INSERT TRIGGER), you don't really have a source for that value.
The easy option is to provide a default value for it.
If you want to supply a value for the sortOrder column, then one option is to add a sortOrder column to the image table, and then the value can be supplied in the INSERT INTO image statement. Then it would available in the trigger.
(The purpose of the sortorder table is not at all clear.)