Home  >  Article  >  Backend Development  >  Related knowledge about SQL DEFAULT constraints

Related knowledge about SQL DEFAULT constraints

jacklove
jackloveOriginal
2018-05-08 11:00:491555browse

SQL DEFAULT Constraints are very important in php, this article will explain them.

SQL DEFAULT constraint

DEFAULT constraint is used to insert a default value into a column.

If no other value is specified, the default value will be added to all new records.

SQL DEFAULT Constraint on CREATE TABLE

The following SQL creates a DEFAULT constraint for the "City" column when the "Persons" table is created:

My SQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) ,
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)

By using something like GETDATE() Function, DEFAULT constraints can also be used to insert system values:

CREATE TABLE Orders
(
Id_O int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
OrderDate date DEFAULT GETDATE()
)

SQL DEFAULT Constraint on ALTER TABLE

If created for the "City" column when the table already exists DEFAULT constraint, please use the following SQL:

MySQL:

ALTER TABLE Persons
ALTER City SET DEFAULT 'SANDNES'

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'SANDNES'

Revoke DEFAULT constraint

If you need to revoke DEFAULT constraint, please use the following SQL:

MySQL:

ALTER TABLE Persons
ALTER City DROP DEFAULT

This article is related to DEFAULT constraint Explanation, for more learning materials, please pay attention to the php Chinese website to view.

Related recommendations:

Related knowledge about SQL CHECK constraints

Related knowledge about SQL UNIQUE constraints

Related knowledge about SQL NOT NULL constraints

The above is the detailed content of Related knowledge about SQL DEFAULT constraints. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn