forked from shashank-mishra219/SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_Class_1.txt
More file actions
105 lines (78 loc) · 3 KB
/
SQL_Class_1.txt
File metadata and controls
105 lines (78 loc) · 3 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
---Command to create DATABASE
Create DATABASE BigDataBootCamp;
Create DATABASE Test;
---To list down all the databases
Show Databases;
---Command to drop a DATABASE
Drop DATABASE Test;
--- Go inside the particular DATABASE
use BigDataBootCamp;
--- Command to create a TABLE
Create table if not exists employee
(
id int,
name VARCHAR(50)
);
---Command to list down all the TABLES
show tables;
---Command to list down all the TABLES
show create table employee;
--- Command to create a TABLE
Create table if not exists employee
(
id int,
name VARCHAR(50),
salary DOUBLE,
hiring_date DATE
);
--- Syntax 1 To insert data into a TABLE
insert into employee values(1,'Shashank',1000,'2021-09-15');
--- This statement will fail
insert into employee values(2,'Rahul','2021-09-15');
--- Syntax 2 To insert data into a TABLE
insert into employee(salary,name,id) values(2000,'Rahul',2);
--- Insert multiple rows using single insert statement
insert into employee values(3,'Amit',3000,'2021-09-15'),(4,'Niting',3500,'2021-09-16'),(5,'Kajal',4000,'2021-09-20');
--- Use select command to query the data
Select * from employee;
--- Example for Integrity Constraints
Create table if not exists employee_with_constraints
(
id int NOT NULL,
name VARCHAR(50) NOT NULL,
salary DOUBLE,
hiring_date DATE DEFAULT '2021-01-01',
UNIQUE (id),
CHECK (salary > 1000)
);
--- Example 1 for Integrity Constraint failure
--- Exception will be thrown -> Column 'id' cannot be null
insert into employee_with_constraints values(null,'Amit',3000,'2021-09-15');
--- Example 2 for Integrity Constraint failure
--- Exception will be thrown -> Column 'name' cannot be null
insert into employee_with_constraints values(3,null,3000,'2021-09-15');
--- Example 3 for Integrity Constraint failure
--- Exception will be thrown -> Check constraint 'employee_with_constraints_chk_1' is violated.
insert into employee_with_constraints values(1,'Shashank',500,'2021-09-15');
--- Insert corect data
insert into employee_with_constraints values(1,'Shashank',1200,'2021-09-15');
--- Example 4 for Integrity Constraint failure
--- Exception will be thrown -> Duplicate entry '1' for key 'employee_with_constraints.id'
insert into employee_with_constraints values(1,'Amit',1300,'2021-09-28');
--- Example 5 for Integrity Constraint
insert into employee_with_constraints values(2,'Amit',1300,null);
insert into employee_with_constraints(id,name,salary) values(3,'Mukesh',2400);
select * from employee_with_constraints;
--- Add alias name for constraints
Create table if not exists employee_with_constraints_tmp
(
id int NOT NULL,
name VARCHAR(50) NOT NULL,
salary DOUBLE,
hiring_date DATE DEFAULT '2021-01-01',
CONSTRAINT unique_id UNIQUE (id),
CONSTRAINT salary_check CHECK (salary > 1000)
);
--- Example for Integrity Constraint failure with alias name of constraint
--- Exception will be thrown -> Check constraint 'salary_check' is violated.
insert into employee_with_constraints_tmp values(1,'Shashank',500,'2021-09-15');