-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.html
More file actions
64 lines (53 loc) · 2.04 KB
/
default.html
File metadata and controls
64 lines (53 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!-- SECCIÓN DEFAULT -->
<div id="default-section">
<div id="adsense-container" style="width: 350px; position: absolute; left: 0px;"></div>
<div class="inner-modal">
<div class="inner" style="padding: 0px;">
<h1>SQL DEFAULT Constraint</h1>
<p>The <strong>DEFAULT</strong> constraint is used to set a default value for a column.</p>
<p>The default value will be added to all new records if no other value is specified.</p>
<h2>DEFAULT on CREATE TABLE</h2>
<p>Set a default value for the "City" column when creating the "Persons" table:</p>
<pre><code class="language-sql">-- MySQL / SQL Server / Oracle / MS Access
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);</code></pre>
<p>DEFAULT can also use system values like <code>current_timestamp</code>:</p>
<pre><code class="language-sql">-- MySQL / SQL Server / Oracle / MS Access
CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT current_timestamp
);
-- Example insert
INSERT INTO Orders (ID, OrderNumber) VALUES (1,1);
SELECT * FROM Orders;</code></pre>
<h2>DEFAULT on ALTER TABLE</h2>
<p>Create a DEFAULT constraint on an existing column:</p>
<pre><code class="language-sql">-- MySQL
ALTER TABLE Persons
ALTER City SET DEFAULT 'Sandnes';
-- SQL Server
ALTER TABLE Persons
ADD CONSTRAINT df_City
DEFAULT 'Sandnes' FOR City;
-- MS Access
ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'Sandnes';
-- Oracle
ALTER TABLE Persons
MODIFY City DEFAULT 'Sandnes';</code></pre>
<h2>Drop a DEFAULT Constraint</h2>
<pre><code class="language-sql">-- MySQL
ALTER TABLE Persons
ALTER City DROP DEFAULT;
-- SQL Server / Oracle / MS Access
ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;</code></pre>
</div>
</div>
</div>