-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathddl-commands.sql
More file actions
64 lines (48 loc) · 1.26 KB
/
ddl-commands.sql
File metadata and controls
64 lines (48 loc) · 1.26 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
-- Creating database
create database cheatsheet;
-- Viewing the databases
show databases;
-- using the database
use cheatsheet;
create table student
(
student_id int primary key, -- Setting primary key(1st method)
first_name varchar(50),
last_name varchar(50),
class_number int,
age int,
salary real
);
create table class
(
class_number int,
class_name varchar(50),
class_location varchar(50),
st_id int,
primary key(class_number) -- Setting primary key(2nd method)
);
-- veiwing tables in the selected database
show tables;
-- print the structure of the table
describe student;
desc student;
show columns in student;
-- renaming of table
rename table student to student_table;
alter table student_table rename to student;
-- reanaming a column
alter table student change column student_id st_id int;
-- add a constraint to column
alter table student change column first_name first_name varchar(50) not null;
-- add column
alter table student add column salary real;
-- drop a column
alter table student drop column salary;
-- modify the datatype
alter table student modify column salary int;
-- truncate a table
truncate student;
-- drop table
drop table class;
-- drop database
drop database cheatsheet;