Skip to content

Commit 6d60f7a

Browse files
authored
Add files via upload
add new file
1 parent 803613b commit 6d60f7a

9 files changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Alter database using alter database
2+
Alter database enables you to change the overall characteristics of a database.
3+
These characteristics are stored in the db.opt file in the database directory.
4+
to use ALTER DATABASE, you need the ALTER privilege on the database.
5+
ALTER SCHEMA is a synonym for ALTER DATABASE.
6+
The database name can be omitted from the syntax in which case the statemnet
7+
applies to the default database.
8+
Syntax: ALTER DATABASE database_name;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Change column:
2+
This is used to change name and data type of an existing column. without constraints
3+
syntax: ALTER TABLE table_name CHANGE COLUMN old_column_name new_column_name new data_type(size);
4+
5+
ex: ALTER TABLE new CHANGE COLUMN name student varchar(20);
6+
Query OK, 0 rows affected (0.01 sec)
7+
Records: 0 Duplicates: 0 Warnings: 0
8+
mysql> desc new;
9+
+----------+-------------+------+-----+---------+----------------+
10+
| Field | Type | Null | Key | Default | Extra |
11+
+----------+-------------+------+-----+---------+----------------+
12+
| t_id | int | YES | | NULL | |
13+
| id | int | NO | PRI | NULL | auto_increment |
14+
| student | varchar(20) | YES | | NULL | |
15+
| t_number | int | YES | UNI | NULL | |
16+
| address | varchar(50) | YES | | NULL | |
17+
| number | int | YES | | NULL | |
18+
| s_id | int | NO | | NULL | |
19+
+----------+-------------+------+-----+---------+----------------+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Change Column name and its Data type with constraint
2+
Change column with Constraints:-
3+
syntax: ALTER TABLE table_name CHANGE COLUMN old_column_name new_column new _datetype (size) Constraint_name,
4+
ADD Constraint_name (column_name);
5+
6+
ex: ALTER TABLE staff CHANGE COLUMN s_id id int NOT NULL, ADD PRIMARY KEY
7+
(id);
8+
Query OK, 1 row affected (0.08 sec)
9+
Records: 1 Duplicates: 0 Warnings: 0
10+
11+
mysql> desc staff;
12+
+--------+-------------+------+-----+---------+-------+
13+
| Field | Type | Null | Key | Default | Extra |
14+
+--------+-------------+------+-----+---------+-------+
15+
| id | int | NO | PRI | NULL | |
16+
| name | varchar(50) | YES | | NULL | |
17+
| number | int | YES | | NULL | |
18+
+--------+-------------+------+-----+---------+-------+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Change more than one column Name and Its data type without constraints in SQL.
2+
syntax: ALTER TABLE table_name CHANGE COLUMN old_column_name new_column_name new_datatype(size);
3+
CHANGE COLUMN old_column_name new_column_name new_datatype(size);
4+
ex: ALTER TABLE new CHANGE COLUMN t_id teacher_id int (10), CHANGE COLUMN
5+
id student_id int(10);
6+
Query OK, 4 rows affected, 2 warnings (0.08 sec)
7+
Records: 4 Duplicates: 0 Warnings: 2
8+
9+
mysql> desc new;
10+
+------------+-------------+------+-----+---------+-------+
11+
| Field | Type | Null | Key | Default | Extra |
12+
+------------+-------------+------+-----+---------+-------+
13+
| teacher_id | int | YES | | NULL | |
14+
| student_id | int | NO | PRI | NULL | |
15+
| student | varchar(20) | YES | | NULL | |
16+
| t_number | int | YES | UNI | NULL | |
17+
| address | varchar(50) | YES | | NULL | |
18+
| number | int | YES | | NULL | |
19+
| s_id | int | NO | | NULL | |
20+
+------------+-------------+------+-----+---------+-------+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//Delete data of table using Truncate table.
2+
-> when we only want to delete the data inside the table, and not the table itself.
3+
syntax: TRUNCATE TABLE table_name;
4+
ex:select * from student;
5+
+-------------+------+---------+
6+
| name | roll | address |
7+
+-------------+------+---------+
8+
| U'I Testing | 101 | Ktm |
9+
+-------------+------+---------+
10+
1 row in set (0.02 sec)
11+
12+
mysql> TRUNCATE TABLE student;
13+
Query OK, 0 rows affected (0.02 sec)
14+
15+
mysql> select * from student;
16+
Empty set (0.00 sec)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//Drop Column with or without constraint
2+
-> When a column a table need to delete.
3+
syntax : ALTER TABLE table_name DROP COLUMN column_name;
4+
ex:ALTER TABLE staff DROP COLUMN number;
5+
-> desc staff;
6+
+-------+----------+------+-----+---------+-------+
7+
| Field | Type | Null | Key | Default | Extra |
8+
+-------+----------+------+-----+---------+-------+
9+
| id | int | NO | PRI | NULL | |
10+
| name | char(20) | YES | | NULL | |
11+
+-------+----------+------+-----+---------+-------+
12+
13+
// When removing constraint from a column.
14+
syntax: ALTER TABLE table_name DROP constraint_name column_name;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Drop Table or Delete Table from Database.
2+
-> This command is used to delete/remove table from the database;
3+
syntax: DROP TABLE table_name;
4+
ex: show tables;
5+
+-----------------+
6+
| Tables_in_my_db |
7+
+-----------------+
8+
| new |
9+
| staff |
10+
| student |
11+
| student_rec |
12+
| student_reg |
13+
+-----------------+
14+
5 rows in set (0.00 sec)
15+
16+
mysql> drop table student_rec;
17+
Query OK, 0 rows affected (0.01 sec)
18+
19+
mysql> show tables;
20+
+-----------------+
21+
| Tables_in_my_db |
22+
+-----------------+
23+
| new |
24+
| staff |
25+
| student |
26+
| student_reg |
27+
+-----------------+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//Rename table name using Rename table
2+
-> This command is used to rename one or more table.
3+
syntax: RENAME TABLE old_table_name to new_table_name;
4+
ex: RENAME TABLE staff TO emp;
5+
mysql> show tables;
6+
+-----------------+
7+
| Tables_in_my_db |
8+
+-----------------+
9+
| emp |
10+
| new |
11+
| student |
12+
| student_reg |
13+
+-----------------+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Modify Column Data Type Its size with or Without Constraint
2+
-> This is used to modify size of the data type or the data type it self of an existing column without changing column name.
3+
syntax:ALTER TABLE table_name MODIFY COLUMN column_name datatype(size);
4+
ex: ALTER TABLE staff MODIFY COLUMN name char(20);
5+
Query OK, 1 row affected (0.08 sec)
6+
Records: 1 Duplicates: 0 Warnings: 0
7+
8+
mysql> desc staff;
9+
+--------+----------+------+-----+---------+-------+
10+
| Field | Type | Null | Key | Default | Extra |
11+
+--------+----------+------+-----+---------+-------+
12+
| id | int | NO | PRI | NULL | |
13+
| name | char(20) | YES | | NULL | |
14+
| number | int | YES | | NULL | |
15+
+--------+----------+------+-----+---------+-------+

0 commit comments

Comments
 (0)