Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

SQL Commands (Command Prompt)

Sql paper
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL Commands (Command Prompt)

Sql paper
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Commands

Command Prompt

1. mysql --version // to see the sql version

2. C:\Users\Akshay>mysql -u root -p //-u(user), -p (password)


Enter password: *****
3. show databases; // show all the databases.
4. create database ruchi; //to create new database.
show databases; // show all databases.
5. mysql> use ruchi;
Database changed
6. mysql> CREATE TABLE Persons ( // create a table
-> PersonID int,
-> LastName varchar(255),
-> FirstName varchar(255),
-> Address varchar(255),
-> City varchar(255) );
7. mysql> drop table Persons; // delete the structure of “Persons” table
Query OK, 0 rows affected (0.08 sec)
8. mysql> CREATE TABLE Persons ( // again created a table “Persons”
-> PersonID int,
-> LastName varchar(255),
-> FirstName varchar(255),
-> Address varchar(255),
-> City varchar(255)
-> );
Query OK, 0 rows affected (0.07 sec)
9. mysql> Create table students ( // created another table “students”
-> rolno int,
-> name varchar(20),
-> address varchar(30)
-> );
Query OK, 0 rows affected (0.04 sec)
10. mysql> insert into students // inserted a value inside students’ table (DML)
-> values (1,'ram', '101#abc');
Query OK, 1 row affected (0.04 sec)
11. Insert multiple rows – (dml)
mysql> INSERT INTO employees VALUES
-> (1, 'John Doe', 'Sales', NULL),
-> (2, 'Jane Smith', 'Marketing', '555-1234'),
-> (3, 'Bob Johnson', 'Sales', NULL);
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0

12. mysql> DESC persons; // describe persons table


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| PersonID | int | YES | | NULL | |
| LastName | varchar(255) | YES | | NULL | |
| FirstName | varchar(255) | YES | | NULL | |
| Address | varchar(255) | YES | | NULL | |
| City | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.03 sec)
13. mysql> desc students; // describe students table
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| rolno | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| address | varchar(30) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
14. mysql> select * from students; // (DML)to see all records in a table
+-------+------+---------+
| rolno | name | address |
+-------+------+---------+
| 1 | ram | 101#abc |
+-------+------+---------+
1 row in set (0.04 sec)
15. mysql> alter table persons // to rename the table name using ALTER
-> rename employee;
Query OK, 0 rows affected (0.20 sec)
16. mysql> select * from employee; // to see all records in a table
Empty set (0.06 sec)

17. mysql> desc employee;


+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| PersonID | int | YES | | NULL | |
| LastName | varchar(255) | YES | | NULL | |
| FirstName | varchar(255) | YES | | NULL | |
| Address | varchar(255) | YES | | NULL | |
| City | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
5 rows in set (0.07 sec)
ALTER
18. alter table – add column
alter table employee
-> add Email varchar(20) ;
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0

19. Alter table – Drop column


mysql> alter table employee drop phoneno;
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
20. Alter table – modify column
To modify existing columns in a table. To change data type of any column or to modify
its size.
mysql> alter table employee modify LastName varchar(20);
Query OK, 0 rows affected (0.25 sec)
Records: 0 Duplicates: 0 Warnings: 0
21. Rename column
To rename an existing column in a table.

1. Example 1: Rename Column Using ALTER TABLE Statement with Rename Clause

mysql> ALTER TABLE employees


-> RENAME COLUMN Full_name TO name;
Query OK, 0 rows affected (0.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

22. Update the row (DML)


update students
-> set name = 'mukesh' , address= '104#abc'
-> where rolno = 1;
23. Delete the row (DML)
DELETE FROM students WHERE rolno= 1;

You might also like