forked from harsh432004/Complete-Data-Science-with-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 14-primary key.sql
174 lines (139 loc) · 3.9 KB
/
Day 14-primary key.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
CREATE DATABASE test;
-- DROP DATABASE test;
CREATE TABLE harsh;
CREATE TABLE employee(
name char(10),
salary int,
phone int(10),
email varchar(100),
department char
);
create table employee1 as select salary, department from employee;
-- drop ->Delete the existing table
-- Syntax: drop table tablename;
drop table employee1;
-- truncate -> delete all data inside table but dosnt delete data itself
-- syntax -> truncate table tablename;
truncate table employee1;
desc employee;
-- update-> Alter the table
-- 1. add the column in table
-- Syntax: Alter table tablename;
-- add col_name datatype(max)
alter table employee
add maritial_status char;
-- 2. Delete the column in table
-- Syntax: Alter table tablename;
-- drop col_name datatype(max)
alter table employee
drop maritial_status;
-- 3. change the datatype of that column in table
-- Syntax: Alter table tablename;
-- drop col_name datatype(max)
alter table employee
drop maritial_status;
-- Syntax: Alter table tablename;
-- modify column col_name datatype(max)
Alter table employee
modify column salary varchar(100);
CREATE database cartoon;
USE cartoon;
create table doremon(
iD int,
name char,
address varchar(100),
age int
);
DROP table doremon;
-- SYNTAX for Constrains
-- CREATE TABLE table_name(
-- col1 datatype constrain,
-- col2 datatype constrain,
-- col3 datatype constrain,
-- )
create table doremon(
iD int NOT NULL,
name char,
address varchar(100),
age int
);
Alter table doremon
Modify column name char not null;
Alter table doremon
Modify column age char NOT NULL UNIQUE;
-- for oracle db
create table shinchan(
iD int NOT NULL UNIQUE,
name char,
address varchar(100),
age int
);
-- for mysql workbench
create table shinchan(
iD int NOT NULL,
name char,
address varchar(100),
age int,
primary key (id, name,address, age)
);
-- Primary = Unique + NOT NULL
-- A table can have multiple unique constrain but only 1 primary key
-- PK for single field
Alter table shinchan
Add primary key (age);
-- PK for multiple field
Alter table shinchan
Add Constraint SHINPR primary key (age, address, Name );
Drop table doremon;
Drop table shinchan;
-- Data -> Rows
Create table employee(
id int,
name char,
salary int not null,
Experience int not null,
primary key (ID)
);
-- Insert
Insert into employee(name, id, salary, experience)
Values("Harsh",1,100,2);
alter table employee
modify column name char(100);
SELECT * FROM employee;
Insert into employee(name, id, salary, experience)
Values("Harsh",1,10000,2);
Insert into employee(name, id, salary, experience)
Values("Sachin",2,1000000,2);
Insert into employee(name, id, salary, experience)
Values("Vaishali",3,50000,2);
Insert into employee(name, id, salary, experience)
Values("Bhupendra",4,10000,2);
Insert into employee(name, id, salary, experience)
Values("Charu",5,7000,2);
Insert into employee(name, id, salary, experience)
Values("Surya",6,1000,2);
Insert into employee(name, id, salary, experience)
Values("Bhuvan",7,1000,2);
Insert into employee(name, id, salary, experience)
Values("Labdhi",8,1000,4);
Insert into employee(name, id)
Values(9,"Hitesh");
SELECT * from employee;
SELECT name, salary from employee
SELECT name, salary from employee
Select distinct experience from employee;
Select salary from employee
where salary>100
>,<, >=, <=, =, != or <>, between, like
Text values should always have quotes
AND, OR, Not <- operators
UPDATE employee
set name = "Khushi"
where id = 8;
-- constrains -> NOT NULL, UNIQUE, PR
-- TABLE ->
C -> Create a data -> insert into
R -> select *(all) col from table_name, where,
And, OR, NOT, BETWEEN, LIKE, DISTINCT
U-> UPDATE THE DATA
D-> DELETE THE DATA