-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic001.sql
86 lines (61 loc) · 1.91 KB
/
basic001.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
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');
SELECT * FROM student;