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.

85 questions
106
votes
2 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
  • 8,776
  • 14
  • 46
  • 78
47
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
  • 29,165
  • 26
  • 57
  • 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…
5arx
  • 809
  • 2
  • 7
  • 16
16
votes
1 answer

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
  • 37,076
  • 14
  • 93
  • 173
15
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,005
  • 4
  • 11
  • 16
12
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
  • 352
  • 1
  • 2
  • 8
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

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
  • 565
  • 1
  • 5
  • 11
10
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
  • 321
  • 1
  • 4
  • 10
8
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
  • 307
  • 2
  • 5
  • 12
6
votes
2 answers

Can you use an inserted variable in a default constraint

Working in SQL Server, let's say I have a database with two tables: events and competitors. events has the following columns: [event_id] [event_name] [winner_id] [loser_id] competitors has the following columns: [comp_id] [comp_name] I wanted to…
6
votes
1 answer

Find out which columns must be specified in insert-statement

Sometimes when inserting data in a table with many columns it could be useful to know which columns must be specified if the insert-statement shouldn't fail. I wrote this query to find out which columns are not nullable, identity, computed,…
Andreas Ågren
  • 644
  • 4
  • 11
5
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
  • 269
  • 1
  • 4
  • 11
1
2 3 4 5 6