1

i have tried following queries to alter existing column with default getdate constraint

ALTER TABLE PartyTypes ALTER COLUMN CreatedDate GetDate();
ALTER TABLE partytypes ADD CONSTRAINT DF_Constraint DEFAULT GetDate() FOR CreatedDate;

both are giving error

ALTER TABLE partytypes ADD CONSTRAINT DF_Constraint DEFAULT GetDate() FOR CreatedDate Error Code: 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 'DEFAULT GetDate() FOR CreatedDate' at line 1 0.000 sec

any updates on this

2
  • Using workbench right click on the table and select Alter Table. Then using the GUI its more easy to set the default for the desired column Commented Mar 13, 2017 at 9:18
  • nope...i want to do this from query....as it will be helpfull for others also...and what is wrong with sql query Commented Mar 13, 2017 at 9:20

1 Answer 1

1

You don't specify any foreign table.

Constraints are only foreign keys, not limitations to column values. What you might want to use is a trigger or set the default to now():

ALTER TABLE PartyTypes CHANGE COLUMN CreatedDate CreatedDate datetime NOT NULL DEFAULT NOW();

Be sure to provide the column name twice, so you don't rename it.

More on the syntax of ALTER TABLE can be found in the MySQL manual.

Sign up to request clarification or add additional context in comments.

6 Comments

What is GetDate()? Does not exist in MySQL
Are we talking about SQL Server or MySQL? Your tag says MySQL. And MySQL does not have any function called GetDate
we are talking about executing this in mysql..sorry to mix with sql server..but i just wanted to know what works in mysql in place of getdate()
If you are interested in only the date, call date(now()) instead of now()
Then try curdate()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.