Skip to content

Commit a7b2e43

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

9 files changed

+153
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copy Old Table to New Table within same Database.
2+
Within same Database
3+
Syntax: CREATE TABLE new_table LIKE old_table;
4+
INSERT new_table SELECT * FROM old_table;
5+
ex: CREATE TABLE emp Like staff; INSERT emp SELECT * FROM staff;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copy old Table to new Table
2+
Within different Database
3+
Use the database where you want to copy the old table
4+
syntax: CREATE TABLE new_table LIKE old_db.old_table;
5+
INSERT new_table SELECT * FROM old_db.old_table;
6+
7+
ex: CREATE TABLE teacher LIKE my_db.student;
8+
INSERT teacher SELECT * FROM my_db.student;

Delete Records in Table in SQL.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// DELETE Records in Table
2+
The DELETE statement is used to delete records in a table.
3+
1. DELETE a specific Record
4+
Syntax: DELETE FROM table_name WHERE some_column = some_value;
5+
ex: DELETE FROM student_reg WHERE s_id = 105 AND name ='KAMAL';
6+
7+
8+
:- select * from student_reg;
9+
+------+---------+---------+------------+-----------+------+--------+-------+
10+
| s_id | name | address | dob | fees | id | result | marks |
11+
+------+---------+---------+------------+-----------+------+--------+-------+
12+
| 101 | RAM | KTM | 1998-12-15 | 100000.23 | 1001 | SECOND | 300 |
13+
| 102 | SITA | KTM | 2001-10-11 | 200000.12 | NULL | SECOND | 400 |
14+
| 103 | HARI | PKR | 2001-01-12 | 10000.52 | NULL | FIRST | 450 |
15+
| 104 | BIMAl | KTM | NULL | 100200.00 | NULL | THIRD | 250 |
16+
| 106 | SITA KC | PKR | 1998-02-02 | 20000.12 | NULL | SECOND | 340 |
17+
+------+---------+---------+------------+-----------+------+--------+-------+
18+
.Note :- WHERE is necessary otherqwise all records will be delete.
19+
2. DELETE ALL Records
20+
-> syntax: DELETE FROM table_name;
21+
OR//
22+
DELETE * FROM table_name;
23+
ex: DELETE FROM table_name;
24+
25+
. Note : We cannot undo this
26+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Drop database.
2+
-> The DROP DATABSES statement is used to delete a database.
3+
syntax: DROP DATABSES database_name;
4+
ex: DROP DATABASE my_db;

Show Create Database in SQL.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// show create database
2+
It shows commands which you have written while creating your Database.
3+
syntax: SHOW CREATE DATABASE database_name;
4+
ex:SHOW CREATE DATABASE my_db;
5+
Output:
6+
+----------+---------------------------------------------------------------------------------------------------------------------------------+
7+
| Database | Create Database
8+
|
9+
+----------+---------------------------------------------------------------------------------------------------------------------------------+
10+
| my_db | CREATE DATABASE `my_db` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
11+
+----------+---------------------------------------------------------------------------------------------------------------------------------+

