i have a table like below:
create table info (username varchar(30),otherinfo varchar(100));
now i want to alter this table to have new field and this field has to have default value as
md5(username)
something like below:
alter table info add NewField varchar(100) default md5(username);
how to do so?
Thanks for your help
Per MySQL docs (emphasis added) you cannot have an expression in a default value:
10.1.4. Data Type Default Values
The DEFAULT value clause in a data
type specification indicates a default
value for a column. With one
exception, the default value must be a
constant; it cannot be a function or
an expression. This means, for
example, that you cannot set the
default for a date column to be the
value of a function such as NOW() or
CURRENT_DATE. The exception is that
you can specify CURRENT_TIMESTAMP as
the default for a TIMESTAMP column.
I've tested that the following trigger works for your intent:
CREATE TRIGGER MyTriggerName
BEFORE INSERT ON info
FOR EACH ROW
SET NEW.NewField = md5(NEW.username);
You should write a Trigger to achieve this functionality, given that MySQL has a md5 function built in or you are capable to either write or find a md5 algorithm for MySQL :)
Take a look # http://dev.mysql.com/doc/refman/5.0/en/triggers.html
for the md5 function take a look here
http://dev.mysql.com/doc/refman/5.0/en/encryption-functions.html
Also, you should know that MD5 is not a secure algorithm anymore.
Related
CREATE TABLE test ( id serial primary key, name text );
CREATE OR REPLACE PROCEDURE test_insert_data( "name" text)
LANGUAGE SQL
AS $$
INSERT INTO public.test values("name")
$$;
Error & Hint:
column "id" is of type integer but expression is of type character varying
LINE 4: INSERT INTO public.test values("name")
^
HINT: You will need to rewrite or cast the expression.
I followed this tutorial: https://www.enterprisedb.com/postgres-tutorials/10-examples-postgresql-stored-procedures.
Obviously, I don't need to attach the column id for inserting.
There is no quoting issue, like comments would suggest.
And the linked tutorial is not incorrect. (But still bad advise.)
The missing target column list is the problem.
This would work:
CREATE OR REPLACE PROCEDURE test_insert_data("name" text)
LANGUAGE sql AS
$proc$
INSERT INTO public.test(name) -- !! target column list !!
VALUES ("name");
$proc$;
(For the record, since "name" is a valid identifier, all double quotes are just noise and can (should) be omitted.)
If you don't specify the target column(s), Postgres starts to fill in columns from left to right, starting with id in your case - which triggers the reported error message.
(The linked tutorial also provides an ID value, so it does not raise the same exception.)
Even if it would work without explicit target column list, it's typically still advisable to add one for persisted INSERT commands. Else, later modifications to the table structure can break your code silently. With any bad luck in a way you'll only notice much later - like filling in the wrong columns without raising an error.
See:
SQL INSERT without specifying columns. What happens?
Inserting into Postgres within a trigger function
Aside: I would never use "name" as column name. Not even in a generic tutorial. That's not helpful. Any column name is a "name". Use meaningful identifiers instead.
I was trying to create a column with data type time and trying to set the default value as the current time using HeidiSql. Below is the altered code.
ALTER TABLE "user"
ADD "user" TIME NULL DEFAULT CURRENT_TIME();
But it output an error as below.
syntax error at or near ")"
LINE 2: ADD "created_time" TIME NULL DEFAULT CURRENT_TIME()
Any idea about my mess?
This works fine:
create table users (user_id serial);
ALTER TABLE users
ADD create_Time TIME NULL DEFAULT CURRENT_TIME;
You don't need parentheses around CURRENT_TIME.
Some other things:
The error in your question is not generated by the SQL in the question; the column names are different.
It is highly unusual to put the time in without the date. Are you sure you don't want now()/CURRENT_TIMESTAMP?
Don't use escapes around names. They are just cumbersome to read and clunky to write.
Is it possible to add default constraint with synonym or UDF from other database?
Create table TestTable (
ID int identity(1,1),
SData varchar(100),
UserName varchar(100) default [OtherDatabaseName].dbo.fn_Test('value'))
Below is the error message, while I am trying to add default constraint
The name "OtherDatabaseName" is not permitted in this context. Valid
expressions are constants, constant expressions, and (in some
contexts) variables. Column names are not permitted.
We can add UDF from same database but I want add UDF from other database. Because it is used in multiple databases on same server.
And I do not want to create that UDF in all database and prevent duplicate code.
Please let me know if there is any other/better way. Thank you.
Please let me know if there is any other/better way.
One other way would be to use a trigger instead of a default.
I have to create a schema for Oracle and another one for SQL Server. Is it better to use alter table to modify default value (date), or should I use a trigger, since Oracle uses SYSDATE and SQL Server uses GETDATE()
I think generally it would be better use a default value, without using trigger (my opinion is to use triggers only if strictly necessary). Moreover, trigger difference between MSSQL and ORACLE requests more attention than using DEFAULT SYSDATE rather than DEFAULT GETDATE().
On the other hand, default value will be used ONLY if you don't pass any value with insert command: with trigger you can change values as you want.
I'm facing with similar problem (we have two DB version, MSSSQL and ORACLE, for the same application) and in this case we use DEFAULT value for insert date (eg. column DATA_INS) and trigger for update date (column DATA_UPD) of a record.
I've got a new website moved to my server. This website uses PHP and MySQL, and is built very poorly.
The problem is - it needs non-null values inserted into new records where ever I don't specify a value, though the default value is null.
I've been told it has been done on the previous server, but they have no idea how. I'd be glad to receive some help on this.
You could update the default values of the fields of your database to prevent problems using:
ALTER TABLE `table` ALTER `field` SET DEFAULT 'value'
More information on ALTER TABLE for specific fields and parameters can be found in the documentation.
You need to add default values for the columns, either recreate the tables with defaults or alter the table definitions.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
http://dev.mysql.com/doc/refman/5.1/en/create-table.html