Questions tagged [default-value]

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

Filter by
Sorted by
Tagged with
17 votes
2 answers
19k views

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 ...
Jack Douglas's user avatar
  • 39.7k
15 votes
3 answers
25k views

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) | +===========+=========+=...
Joshua Burns's user avatar
2 votes
3 answers
5k views

PostgreSQL execute a default query when no row are returned

I need to calculate the average score of a list of userId. I have a table user_place_score: (user_id, place_id, score) Sometimes I do not have the score (no row for (user_id,place_id) in the table) ...
Alexis's user avatar
  • 279
131 votes
3 answers
227k views

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's user avatar
6 votes
1 answer
4k views

Why mysql throws "invalid default value for field" only when two or more timestamp fields exist in the create statement?

In mysql 5.7.28 I create a table like this: create table t1 ( id int not null, d1 timestamp) engine=innodb; which works fine and creates d1 as non-null with default value current_timetamp,on update ...
Marinos An's user avatar
5 votes
1 answer
4k views

How to set column DEFAULT using value of a variable

In a script for setting up a database which uses random UUIDs for primary keys, I have: CREATE TABLE alpha (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), value INT); CREATE TABLE beta (aref UUID ...
Mark K Cowan's user avatar
3 votes
2 answers
4k views

Inserting dummy data into an empty table having a primary key integer field GENERATED ALWAYS AS IDENTITY

I'm trying to generate dummy tables with dummy data for testing performance with some SQL tasks. Based on this old thread: Is there a way to insert multiple rows into a table with default values for ...
s.k's user avatar
  • 342
3 votes
1 answer
2k views

How can I get MySQL to use the value returned by a function as a default?

I have a pre-existing table that has a column to that can hold a UUID. I'd like to be able to define a default such that new rows coming in (from legacy systems) will still get a UUID defined, via ...
Rowland Shaw's user avatar
1 vote
1 answer
1k views

How do I an add a DEFAULT on a column if the table already exists?

I want to add a DEFAULT for a timestamp column in PostgreSQL. With SQL Server you use ALTER TABLE ADD CONSTRAINT You can see the example in their docs, ALTER TABLE dbo.doc_exz ADD CONSTRAINT ...
Venki's user avatar
  • 21
0 votes
1 answer
314 views

What is the default column name given to a TABLE() type with a single column

CREATE OR REPLACE TYPE MISSING_KEYS AS TABLE OF VARCHAR2(256); SELECT [¿what goes here?] as missing_key FROM TABLE (MISSING_KEYS('A','B','C','D')) What is the default column name that should be in [¿...
user avatar