Skip to content

Commit a68860d

Browse files
committed
added readme and ddl commands
0 parents  commit a68860d

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# MySQL cheatsheet
2+
3+
I have even uploaded the .sql file which you can run and directly run them in the sql prompt.
4+
5+
### General Commands
6+
To run sql files
7+
```sql
8+
source <filename>.sql;
9+
```
10+
11+
## Data Definition Language (DDl)
12+
13+
#### Create Database
14+
```sql
15+
create database cheatsheet;
16+
```
17+
18+
#### Use Database
19+
```sql
20+
use cheatsheet;
21+
```
22+
23+
#### Show Databases
24+
```sql
25+
show databases;
26+
```
27+
28+
#### Create Table
29+
```sql
30+
create table employee
31+
(
32+
employee_id int primary key, -- Setting primary key(1st method)
33+
first_name varchar(50),
34+
last_name varchar(50),
35+
dept_number int,
36+
age int,
37+
salary real
38+
);
39+
40+
create table department
41+
(
42+
dept_number int,
43+
dept_name varchar(50),
44+
dept_location varchar(50),
45+
emp_id int,
46+
primary key(dept_number) -- Setting primary key(2nd method)
47+
);
48+
```
49+
50+
#### Show Tables
51+
```sql
52+
show tables;
53+
```
54+
55+
#### Describe Table
56+
```sql
57+
describe employee;
58+
desc employee;
59+
show columns in employee;
60+
```
61+
62+
#### Rename Table
63+
```sql
64+
rename table employee to employee_table;
65+
alter table employee_table rename to employee;
66+
```
67+
68+
#### Renaming Column
69+
```sql
70+
alter table employee change column employee_id emp_id int;
71+
```
72+
73+
#### Add Constraint to Column
74+
```sql
75+
alter table employee change column first_name first_name varchar(50) not null;
76+
```
77+
78+
#### Add Column
79+
```sql
80+
alter table employee add column salary real;
81+
```
82+
83+
#### Drop Column
84+
```sql
85+
alter table employee drop column salary;
86+
```
87+
88+
#### Modify the Datatype of column
89+
```sql
90+
alter table employee modify column salary int;
91+
```
92+
93+
#### Truncate Table
94+
```sql
95+
truncate employee;
96+
```
97+
98+
#### Drop Table
99+
```sql
100+
drop table department;
101+
```
102+
103+
#### Drop Database
104+
```sql
105+
drop database cheatsheet;
106+
```

ddl-commands.sql

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
-- Creating database
3+
create database cheatsheet;
4+
5+
-- Viewing the databases
6+
show databases;
7+
8+
-- using the database
9+
use cheatsheet;
10+
11+
create table employee
12+
(
13+
employee_id int primary key, -- Setting primary key(1st method)
14+
first_name varchar(50),
15+
last_name varchar(50),
16+
dept_number int,
17+
age int,
18+
salary real
19+
);
20+
21+
create table department
22+
(
23+
dept_number int,
24+
dept_name varchar(50),
25+
dept_location varchar(50),
26+
emp_id int,
27+
primary key(dept_number) -- Setting primary key(2nd method)
28+
);
29+
30+
-- veiwing tables in the selected database
31+
show tables;
32+
33+
-- print the structure of the table
34+
describe employee;
35+
desc employee;
36+
show columns in employee;
37+
38+
-- renaming of table
39+
rename table employee to employee_table;
40+
alter table employee_table rename to employee;
41+
42+
-- reanaming a column
43+
alter table employee change column employee_id emp_id int;
44+
45+
-- add a constraint to column
46+
alter table employee change column first_name first_name varchar(50) not null;
47+
48+
-- add column
49+
alter table employee add column salary real;
50+
51+
-- drop a column
52+
alter table employee drop column salary;
53+
54+
-- modify the datatype
55+
alter table employee modify column salary int;
56+
57+
-- truncate a table
58+
truncate employee;
59+
60+
-- drop table
61+
drop table department;
62+
63+
-- drop database
64+
drop database cheatsheet;

0 commit comments

Comments
 (0)