I have a requirement where in my sql a column 2 would always have the uppercase value that is column 1,
Not sure ow to do this in mysql. I want to do something like this , I know the syntax below is incorrect but writing some psuedo code as to make it clear what I am trying to achieve
create table sakila.testupper(name varchar(50),
uppername varchar(50) not null default as select upper(#name));
I believe you could accomplish this across most version of MySQL with a trigger on insert or update: http://dev.mysql.com/doc/refman/5.7/en/triggers.html
The syntax you're after looks like this: "create table sakila.testupper (name varchar(50), uppername varchar(50) generated always as (upper(name)));". However, you'd need a recent release of MySQL version 5.7.
You can't have the table do this automatically but you can create a trigger for all future INSERTs. With a trigger you will need to maintain a separate table.
drop table if exists p;
drop table if exists q;
create table q (name nvarchar(59),uppername nvarchar(59));
create table p (name nvarchar(59));
create trigger trig_thing after insert on p
for each row
begin
insert into q set name = new.name, uppername = upper(new.name);
end;
insert into p (name) values ('Some nice gentleman');
insert into p (name) values ('A sweet old lady');
select * from q;
Your original table will keep only the name, but each insert will cause a trigger to insert the same data into your new table with name and uppername.
If you already have names stored in a table you should insert into your second table to get them in line before you set up the trigger:
insert into q name, upper(name) from p;
here is a functional example
Related
So I read the other posts but this question is unique. So this SQL dump file has this as the last entry.
INSERT INTO `wp_posts` VALUES(2781, 3, '2013-01-04 17:24:19', '2013-01-05 00:24:19'.
I'm trying to insert this value to the table...
INSERT INTO `wp_posts` VALUES(5, 5, '2005-04-11 09:54:35', '2005-04-11 17:54:35'
it gives me the error, "Column count doesn't match value count at row 1." So I'm lost on the concept of how the column and row apply here.
Doesn't 2781,3 mean row 2781 and column 3? And doesn't 5,5 mean row 5 and column 5?
The error means that you are providing not as much data as the table wp_posts does contain columns. And now the DB engine does not know in which columns to put your data.
To overcome this you must provide the names of the columns you want to fill. Example:
insert into wp_posts (column_name1, column_name2)
values (1, 3)
Look up the table definition and see which columns you want to fill.
And insert means you are inserting a new record. You are not modifying an existing one. Use update for that.
you missed the comma between two values or column name
you put extra values or an extra column name
You should also look at new triggers.
MySQL doesn't show the table name in the error, so you're really left in a lurch. Here's a working example:
use test;
create table blah (id int primary key AUTO_INCREMENT, data varchar(100));
create table audit_blah (audit_id int primary key AUTO_INCREMENT, action enum('INSERT','UPDATE','DELETE'), id int, data varchar(100) null);
insert into audit_blah(action, id, data) values ('INSERT', 1, 'a');
select * from blah;
select * from audit_blah;
truncate table audit_blah;
delimiter //
/* I've commented out "id" below, so the insert fails with an ambiguous error: */
create trigger ai_blah after insert on blah for each row
begin
insert into audit_blah (action, /*id,*/ data) values ('INSERT', /*NEW.id,*/ NEW.data);
end;//
/* This insert is valid, but you'll get an exception from the trigger: */
insert into blah (data) values ('data1');
MySQL will also report "Column count doesn't match value count at row 1" if you try to insert multiple rows without delimiting the row sets in the VALUES section with parentheses, like so:
INSERT INTO `receiving_table`
(id,
first_name,
last_name)
VALUES
(1002,'Charles','Babbage'),
(1003,'George', 'Boole'),
(1001,'Donald','Chamberlin'),
(1004,'Alan','Turing'),
(1005,'My','Widenius');
You can resolve the error by providing the column names you are affecting.
> INSERT INTO table_name (column1,column2,column3)
`VALUES(50,'Jon Snow','Eye');`
please note that the semi colon should be added only after the statement providing values
In my case i just passed the wrong name table, so mysql couldn't find the right columns names.
I want to create a procedure that creates a course for a user this takes one parameter that is userid and the other value will be selected from tbl_chapter.there are 27 chapters that are going to be selected and 27 inserts will be executed .insert is going to be like INSERT INTO tbl_user_chapter(user_id,chapter_id) VALUEs (9 , 1),(9,2),(9,3),....
I want something like this:
CREATE PROCEDURE createCourse (IN userid_param int)
BEGIN
INSERT INTO tbl_user_chapter(tbl_user_chapter.user_id,tbl_user_chapter.chapter_id) VALUE(userid_param , SELECT id FROM tbl_chapter)
END
SELECT id FROM tbl_chapter will be multiple rows.
I know this is wrong and I need help.
if there is a better way to do this please let me know.
If the select does not return one row, then don't use the VALUES( ) syntax. Use INSERT ... SELECT syntax:
CREATE PROCEDURE createCourse (IN userid int)
BEGIN
INSERT INTO tbl_user_chapter(user_id,chapter_id)
SELECT userid, id FROM tbl_chapter;
END
Make sure userid does NOT conflict with a column of the same name in your tbl_chapter table. If a column exists with that name, you should change the IN parameter of the stored procedure.
I looked into MySQL duplicate key but cant figure it out.
I have a table like below:
id series chapter path(can be unique)
I want only insert data and not update. Lets say I have data like below:
seri:Naruto, klasor:567 ==> If both of these exist in table then do not insert.
seri:Naruto, klasor:568 ==> If Naruto exist but 568 does not exist then do insert.
How can I achieve this?
Easiest way would be to define unique index with two columns on that table:
ALTER TABLE yourtable ADD UNIQUE INDEX (seri,klasor);
You may also define two column primary key, which would work just as well.
Then use INSERT IGNORE to only add rows when they will not be duplicates:
INSERT IGNORE INTO yourtable (seri, klasor) VALUES ('Naruto',567);
INSERT IGNORE INTO yourtable (seri, klasor) VALUES ('Naruto',568);
Edit: As per comments, you can't use UNIQUE INDEX which complicates things.
SET #seri='Naruto';
SET #klasor=567;
INSERT INTO yourtable
SELECT seri,klasor FROM (SELECT #seri AS seri, #klasor AS klasor)
WHERE NOT EXISTS (SELECT seri, klasor FROM yourtable WHERE seri=#seri AND klasor=#klasor);
You may use the above query with two local variables or convert it to single statement by replacing the local variables with actual values.
Better way would be to use stored procedure:
CREATE PROCEDURE yourinsert (vseri VARCHAR(8), vklasor INT)
BEGIN
DECLARE i INT;
SELECT COUNT(*) INTO i FROM yourtable WHERE seri=vseri AND klasor=vklasor;
IF i=0 THEN
INSERT INTO yourtable (seri,klasor) VALUES (vseri, vklasor);
END IF;
END;
This would allow you to perform the INSERT using:
CALL yourinsert('Naruto',567);
INSERT INTO table_name (seri, klasor) VALUES ('Naruto',567)
WHERE NOT EXISTS( SELECT seri,klasor FROM table_name WEHERE seri='Naruto' AND klasor=567
)
Hope this helps..
I'm trying to insert new rows into a MySQL table, but only if one of the values that I'm inserting isn't in a row that's already in the table.
For example, if I'm doing:
insert into `mytable` (`id`, `name`) values (10, `Fred`)
I want to be able to check to see if any other row in the table already has name = 'Fred'. How can this be done?
Thanks!
EDIT
What I tried (can't post the exact statement, but here's a representation):
INSERT IGNORE INTO mytable (`domain`, `id`)
VALUES ('i.imgur.com', '12gfa')
WHERE '12gfa' not in (
select id from mytable
)
which throws the 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 'WHERE '12gfa' not in ( select id from mytable)' at line 3
First of all, your id field should be an autoincrement, unless it's a foreign key (but I can't assume it from the code you inserted in your question).
In this way you can be sure to have a unique value for id for each row.
If it's not the case, you should create a primary key for the table that includes ALL the fields you don't want to duplicate and use the INSERT IGNORE command.
Here's a good read about what you're trying to achieve.
You could use something like this
INSERT INTO someTable (someField, someOtherField)
VALUES ("someData", "someOtherData")
ON DUPLICATE KEY UPDATE someOtherField=VALUES("betterData");
This will insert a new row, unless a row already exists with a duplicate key, it will update it.
DELIMITER |
CREATE PROCEDURE tbl_update (IN id INT, IN nm VARCHAR(15))
BEGIN
DECLARE exst INT;
SELECT count(name) INTO exst FROM mytable WHERE name = nm;
IF exst = 0 THEN
INSERT INTO mytable VALUES(id, name);
END IF;
END
|
DELIMITER ;
or just make an attribute name as UNIQUE
Is it possible in mysql to create a table with a column that combines two column values? something like this:
create table test1 (
number1 int,
number2 int,
total int DEFAULT (number1+number2)
);
or like this :
CREATE TABLE `Result` (
`aCount` INT DEFAULT 0,
`bCount` INT DEFAULT 0,
`cCount` = `aCount` + `bCount`
);
It is not possible to do that exactly, but you can create a view based on a query that combines them:
CREATE VIEW `my_wacky_view` AS
SELECT `number1`, `number2`, `number1` + `number2` AS `total`
FROM `test1`;
I would avoid actually storing the combined data in a table, unless you're going to be running lots of queries that will reference the combined data in their WHERE clauses.
You can create a trigger on the table so MySQL calculates and automatically inserts that column value every time an INSERT happens on your test1 table. Make the table:
create table test1 (
number1 int,
number2 int,
number3 int
);
Then create a Trigger
CREATE TRIGGER triggername AFTER INSERT
ON test1
FOR EACH ROW
UPDATE test1 SET NEW.number3=NEW.number1+NEW.number2
MySQL documentation on triggers:
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Make sure to add the ON UPDATE trigger as well if you expect UPDATES to happen to the rows.
I had this issue as well. From Edgar Velasquez' answer here, and the answer to this question, I stumbled upon this incantation:
CREATE TRIGGER insert_t BEFORE INSERT
ON test1
FOR EACH ROW
SET NEW.number3=NEW.number1+NEW.number2;
CREATE TRIGGER insert_t_two BEFORE UPDATE
ON test1
FOR EACH ROW
SET NEW.number3=NEW.number1+NEW.number2;
This works for me on MySQL 5.6.22.
Little fix :
CREATE TRIGGER triggername BEFORE INSERT
ON test1
FOR EACH ROW
SET NEW.number3=NEW.number1+NEW.number2