Questions tagged [default-value]

For questions relating to DBMS-supplied values that are generated when none are explicitly provided.

Often a DBMS can be set up to provide values during reads or writes when none is explicitly stated in the program code.

A common use case is in relational databases, where a default constraint can be defined for a column. Any INSERT statement which does not list that column explicitly will result in the declared default value being stored.

90 questions
140
votes
3 answers

Default value for UUID column in Postgres

In Postgres 9.x, for a column of type UUID, how do I specify a UUID to be generated automatically as a default value for any row insert?
Basil Bourque
  • 11,188
  • 20
  • 63
  • 96
55
votes
5 answers

How can I use a default value in a Select query in PostgreSQL?

I would like to use a default value for a column that should be used if no rows is returned. Is that possible in PostgreSQL? How can I do it? Or is there any other way I can solve this? E.g. something like this: SELECT MAX(post_id) AS max_id DEFAULT…
Jonas
  • 33,945
  • 27
  • 62
  • 64
25
votes
2 answers

Can't default date to CURRENT_TIMESTAMP in MySQL 5.5

I am not able to set Current_timestamp as default value. My Mysql version is 5.5.47. Query is ALTER TABLE `downloads` ADD `date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ; While it is working fine on my local DB with mysql V5.6.56.
urfusion
  • 353
  • 1
  • 4
  • 9
20
votes
2 answers

DEFAULT CONSTRAINT, worth it?

I usually design my databases following next rules: Nobody else than db_owner and sysadmin have access to the database tables. User roles are controlled at application layer. I usually use one db role to grant access to the views, stored…
19
votes
2 answers

Why can't I use NEWSEQUENTIALID() as the default value for my column?

I'm trying to create a table in Management Studio and after reading about the new (from SQL 2005 on) NEWSEQUENTIALID() function, thought I'd give it a go. This is what I'm doing: But it's not letting me. The error message I get is: 'FormTemplate…
immutabl
  • 809
  • 2
  • 7
  • 16
19
votes
2 answers

How to create column in DB with default value random string

Can I create column in DB table (PostgreSQL) which have default value random string, and how ? If is not possible, please let me know that.
tasmaniski
  • 1,195
  • 4
  • 13
  • 16
17
votes
2 answers

Is there a way to insert multiple rows into a table with default values for all columns?

I can insert multiple rows into a table with default values for all columns the RBAR way: create table course(course_id serial primary key); do $$ begin for i in 1..100000 loop insert into course default values; end loop; end;$$; Is there…
Jack Douglas
  • 40,517
  • 16
  • 106
  • 178
15
votes
3 answers

Duplicate row with Primary Key in PostgreSQL

Assume I have a table as follows named people, where id is a Primary Key: +-----------+---------+---------+ | id | fname | lname | | (integer) | (text) | (text) | +===========+=========+=========+ | 1 | Daniel | Edwards | | 2 …
Joshua Burns
  • 625
  • 1
  • 7
  • 11
13
votes
1 answer

Add DEFAULT to existing column

In Microsoft SQL server, how can I add/change the default on an existing table column using T-SQL?
Russell Steen
  • 362
  • 1
  • 2
  • 8
12
votes
2 answers

Is DEFAULT NULL and 'nullable value without default' completely the same?

ALTER TABLE test_table ADD COLUMN a DEFAULT NULL; vs. ALTER TABLE test_table ADD COLUMN a; Both columns will set NULL if column a is not specified. As far as I know, if I add a column into a table with a default value in production database, it…
SangminKim
  • 359
  • 2
  • 7
  • 15
11
votes
3 answers

SQL set allowed values for a column

I want to make an ALTER TABLE expression which adds a new column and sets a default value and additionaly defines the allowed values for that column. It's a text column, and allowed should be only 'value1', 'value2' and 'value3'. Default should be…
Valentino Ru
  • 213
  • 1
  • 2
  • 6
11
votes
3 answers

Return the uniqueidentifier generated by a default on insert

Goal Retrieve the latest guid value in real time after you have inserted the value in the table Problem Don't know how to do it Info The code should only specify new values for address and zipcode There can be lots of data in the…
KLN
  • 331
  • 1
  • 4
  • 10
10
votes
2 answers

PostgreSQL, what is the hostname address of my default database?

I just created a PostgreSQL database on my laptop (following these instructions), which is running Linux CentOS 7. Now I would like to understand what is the hostname address of my default database. I thought it was localhost but it's not. I want to…
DavideChicco.it
  • 327
  • 2
  • 5
  • 13
7
votes
1 answer

Is it safe to use default value with not null when adding a new column?

We have a Rails app powered by Postgresql v11.4 where I want to add a new column with a default value and a not null constraint like below: ALTER TABLE "blogs" ADD "published" boolean DEFAULT FALSE NOT NULL I know adding a new column with a default…
ogirginc
  • 275
  • 2
  • 3
  • 10
7
votes
1 answer

Cannot alter MYSQL Table (stuck in a invalid default value loop)

Some how I have 2 columns with incorrect default values. Not sure how it got created in the first place as I imported the db from another source. Problem now is, I cannot make any changes to the table cause MYSQL will complain: MySQL said: Invalid…
resting
  • 173
  • 1
  • 3
1
2 3 4 5 6