curdate() causes error in trigger - mysql

i am using navicat8 for MySQL for writing trigger, bellow is my trigger statement.
insert into two(name,curdate())
select name from one
but it will display error while saving trigger.

insert into two(name, date_col)
select name, curdate()
from one
First you have to name the columns you want to insert into, and then the values. If you want to fill all columns from your table you can leave the column name part and do
insert into two
select name, curdate()
from one

You should specify the name of the column instead of passing CURDATE().
Use the following syntax,
insert into two (name, colName)
select name, CURDATE()
from one
UPDATE 1
So here's what you are going to do.
You need to add UNIQUE constraint on column Name on table two.
To alter the table,
ALTER TABLE tableNamehere ADD CONSTRAINT two_uq UNIQUE(name);
SQLFiddle Demo Link

Related

trying to ALTER a table based on the results from a SELECT statement in mySQL

when i use the following select statement i can return the date differences between date_due and date_returned.
select Date_Due,Date_Returned,
datediff(Date_Due,Date_Returned) as Day_Diff
From tp_rental
However i cant get these values to replace the current null values in the tp_rental table. I tried the below statement also thinking it would create a new column.
alter table tp_rental
ADD DayDiff as DATEDIFF (Date_Due,Date_Returned);
my errors say there is a problem with the syntax near as DATEDIFF
any ideas? thanks in advance
Generated column expressions need to be enclosed in () (see the manual). You also need to add a column type definition:
alter table tp_rental
ADD DayDiff INT as (DATEDIFF(Date_Due,Date_Returned));

Mysql - Unique constraint on CAST(TIMESTAMP as DATE)

I have a TIMESTAMP myDate field in my MYSQL Table.
Is there a way I can define a unique string stringForTheDay for every day in myDate?
Something like:
UNIQUE(stringForTheDay, day_in_mydate)
In short, these 2 rows must not be allowed:
2012-11-29 13:35:53 samestring
2012-11-29 20:39:09 samestring
I think your best bet is to copy the date-part into a separate column, and add a unique constraint on it. You can keep the date-part up-to-date by using triggers:
CREATE TRIGGER ins_tablename BEFORE INSERT ON tablename
FOR EACH ROW SET NEW.dateColumn = CAST(NEW.timestampColumn AS DATE);
CREATE TRIGGER upd_tablename BEFORE UPDATE ON tablename
FOR EACH ROW SET NEW.dateColumn = CAST(NEW.timestampColumn AS DATE);
(See ยง18.3 "Using Triggers" in the MySQL 5.6 Reference Manual.)
You can't.
Alternatively, you can create another column which has a data type of DATE only and make a unique compound key with your dateOnly column and the string column,
eg
CREATE TABLE Table1
(
`day_in_mydate` datetime,
`stringForTheDay` varchar(10),
`dateControl` date,
UNIQUE (dateControl, stringForTheDay)
)
a full example was demonstrated here,
SQLFiddle Demo
if you have an existing table with records on it, but you can still alter it. Add a column with DATE datatype, and execute this query
ALTER TABLE tableName ADD CONSTRAINT tb_UQ UNIQUE (dateOnlyColumn, stringColumn)
and update the table with its value from the timestamp field.
UPDATE tableName
SET dateOnlyColumn = DATE(timestampColumn)

Trouble with MAX(col)+1 INSERT into same MySQL table

I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.

Copy data from one table into other table in MYSQL

INSERT INTO delete_subscriber (SubID,RandomCode,Email,FirstName,LastName,CreateDate,UpdateDate)
SELECT subscriber.*
FROM subscriber, list_sub
WHERE ListID=? AND list_sub.SubID=subscriber.SubID;
I use a select statement to get all the record and store it into the other database, the problem is , there are more column in the delete_subscriber, that means there has a column e.g. delete date, delete person that is not in subscriber table.
How can I add them in one sql statement ? Or i have to use another statement?
But if i try the latter method, the problem is delete date, delete person are not null, I can not insert the partial record using the 1st statement
INSERT INTO delete_subscriber (SubID,RandomCode,Email,FirstName,LastName,CreateDate,UpdateDate,DeleteDate,DeletePerson)
SELECT s.SubID,s.RandomCode,s.Email,s.FirstName,s.LastName,s.CreateDate,s.UpdateDate,NOW(),?
FROM subscriber s , list_sub
WHERE list_sub.ListID=? AND list_sub.SubID=s.SubID;
I can run it on sql but not php pdo? i found it not doing transaction
Change your insert statement to
INSERT INTO delete_subscriber
(SubID,RandomCode,Email,FirstName,LastName,
CreateDate,UpdateDate,DeleteDate,DeletingPerson)
SELECT s.SubID,s.RandomCode,s.Email,s.FirstName,s.LastName,
s.CreateDate,s.UpdateDate,NOW(),?
FROM subscriber s , list_sub
WHERE list_sub.ListID=? AND list_sub.SubID=s.SubID;
You can Alter delete_subscriber table set delete date, delete person default null before insert.and after populating data from select query modify again them as previous, not null.

mysql changing values in table?

I have to change the values in my table and create an temporary table from that. But not with UPDATE, since I have to keep the original table.
for example a table like Table(id, date), I have to create a temp table by changing the date values. The ones which are NULL must be CURRENT_DATE().
How can I manage that??
Create temporary table with (id,date)
then
INSERT INTO tempoaryTable (id,date)
SELECT id, IFNULL(date,CURRENT_DATE())
FROM yourTable
You can use INSERT-SELECT form to copy all values from original table to temporary, setting corresponding field to CURRENT_DATE().
Syntax might be off as I don't now mysql
insert into temp_table
select id,coalesce(date,current_date) from mytable