-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathddl-commands.sql
64 lines (48 loc) · 1.29 KB
/
ddl-commands.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
-- Creating database
create database cheatsheet;
-- Viewing the databases
show databases;
-- using the database
use cheatsheet;
create table employee
(
employee_id int primary key, -- Setting primary key(1st method)
first_name varchar(50),
last_name varchar(50),
dept_number int,
age int,
salary real
);
create table department
(
dept_number int,
dept_name varchar(50),
dept_location varchar(50),
emp_id int,
primary key(dept_number) -- Setting primary key(2nd method)
);
-- veiwing tables in the selected database
show tables;
-- print the structure of the table
describe employee;
desc employee;
show columns in employee;
-- renaming of table
rename table employee to employee_table;
alter table employee_table rename to employee;
-- reanaming a column
alter table employee change column employee_id emp_id int;
-- add a constraint to column
alter table employee change column first_name first_name varchar(50) not null;
-- add column
alter table employee add column salary real;
-- drop a column
alter table employee drop column salary;
-- modify the datatype
alter table employee modify column salary int;
-- truncate a table
truncate employee;
-- drop table
drop table department;
-- drop database
drop database cheatsheet;