Show Create Table in SQL.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Show Create Table
2+
-> It shows commands which you have written while creating your table.
3+
Syntax: SHOW CREATE TABLE table_name;
4+
ex:show create table student;
5+
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
6+
| Table | Create Table
7+
8+
|
9+
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
10+
| student | CREATE TABLE `student` (
11+
`name` varchar(20) DEFAULT NULL,
12+
`roll` int DEFAULT NULL,
13+
`address` varchar(20) DEFAULT NULL
14+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
15+
+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Show column in SQL.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Show columns
2+
It shows all the columns of table and their data type along with any other columns
3+
specific details. it is just like DESC table_name.
4+
syntax: SHOW COLUMNS FROM table_name;
5+
ex: show columns from student;
6+
+---------+-------------+------+-----+---------+-------+
7+
| Field | Type | Null | Key | Default | Extra |
8+
+---------+-------------+------+-----+---------+-------+
9+
| name | varchar(20) | YES | | NULL | |
10+
| roll | int | YES | | NULL | |
11+
| address | varchar(20) | YES | | NULL | |
12+
+---------+-------------+------+-----+---------+-------+

Update Records in Table SQL.txt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Update Records in Table.
2+
The Update statement is used existing records in a table.
3+
syntax: UPDATE table_name SET column1 = value1, column2=value2 WHERE some_column = some_value;
4+
ex: UPDATE student_reg SET name ='RAM',fees=100000.23 WHERE s_id =101;
5+
mysql> select * from student_reg;
6+
+------+---------+---------+------------+-----------+------+
7+
| s_id | name | address | dob | fees | id |
8+
+------+---------+---------+------------+-----------+------+
9+
| 101 | RAM | KTM | 1998-12-15 | 100000.23 | NULL |
10+
| 102 | SITA | KTM | 2001-10-11 | 200000.12 | NULL |
11+
| 103 | HARI | PKR | 2001-01-12 | 10000.52 | NULL |
12+
| 104 | BIMAl | KTM | NULL | 1001.23 | NULL |
13+
| 105 | KAMAL | PKR | 1956-12-12 | 100000.12 | NULL |
14+
| 106 | SITA KC | PKR | 1998-02-02 | 20000.12 | NULL |
15+
+------+---------+---------+------------+-----------+------+
16+
17+
// example 2nd file data
18+
ex:UPDATE student_reg SET name ='RAM',id=1001 WHERE s_id =101;
19+
20+
21+
mysql> select * from student_reg;
22+
+------+---------+---------+------------+-----------+------+
23+
| s_id | name | address | dob | fees | id |
24+
+------+---------+---------+------------+-----------+------+
25+
| 101 | RAM | KTM | 1998-12-15 | 100000.23 | 1001 |
26+
| 102 | SITA | KTM | 2001-10-11 | 200000.12 | NULL |
27+
| 103 | HARI | PKR | 2001-01-12 | 10000.52 | NULL |
28+
| 104 | BIMAl | KTM | NULL | 1001.23 | NULL |
29+
| 105 | KAMAL | PKR | 1956-12-12 | 100000.12 | NULL |
30+
| 106 | SITA KC | PKR | 1998-02-02 | 20000.12 | NULL |
31+
+------+---------+---------+------------+-----------+------+
32+
33+
Ex: UPDATE student_reg SET fees = 100200 WHERE s_id = 104;
34+
Output:select * from student_reg;
35+
+------+---------+---------+------------+-----------+------+
36+
| s_id | name | address | dob | fees | id |
37+
+------+---------+---------+------------+-----------+------+
38+
| 101 | RAM | KTM | 1998-12-15 | 100000.23 | 1001 |
39+
| 102 | SITA | KTM | 2001-10-11 | 200000.12 | NULL |
40+
| 103 | HARI | PKR | 2001-01-12 | 10000.52 | NULL |
41+
| 104 | BIMAl | KTM | NULL | 100200.00 | NULL |
42+
| 105 | KAMAL | PKR | 1956-12-12 | 100000.12 | NULL |
43+
| 106 | SITA KC | PKR | 1998-02-02 | 20000.12 | NULL |
44+
+------+---------+---------+------------+-----------+------+
45+
46+
47+
48+
Note: WHERE is necessary otherwise all records will be replace with given value.

Update with Case in SQL.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Update with case
2+
synatx: UPDARE table_name SET new_column = CASE
3+
WHEN column_name1 = some_value1 THEN new_value1
4+
WHEN column_name2 = some_value2 THEN new_value2
5+
ELSE new_value3 END;
6+
ex: UPDATE student_reg SET result = CASE
7+
-> WHEN marks >= 450 Then 'FIRST'
8+
-> WHEN marks < 450 AND marks >=300 THEN 'SECOND'
9+
-> WHEN marks < 300 AND marks >= 200 ThEN 'THIRD'
10+
-> ELSE 'Fail' END;
11+
Query OK, 6 rows affected (0.01 sec)
12+
Rows matched: 6 Changed: 6 Warnings: 0
13+
14+
mysql> select * from student_reg;
15+
+------+---------+---------+------------+-----------+------+--------+-------+
16+
| s_id | name | address | dob | fees | id | result | marks |
17+
+------+---------+---------+------------+-----------+------+--------+-------+
18+
| 101 | RAM | KTM | 1998-12-15 | 100000.23 | 1001 | SECOND | 300 |
19+
| 102 | SITA | KTM | 2001-10-11 | 200000.12 | NULL | SECOND | 400 |
20+
| 103 | HARI | PKR | 2001-01-12 | 10000.52 | NULL | FIRST | 450 |
21+
| 104 | BIMAl | KTM | NULL | 100200.00 | NULL | THIRD | 250 |
22+
| 105 | KAMAL | PKR | 1956-12-12 | 100000.12 | NULL | Fail | 150 |
23+
| 106 | SITA KC | PKR | 1998-02-02 | 20000.12 | NULL | SECOND | 340 |
24+
+------+---------+---------+------------+-----------+------+--------+-------+

0 commit comments

Comments
 (0)