-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic002.sql
154 lines (94 loc) · 3.04 KB
/
basic002.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
CREATE DATABASE STUDENT_INFO;
USE DATABASE STUDENT_INFO;
--a primary key is automatically not null and unique
create table
animals (
student_id INT PRIMARY KEY,
name VARCHAR(50)
);
INSERT INTO animals VALUES(123, 'Rahul Gupta');
select * from animals;
-- WRITE SQL KEYWORDS IN CAPITALS
-- creating a table
CREATE TABLE
student (
student_id INT PRIMARY KEY,
name VARCHAR(20),
major VARCHAR(20)
);
--gives a description about the table
DESCRIBE student;
--deletes the entire table
DROP TABLE student;
--adds a new column
ALTER TABLE student ADD gpa DECIMAL(3,2);
--drops a new column
ALTER TABLE student DROP COLUMN gpa;
--inserting values into the table
INSERT INTO student VALUES(1, 'jACK', 'Biology');
INSERT INTO student VALUES(2, 'Chris', 'Science');
INSERT INTO student VALUES(3, 'Will', 'Computer Science');
--insert into specific columns
--you cannot insert values with duplicate primary keys
--INSERT INTO student VALUES(4, NULL,'IAN');
INSERT INTO student VALUES(5, 'Mike', NULL);
INSERT INTO student(student_id, major) VALUES(6, 'Mike');
INSERT INTO student(student_id, name) VALUES(1, 'Rachel');
--retrieve all the values from the table
SELECT * FROM student;
DROP TABLE student;
--creating a table in a way it is easier for us to insert stuff
--name cannot be null
--every major needs to be unique
CREATE TABLE
student (
student_id INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
major VARCHAR(20) UNIQUE
);
--setting a default value
CREATE TABLE
student (
student_id INT PRIMARY KEY,
name VARCHAR(20),
major VARCHAR(20) DEFAULT 'undecided'
);
--AUTO INCREMENTING PRIMARY KEY FOR US
CREATE TABLE
student (
student_id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20),
major VARCHAR(20) DEFAULT 'undecided',
PRIMARY KEY (student_id)
);
INSERT INTO student(name, major) values('Jack', 'Biology');
INSERT INTO student(name, major) values('kate', 'science');
INSERT INTO student(name, major) values('middleton', 'physco');
INSERT INTO student(name, major) values('nick', 'chemistry');
INSERT INTO student(name, major) values('Dua', 'science');
SELECT * FROM student;
--updating and deleting specific pieces of information
UPDATE student SET major = 'Bio' WHERE major = 'Biology';
UPDATE student SET major = 'CompSci' WHERE major = 'science';
--based on id
UPDATE student SET major = 'Chem' WHERE student_id = 4;
--based on two or more conditions
UPDATE student
SET major = 'BioChemistry'
WHERE
major = 'Bio'
OR major = 'Chem';
--setting multiple values at once and using conditionals
UPDATE student
SET
name = 'Tom',
major = 'Undecided'
WHERE student_id = 3;
--get rid of where -> applies to every single row
UPDATE student SET major = 'Undecided';
--delete a specific row or a group of rows
--will delete everything without where
DELETE FROM student;
DELETE FROM student where student_id = 5;
SELECT * FROM student;
DELETE FROM student where name = 'Tom' AND major = 'Undecided';