zmień kontrast font size: A A A
rss polski
you are at Encyclopedia of SQL >> DEFAULT

DEFAULT

It is used to set default value for specified column. There is possibility of setting default value while creating new table or for existing table with ALTER keyword.

Syntax for setting default value during creating new table


CREATE `table_name`
(
`column_name1` data_type [DEFAULT 'default_value'],
`column_name2` data_type [DEFAULT 'default_value']
)

Syntax for setting default value for existing table


ALTER TABLE `table_name`
ALTER [COLUMN] `column_name`
SET DEFAULT 'default_value'

 

Syntax for deleting DEFAULT for existing table


ALTER TABLE `table_name`
ALTER [COLUMN] `column_name`
DROP DEFAULT 'default_value'

In MySQL after ALTER there heve to be placed name of modified column, in SQL Server / Oracle / MS Access before giving column name there is used ALTER COLUMN keyword.

Example

Create `people` table for name, surname and visitors counter with default value equals zero.

query


CREATE TABLE `people`
{
`name` text
`surname` text
`counter` int DEFAULT 0
}

Now add data of Mr Jan Kowalski to existing table.

query


INSERT  INTO `people` (`name`,`surname`)
VALUES ('Jan','Kowalski')
}

result

namesurnamecounter
JanKowalski0

[ wróć na górę strony ]