SQL CREATE ALTER DROP TABLE: Difference between revisions
Jump to navigation
Jump to search
Line 50: | Line 50: | ||
|} | |} | ||
===Primary Key=== | ===Primary Key=== | ||
<syntaxhighlight lang='sql'> | |||
ALTER TABLE sometable ADD PRIMARY KEY (somecolumn) <table_name>_pkey; | |||
</syntaxhighlight> | |||
The name of the primary key constraint is optional, and if not specified, it will be generated according to the <code><table_name>_pkey</code> pattern. | |||
<syntaxhighlight lang='sql'> | |||
ALTER TABLE sometable DROP CONSTRAINT [IF EXISTS] constraint_name [RESTRICT|CASCADE]; | |||
</syntaxhighlight> | |||
:{| class="wikitable" style="text-align: left;" | :{| class="wikitable" style="text-align: left;" | ||
|- | |- | ||
| [[PostgreSQL_DDL_Operations# | | [[PostgreSQL_DDL_Operations#Primary_Key|PostgreSQL]] || [[MySQL_DDL_Operations|MySQL]] | ||
|- | |- | ||
|} | |} |
Revision as of 19:35, 23 May 2024
Internal
Overview
CREATE
CREATE [TEMPORARY] TABLE person
( id smallint,
name varchar(30) NOT NULL,
CONSTRAINT pk_person PRIMARY KEY (id)
);
If the TEMPORARY
keyword is used, the created table will be temporary instead of permanent.
ALTER
ALTER TABLE person [...]
Add/Remove a Column
ALTER TABLE person ADD COLUMN birthday DATE;
ALTER TABLE person DROP COLUMN birthday;
Add/Remove a Constraint
NOT NULL
ALTER TABLE sometable ALTER COLUMN somecolumn SET NOT NULL;
ALTER TABLE sometable ALTER COLUMN somecolumn DROP NOT NULL;
Primary Key
ALTER TABLE sometable ADD PRIMARY KEY (somecolumn) <table_name>_pkey;
The name of the primary key constraint is optional, and if not specified, it will be generated according to the <table_name>_pkey
pattern.
ALTER TABLE sometable DROP CONSTRAINT [IF EXISTS] constraint_name [RESTRICT|CASCADE];
DROP
DROP TABLE person [...]