-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint.sql
More file actions
153 lines (109 loc) · 2.83 KB
/
constraint.sql
File metadata and controls
153 lines (109 loc) · 2.83 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
create database const_db;
use const_db;
-- to access the database
SELECT DATABASE();
-- DDL ( data defination language) create
CREATE TABLE IF NOT EXISTS test1 (
id INT,
age INT
);
-- DML (data manipulation language) insert
INSERT INTO test1 VALUES
(101,32),
(102,NULL);
-- to fetch the data
SELECT * FROM test1;
-- Constraints
-- Constraints are rules applied on table columns to prevent invalid data in the table
CREATE TABLE IF NOT EXISTS test3 (
id INT not null ,
age INT
);
INSERT INTO test3 VALUES (102,32);
INSERT INTO test3 VALUES (NULL,40);
SELECT * FROM test3;
-- ==============================
-- PRIMARY KEY
-- Primary key NULL nahi ho sakti
-- aur duplicate bhi nahi ho sakti
-- ==============================
CREATE TABLE IF NOT EXISTS tests6 (
id INT PRIMARY KEY,
age INT
);
INSERT INTO tests6 VALUES (80,23);
-- Error aayega because primary key NULL nahi ho sakti
-- INSERT INTO tests6 VALUES (NULL,27);
SELECT * FROM tests6;
-- ==============================
-- COMPOSITE PRIMARY KEY
-- multiple columns milkar primary key banati hain
-- ==============================
CREATE TABLE IF NOT EXISTS tests8 (
id INT,
age INT,
salary INT,
PRIMARY KEY (id, age)
);
INSERT INTO tests8 VALUES
(1,50,5000000),
(2,40,20000),
(1,51,6000000),
(3,50,100);
SELECT * FROM tests8;
DESC tests8;
-- Super key = saare possible unique combinations
-- Composite key = smallest combination of columns
-- jo uniquely identify kare
-- ==============================
-- CHECK Constraint
-- ==============================
CREATE TABLE IF NOT EXISTS tests9 (
id INT,
age INT CHECK (age > 18)
);
INSERT INTO tests9 VALUES (1,50);
-- Error aayega because age 18 se choti hai
-- INSERT INTO tests9 VALUES (1,10);
SELECT * FROM tests9;
-- ==============================
-- CHECK with BETWEEN
-- ==============================
CREATE TABLE IF NOT EXISTS test10 (
id INT,
age INT CHECK(age BETWEEN 18 AND 22)
);
INSERT INTO test10 VALUES (1,20);
-- Error aayega because 10 range ke bahar hai
-- INSERT INTO test10 VALUES (1,10);
SELECT * FROM test10;
-- ==============================
-- FOREIGN KEY
-- ==============================
CREATE TABLE class (
cname VARCHAR(20) PRIMARY KEY
);
INSERT INTO class VALUES
('10'),
('8'),
('9');
SELECT * FROM class;
-- foreign key : A Foreign Key creates a relationship between two tables.
-- It refers to the primary key of another table.
CREATE TABLE student (
id INT,
name VARCHAR(20),
cname VARCHAR(20),
FOREIGN KEY (cname) REFERENCES class(cname)
);
INSERT INTO student VALUES
(1,'naina','10'),
(2,'abhi','9');
SELECT * FROM student;
-- ==============================
-- SQL Data Types
-- INT -> Integer values
-- VARCHAR(n) -> String values
-- DATE -> Date values
-- FLOAT -> Decimal numbers
-- ==============================