MySQL - set dynamic default value [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




  • MySQL default value as other field's value

    2 answers




I have table of users in my MySQL database. Is it possible to set default value of column profile_url to value of column Id ?










share|improve this question













marked as duplicate by O. Jones, Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 16:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Your question is difficult to understand. Please consider making an edit to give more details.
    – O. Jones
    Nov 11 at 16:42










  • What is your MySQL server version ? Is the column ID auto increment ?
    – Madhur Bhaiya
    Nov 11 at 16:44






  • 1




    I think in MySQL 8 you can do some things with other column values, have a read of dev.mysql.com/doc/refman/8.0/en/… for more info.
    – Nigel Ren
    Nov 11 at 16:52















up vote
0
down vote

favorite













This question already has an answer here:




  • MySQL default value as other field's value

    2 answers




I have table of users in my MySQL database. Is it possible to set default value of column profile_url to value of column Id ?










share|improve this question













marked as duplicate by O. Jones, Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 16:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    Your question is difficult to understand. Please consider making an edit to give more details.
    – O. Jones
    Nov 11 at 16:42










  • What is your MySQL server version ? Is the column ID auto increment ?
    – Madhur Bhaiya
    Nov 11 at 16:44






  • 1




    I think in MySQL 8 you can do some things with other column values, have a read of dev.mysql.com/doc/refman/8.0/en/… for more info.
    – Nigel Ren
    Nov 11 at 16:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • MySQL default value as other field's value

    2 answers




I have table of users in my MySQL database. Is it possible to set default value of column profile_url to value of column Id ?










share|improve this question














This question already has an answer here:




  • MySQL default value as other field's value

    2 answers




I have table of users in my MySQL database. Is it possible to set default value of column profile_url to value of column Id ?





This question already has an answer here:




  • MySQL default value as other field's value

    2 answers








php mysql database default default-value






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:25









Josef Kotoun

132




132




marked as duplicate by O. Jones, Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 16:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by O. Jones, Nigel Ren php
Users with the  php badge can single-handedly close php questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 11 at 16:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    Your question is difficult to understand. Please consider making an edit to give more details.
    – O. Jones
    Nov 11 at 16:42










  • What is your MySQL server version ? Is the column ID auto increment ?
    – Madhur Bhaiya
    Nov 11 at 16:44






  • 1




    I think in MySQL 8 you can do some things with other column values, have a read of dev.mysql.com/doc/refman/8.0/en/… for more info.
    – Nigel Ren
    Nov 11 at 16:52














  • 1




    Your question is difficult to understand. Please consider making an edit to give more details.
    – O. Jones
    Nov 11 at 16:42










  • What is your MySQL server version ? Is the column ID auto increment ?
    – Madhur Bhaiya
    Nov 11 at 16:44






  • 1




    I think in MySQL 8 you can do some things with other column values, have a read of dev.mysql.com/doc/refman/8.0/en/… for more info.
    – Nigel Ren
    Nov 11 at 16:52








1




1




Your question is difficult to understand. Please consider making an edit to give more details.
– O. Jones
Nov 11 at 16:42




Your question is difficult to understand. Please consider making an edit to give more details.
– O. Jones
Nov 11 at 16:42












What is your MySQL server version ? Is the column ID auto increment ?
– Madhur Bhaiya
Nov 11 at 16:44




What is your MySQL server version ? Is the column ID auto increment ?
– Madhur Bhaiya
Nov 11 at 16:44




1




1




I think in MySQL 8 you can do some things with other column values, have a read of dev.mysql.com/doc/refman/8.0/en/… for more info.
– Nigel Ren
Nov 11 at 16:52




I think in MySQL 8 you can do some things with other column values, have a read of dev.mysql.com/doc/refman/8.0/en/… for more info.
– Nigel Ren
Nov 11 at 16:52












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You could use trigger to implement the feature. For example:



CREATE TRIGGER defaultUrl
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.profile_url = IFNULL(NEW.profile_url, NEW.id);


The expression creates a trigger named defaultUrl that activates before each row inserted into MyTable. If defaultUrl is NULL, the id value is used.








share|improve this answer























  • Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
    – Josef Kotoun
    Nov 12 at 17:20












  • @JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
    – Alexander
    Nov 21 at 2:04




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You could use trigger to implement the feature. For example:



CREATE TRIGGER defaultUrl
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.profile_url = IFNULL(NEW.profile_url, NEW.id);


The expression creates a trigger named defaultUrl that activates before each row inserted into MyTable. If defaultUrl is NULL, the id value is used.








share|improve this answer























  • Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
    – Josef Kotoun
    Nov 12 at 17:20












  • @JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
    – Alexander
    Nov 21 at 2:04

















up vote
0
down vote



accepted










You could use trigger to implement the feature. For example:



CREATE TRIGGER defaultUrl
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.profile_url = IFNULL(NEW.profile_url, NEW.id);


The expression creates a trigger named defaultUrl that activates before each row inserted into MyTable. If defaultUrl is NULL, the id value is used.








share|improve this answer























  • Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
    – Josef Kotoun
    Nov 12 at 17:20












  • @JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
    – Alexander
    Nov 21 at 2:04















up vote
0
down vote



accepted







up vote
0
down vote



accepted






You could use trigger to implement the feature. For example:



CREATE TRIGGER defaultUrl
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.profile_url = IFNULL(NEW.profile_url, NEW.id);


The expression creates a trigger named defaultUrl that activates before each row inserted into MyTable. If defaultUrl is NULL, the id value is used.








share|improve this answer














You could use trigger to implement the feature. For example:



CREATE TRIGGER defaultUrl
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.profile_url = IFNULL(NEW.profile_url, NEW.id);


The expression creates a trigger named defaultUrl that activates before each row inserted into MyTable. If defaultUrl is NULL, the id value is used.









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 3:07

























answered Nov 11 at 16:50









Alexander

2,99261327




2,99261327












  • Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
    – Josef Kotoun
    Nov 12 at 17:20












  • @JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
    – Alexander
    Nov 21 at 2:04




















  • Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
    – Josef Kotoun
    Nov 12 at 17:20












  • @JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
    – Alexander
    Nov 21 at 2:04


















Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
– Josef Kotoun
Nov 12 at 17:20






Thanks for help, it seems it should work, but i have 1 more problem with it. Can i somehow use it if the ID column is autoincrement? When I use any other column it works, but when I want to use value of ID it changes the value to '0' everytime
– Josef Kotoun
Nov 12 at 17:20














@JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
– Alexander
Nov 21 at 2:04






@JosefKotoun , I'm not sure, but you could try to use AFTER value for trigger time instead of BEFORE. Or use autoincrement value from information_schema database.
– Alexander
Nov 21 at 2:04





Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly