Search
 
SCRIPT & CODE EXAMPLE
 

SQL

adding a default constraint to an existing column in sql

--Altering an existing column to add a default constraint:
ALTER TABLE {TABLE NAME}
ADD CONSTRAINT {CONSTRAINT NAME}
DEFAULT {DEFAULT VALUE} FOR {EXISTING COLUMN NAME}

--Adding a new column, with default value, to an existing table:
ALTER TABLE {TABLE NAME}
ADD {COLUMN NAME} {DATA TYPE} {NULL | NOT NULL}
CONSTRAINT {CONSTRAINT NAME} DEFAULT {DEFAULT VALUE}

--Dropping a contraint:
ALTER TABLE {TABLE NAME}
DROP CONSTRAINT {CONSTRAINT NAME}
Comment

SQL DEFAULT Constraint With Alter Table

#SQL Server

ALTER TABLE College
ADD CONSTRAINT country_default
DEFAULT 'US' FOR college_country;

#PostgreSQL

ALTER TABLE College
ALTER COLUMN college_code SET DEFAULT 'US';

#MySQL

ALTER TABLE College
ALTER college_country SET DEFAULT 'US';

#Oracle

ALTER TABLE College
MODIFY college_country DEFAULT 'US';
Comment

SQL DEFAULT Constraint

CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    City varchar(255) DEFAULT 'Sandnes'
);
Comment

PREVIOUS NEXT
Code Example
Sql :: select milliseconds mysql 
Sql :: postgre describe table 
Sql :: oracle revoke grant 
Sql :: sql select duplicates based on two columns 
Sql :: difference between super key and candidate key 
Sql :: sql table 
Sql :: SQL Server lock table example 
Sql :: oracle apex warning message 
Sql :: spring data.sql table not found 
Sql :: what is delimiter in mysql 
Sql :: in mysql workbench contnent not feching 
Sql :: sql server datetime to string 
Sql :: ascending order and where in sql 
Sql :: postgres select duplicate columns 
Sql :: sql date format dd-mm-yyyy 
Sql :: how to uninstall postgresql 13 on mac 
Sql :: postgresql function 
Sql :: postgresql import a database of gzip 
Sql :: mysql get first n characters of string 
Sql :: count in sql and diff 
Sql :: inner join distinct 
Sql :: current date in sql 
Sql :: sql order by multiple columns 
Sql :: sql replace single quote 
Sql :: SQL: get date difference in minutes 
Sql :: sql server remove primary key without dropping table 
Sql :: all_dependencies 
Sql :: sql alter table order by 
Sql :: sql in 
Sql :: get initials name in sql 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =