-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraints.sql
70 lines (57 loc) · 1.43 KB
/
Constraints.sql
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
65
66
67
68
69
70
-- NOT NULL WITHOUT DEFAULT VALUES
CREATE TABLE NOT_NULL_TEST(
COL1 INTEGER,
COL2 INTEGER NOT NULL
);
INSERT INTO NOT_NULL_TEST VALUES (1, NULL);
INSERT INTO NOT_NULL_TEST(COL1) VALUES (1);
INSERT INTO NOT_NULL_TEST VALUES (1,1);
UPDATE NOT_NULL_TEST SET COL2 = NULL;
-- NOT NULL CONSTRAINT WITH DEFAULT CONSTRAINT
CREATE TABLE NOT_NULL_DEFAULT(
COL1 INTEGER,
COL2 INTEGER NOT NULL DEFAULT 5
);
INSERT INTO NOT_NULL_DEFAULT (COL1) VALUES (1);
SELECT * FROM NOT_NULL_DEFAULT;
-- UNIQUE CONSTRAINT DEFINITION TYPE-1
CREATE TABLE UNIQUETEST(
COL1 INTEGER UNIQUE
);
INSERT INTO UNIQUETEST VALUES (1);
INSERT INTO UNIQUETEST VALUES (1);
INSERT INTO UNIQUETEST VALUES (NULL);
INSERT INTO UNIQUETEST VALUES (NULL);
-- UNIQUE CONSTRAINT DEFINITION TYPE-2
CREATE TABLE UNIQUETEST2 (
COL1 INTEGER,
COL2 INTEGER,
UNIQUE (COL1, COL2)
);
-- PRIMARY KEY CONSTRAINT DEFINITION TYPE-1
CREATE TABLE PRIMARYTEST (
COL INTEGER PRIMARY KEY
);
-- PRIMARY KEY CONSTRAINT DEFINITION TYPE-2
CREATE TABLE PRIMARYTEST2 (
COL1,
COL2,
PRIMARY KEY(COL1, COL2)
);
CREATE TABLE STATENAME (
CODE CHAR(2) PRIMARY KEY,
NAME CHAR(30)
);
INSERT INTO STATENAME VALUES ('AL','ALABAMA');
CREATE TABLE CUSTOMER (
CUSTOMER_ID INTEGER,
NAME CHAR(30),
TELEPHONE CHAR(30),
STREET CHAR(30),
CITY CHAR(25),
STATE CHAR(2) REFERENCES STATENAME,
ZIPCODE CHAR(10),
COUNTRY CHAR(20)
);
INSERT INTO CUSTOMER (STATE) VALUES ('AL');
INSERT INTO CUSTOMER (STATE) VALUES ('XX');