SlideShare a Scribd company logo
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 1 of 13
GETTING START WITH MYSQL
1. Install WAMP or XAMPP or MYSQL SERVER on your computer
2. Starts the server and set the Mysql bin path on Command Prompt
XAMPP
[Eg.: cusermaths> cd c:xamppmysqlbin]
WAMP
[Eg.: cusermaths> cd c:wampbinmysqlmysql5.2.3bin]
Mysql Server
[Eg.: cusermaths> cd "c:program files(x86)mysqlmysql
server5.6bin"]
(Otherwise find the bin path)
3. Login into Mysql
If password is set
c:xamppmysqlbin> mysql -u root -p[password without space]
or
c:xamppmysqlbin> mysql -u root –p
Enter password:***** <<Type your password here, Press enter key>>
If password is not set
c:xamppmysqlbin> mysql -u root -p
Enter password: <<No need to type any letters, Press enter key>>
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 2 of 13
[Here -u indicates user, -p indicates password and root is the admin user.]
4. If you want to save all your commands, log a file to save
mysql> tee "absolute path"
or
mysql>T "absolute path"
[Eg.: mysql> tee "D:myfoldermywork.txt"]
5. Show existing databases
mysql> SHOW DATABASES;
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 3 of 13
6. Create a new database
mysql> CREATE DATABASE [IF NOT EXISTS] your_database_name;
[Eg.: mysql> CREATE DATABASE student;]
If there any database with same name it will try to create database then display error message
or
[Eg.: mysql> CREATE DATABASE IF NOT EXISTS student;]
If any database exist with same name it doesn't try to create
7. Open a database to have access
mysql> USE your_database;
8. Show opened database
mysql> SELECT DATABASE();
9. Create tables
mysql> CREATE TABLE your_table_name(
-> column1_name datatype [NOT NULL][AUTO INCREMENT],
-> column2_name datatype [DEFAULT "value"],
-> .....,
-> [PRIMARY KEY (column_name [,column name,...])],
-> [INDEX any_index_name column_name],
-> [CONSTRAINT any_fk_name FOREIGN KEY (column_name)
REFERENCES parent_table(column_name)
ON UPDATE [CASCADE|SET NULL|NO ACTION] ON DELETE
[CASCADE|SET NULL|NO ACTION]]);
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 4 of 13
[Eg.:
--Parent table --
mysql> CREATE TABLE student(
-> name VARCHAR(20) NOT NULL,
-> index_no VARCHAR(6),
-> reg_no VARCAHR(10) NOT NULL,
-> gender ENUM('Male','Female') NOT NULL,
-> addr VARCHAR(20),
-> dob DATE,
-> nic VARCHAR(10) NOT NULL,
-> pwd BLOB,
-> PRIMARY KEY(index_no),
-> INDEX idx_stu_reg (reg_no),
-> INDEX idx_stu_nic (nic));
-- Child table –
mysql> CREATE TABLE marks(
-> index_no VARCHAR(6) NOT NULL,
-> sub_code VARCHAR(5) NOT NULL,
-> sub_marks TINYINT UNSIGNED NOT NULL,
-> INDEX idx_marks_index (index_no),
-> CONSTRAINT fk_marks_stu_index FOREIGN KEY (index_no)
-> REFERENCES student(index_no) ON UPDATE CASCADE ON DELETE
CASCADE);
]
[Hint: AUTO_INCREMENT field must be set as primary key, FOREIGN KEY …REFERENCES table
field also be set as PRIMARY KEY or INDEX ]
10. Describing table contents
mysql> DESC your_table_name;
[Eg.: mysql> DESC student; ]
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 5 of 13
11. Show create definition of table
mysql> SHOW CREATE TABLE your_table_name;
[Eg.: mysql> SHOW CREATE TABLE student; ]
12. Inserting data to the tables
* Insert single row all fields
mysql> INSERT INTO your_table VALUES
-> ('val1','val2',.....);
* Insert single row certain fields
mysql> INSERT INTO your_table (field1,field2,field3) VALUES
-> ('val1','val2','val3');
* Insert multiple row all fields
mysql> INSERT INTO your_table VALUES
-> ('val1','val2',.....),
-> ('val1','val2',.....),
-> ('val1','val2',.....);
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 6 of 13
* Load from external file (ie, sampledata.txt)
mysql> LOAD DATA LOCAL INFILE 'file_pathsampledata.txt'
-> INTO TABLE your_table_name
-> [FIELDS TERMINATED BY 't'] //use field termination character
-> LINES TERMINATED BY 'rn'
-> [(column1,column2,.....)]; //if want to insert certain fields
13. Inserting encrypted data into table
* Data types need to hold : TEXT, BLOB, MEDIUMTEXT, MEDIUMBLOB,
LONGTEXT, LONGBLOB
* Methods to encrypt : PASSWORD, ENCODE, DES_ENCRYPT, SHA1, MD5
mysql> INSERT INTO student(pwd) VALUES (PASSWORD('string’));
or
mysql> INSERT INTO student(pwd) VALUES (ENCODE('string’,'flag'));
Sample Encrypted String
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 7 of 13
or
mysql> INSERT INTO student(pwd)
-> VALUES (DES_ENCRYPT('string','flag'));
or
mysql> INSERT INTO student(pwd) VALUES (SHA1('string’));
or
mysql> INSERT INTO student(pwd) VALUES (MD5('string'));
*Methods to decrypt : DECODE for encoded text, DES_DECRYPT for des_encrypted text
mysql> SELECT DECODE(encrypted_column,'flag') FROM student;
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 8 of 13
or
mysql> SELECT DES_DECRYPT(encrypted_column,'flag') FROM student;
14. Update the fields
-- update all the records --
mysql> UPDATE table_name SET field1='value1',field2='value2',....;
Before Updating Gender Field
After Updating Gender Field
[So, all records have been affected.]
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 9 of 13
or
-- update perticular records --
mysql> UPDATE table_name SET field1='value1',field2='value2',....
-> WHERE field='value';
[Hint: Must be careful when update tables. Use WHERE clause unless updating all records.]
15. Delete records
-- Delete all the records --
mysql> DELETE FROM table_name;
or
-- Delete particular records --
mysql> DELETE FROM table_name
-> WHERE field='value';
After Updating Gender Field
[So, certain records only have been affected.]
[So, all records have been deleted.]
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 10 of 13
[Hint: Must be careful when delete records. Use WHERE clause unless deleting all records.]
16. Alteration on tables
*Add additional column
mysql> ALTER TABLE table_name
-> ADD COLUMN new_column_name datatype [FIRST | AFTER column_name];
*Rename or change column
mysql> ALTER TABLE table_name
-> CHANGE COLUMN old_column_name new_column_name datatype;
[So, particular record only has been deleted.]
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 11 of 13
*change data type or modify
mysql> ALTER TABLE table_name
-> MODIFY old_column_name new_datatype;
*delete a column
mysql> ALTER TABLE table_name
-> DROP COLUMN column_name;
*Add primary key
mysql> ALTER TABLE table_name
-> ADD PRIMARY KEY (column1_name,column2_name,....);
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 12 of 13
*Delete primary key
mysql> ALTER TABLE table_name
-> DROP PRIMARY KEY;
*Add index
mysql> ALTER TABLE table_name
-> ADD INDEX index_name (column_name);
*Delete index
mysql> ALTER TABLE table_name
-> DROP INDEX index_name;
*Add foreign key
mysql> ALTER TABLE table_name
-> ADD CONSTRAINT foreignkey_name
FOREIGN KEY (column_name)
REFERENCES parent_table(column_name)
ON UPDATE [NO ACTION| CASCADE | SET NULL]
ON DELETE [NO ACTION| CASCADE | SET NULL];
*Delete foreign key
mysql> ALTER TABLE table_name
-> DROP FOREIGN KEY foreignkey_name;
Quick Reference Guide
S.Puvikanth, Temp. Demonstrator – CS,
Dept. of Mathematics, EUSL.
Page 13 of 13
*Rename table
mysql> ALTER TABLE table_name
-> RENAME TO new_table_name;
15. Delete a table
mysql> DROP TABLE [IF EXISTS] table_name;
[Eg.: mysql> DROP TABLE student; ]
16. Delete a database
mysql> DROP DATABASE [IF EXISTS] db_name;
[Eg.: mysql> DROP DATABASE mytest; ]
Before drop tables
After dropped tables
Before drop databases
After dropped databases

More Related Content

What's hot (18)

PDF
MySQL partitions tutorial
Giuseppe Maxia
 
PPT
Sql basics and DDL statements
Mohd Tousif
 
DOC
SQLQueries
karunakar81987
 
PPT
My SQL
Karan Kashyap
 
PPTX
Sql 
statements , functions & joins
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Oracle: DDL
DataminingTools Inc
 
PPT
Oracle training in hyderabad
Kelly Technologies
 
DOC
Oracle sql material
prathap kumar
 
PPT
mysqlHiep.ppt
webhostingguy
 
PPTX
SQL - DML and DDL Commands
Shrija Madhu
 
ODP
Sqlharshal
ashvinkokate
 
PDF
SQL Queries - DDL Commands
ShubhamBauddh
 
PPT
Les09
arnold 7490
 
PPT
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
PPTX
Oracle Database DML DDL and TCL
Abdul Rehman
 
MySQL partitions tutorial
Giuseppe Maxia
 
Sql basics and DDL statements
Mohd Tousif
 
SQLQueries
karunakar81987
 
Sql 
statements , functions & joins
baabtra.com - No. 1 supplier of quality freshers
 
Oracle: DDL
DataminingTools Inc
 
Oracle training in hyderabad
Kelly Technologies
 
Oracle sql material
prathap kumar
 
mysqlHiep.ppt
webhostingguy
 
SQL - DML and DDL Commands
Shrija Madhu
 
Sqlharshal
ashvinkokate
 
SQL Queries - DDL Commands
ShubhamBauddh
 
SQL Tutorial - How To Create, Drop, and Truncate Table
1keydata
 
Oracle Database DML DDL and TCL
Abdul Rehman
 

Similar to Mysql quick guide (20)

PDF
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
PPTX
SQL PPT.pptx
PriyaPandey767008
 
PDF
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
PDF
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
PPTX
Mysql-overview.pptx
TamilHunt
 
PPTX
Getting Started with MySQL I
Sankhya_Analytics
 
PPTX
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
teachersduniya.com
 
PPTX
Data Base Management 1 Database Management.pptx
PreeTVithule1
 
PDF
MySQL for beginners
Saeid Zebardast
 
PPTX
Introduction to database
oly07104
 
PPTX
Introduction to database
Kazi Uddin Oly
 
PPTX
sql.pptx
slavskrillex
 
PDF
sql_data.pdf
VandanaGoyal21
 
PDF
SQL on Linux and its uses and application.pdf
bhaveshsethi456
 
PDF
Mysql basics1
Steffy Robert
 
PPT
mysql.ppt
nawaz65
 
PDF
Introduction to MySQL and introduction to basic queries
anishacotta2
 
PPTX
Data base.ppt
TeklayBirhane
 
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
SQL PPT.pptx
PriyaPandey767008
 
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
Mysql-overview.pptx
TamilHunt
 
Getting Started with MySQL I
Sankhya_Analytics
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
teachersduniya.com
 
Data Base Management 1 Database Management.pptx
PreeTVithule1
 
MySQL for beginners
Saeid Zebardast
 
Introduction to database
oly07104
 
Introduction to database
Kazi Uddin Oly
 
sql.pptx
slavskrillex
 
sql_data.pdf
VandanaGoyal21
 
SQL on Linux and its uses and application.pdf
bhaveshsethi456
 
Mysql basics1
Steffy Robert
 
mysql.ppt
nawaz65
 
Introduction to MySQL and introduction to basic queries
anishacotta2
 
Data base.ppt
TeklayBirhane
 
Ad

Recently uploaded (20)

PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Difference between write and update in odoo 18
Celine George
 
Introduction presentation of the patentbutler tool
MIPLM
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Controller Request and Response in Odoo18
Celine George
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Ad

Mysql quick guide

  • 1. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 1 of 13 GETTING START WITH MYSQL 1. Install WAMP or XAMPP or MYSQL SERVER on your computer 2. Starts the server and set the Mysql bin path on Command Prompt XAMPP [Eg.: cusermaths> cd c:xamppmysqlbin] WAMP [Eg.: cusermaths> cd c:wampbinmysqlmysql5.2.3bin] Mysql Server [Eg.: cusermaths> cd "c:program files(x86)mysqlmysql server5.6bin"] (Otherwise find the bin path) 3. Login into Mysql If password is set c:xamppmysqlbin> mysql -u root -p[password without space] or c:xamppmysqlbin> mysql -u root –p Enter password:***** <<Type your password here, Press enter key>> If password is not set c:xamppmysqlbin> mysql -u root -p Enter password: <<No need to type any letters, Press enter key>>
  • 2. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 2 of 13 [Here -u indicates user, -p indicates password and root is the admin user.] 4. If you want to save all your commands, log a file to save mysql> tee "absolute path" or mysql>T "absolute path" [Eg.: mysql> tee "D:myfoldermywork.txt"] 5. Show existing databases mysql> SHOW DATABASES;
  • 3. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 3 of 13 6. Create a new database mysql> CREATE DATABASE [IF NOT EXISTS] your_database_name; [Eg.: mysql> CREATE DATABASE student;] If there any database with same name it will try to create database then display error message or [Eg.: mysql> CREATE DATABASE IF NOT EXISTS student;] If any database exist with same name it doesn't try to create 7. Open a database to have access mysql> USE your_database; 8. Show opened database mysql> SELECT DATABASE(); 9. Create tables mysql> CREATE TABLE your_table_name( -> column1_name datatype [NOT NULL][AUTO INCREMENT], -> column2_name datatype [DEFAULT "value"], -> ....., -> [PRIMARY KEY (column_name [,column name,...])], -> [INDEX any_index_name column_name], -> [CONSTRAINT any_fk_name FOREIGN KEY (column_name) REFERENCES parent_table(column_name) ON UPDATE [CASCADE|SET NULL|NO ACTION] ON DELETE [CASCADE|SET NULL|NO ACTION]]);
  • 4. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 4 of 13 [Eg.: --Parent table -- mysql> CREATE TABLE student( -> name VARCHAR(20) NOT NULL, -> index_no VARCHAR(6), -> reg_no VARCAHR(10) NOT NULL, -> gender ENUM('Male','Female') NOT NULL, -> addr VARCHAR(20), -> dob DATE, -> nic VARCHAR(10) NOT NULL, -> pwd BLOB, -> PRIMARY KEY(index_no), -> INDEX idx_stu_reg (reg_no), -> INDEX idx_stu_nic (nic)); -- Child table – mysql> CREATE TABLE marks( -> index_no VARCHAR(6) NOT NULL, -> sub_code VARCHAR(5) NOT NULL, -> sub_marks TINYINT UNSIGNED NOT NULL, -> INDEX idx_marks_index (index_no), -> CONSTRAINT fk_marks_stu_index FOREIGN KEY (index_no) -> REFERENCES student(index_no) ON UPDATE CASCADE ON DELETE CASCADE); ] [Hint: AUTO_INCREMENT field must be set as primary key, FOREIGN KEY …REFERENCES table field also be set as PRIMARY KEY or INDEX ] 10. Describing table contents mysql> DESC your_table_name; [Eg.: mysql> DESC student; ]
  • 5. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 5 of 13 11. Show create definition of table mysql> SHOW CREATE TABLE your_table_name; [Eg.: mysql> SHOW CREATE TABLE student; ] 12. Inserting data to the tables * Insert single row all fields mysql> INSERT INTO your_table VALUES -> ('val1','val2',.....); * Insert single row certain fields mysql> INSERT INTO your_table (field1,field2,field3) VALUES -> ('val1','val2','val3'); * Insert multiple row all fields mysql> INSERT INTO your_table VALUES -> ('val1','val2',.....), -> ('val1','val2',.....), -> ('val1','val2',.....);
  • 6. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 6 of 13 * Load from external file (ie, sampledata.txt) mysql> LOAD DATA LOCAL INFILE 'file_pathsampledata.txt' -> INTO TABLE your_table_name -> [FIELDS TERMINATED BY 't'] //use field termination character -> LINES TERMINATED BY 'rn' -> [(column1,column2,.....)]; //if want to insert certain fields 13. Inserting encrypted data into table * Data types need to hold : TEXT, BLOB, MEDIUMTEXT, MEDIUMBLOB, LONGTEXT, LONGBLOB * Methods to encrypt : PASSWORD, ENCODE, DES_ENCRYPT, SHA1, MD5 mysql> INSERT INTO student(pwd) VALUES (PASSWORD('string’)); or mysql> INSERT INTO student(pwd) VALUES (ENCODE('string’,'flag')); Sample Encrypted String
  • 7. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 7 of 13 or mysql> INSERT INTO student(pwd) -> VALUES (DES_ENCRYPT('string','flag')); or mysql> INSERT INTO student(pwd) VALUES (SHA1('string’)); or mysql> INSERT INTO student(pwd) VALUES (MD5('string')); *Methods to decrypt : DECODE for encoded text, DES_DECRYPT for des_encrypted text mysql> SELECT DECODE(encrypted_column,'flag') FROM student;
  • 8. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 8 of 13 or mysql> SELECT DES_DECRYPT(encrypted_column,'flag') FROM student; 14. Update the fields -- update all the records -- mysql> UPDATE table_name SET field1='value1',field2='value2',....; Before Updating Gender Field After Updating Gender Field [So, all records have been affected.]
  • 9. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 9 of 13 or -- update perticular records -- mysql> UPDATE table_name SET field1='value1',field2='value2',.... -> WHERE field='value'; [Hint: Must be careful when update tables. Use WHERE clause unless updating all records.] 15. Delete records -- Delete all the records -- mysql> DELETE FROM table_name; or -- Delete particular records -- mysql> DELETE FROM table_name -> WHERE field='value'; After Updating Gender Field [So, certain records only have been affected.] [So, all records have been deleted.]
  • 10. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 10 of 13 [Hint: Must be careful when delete records. Use WHERE clause unless deleting all records.] 16. Alteration on tables *Add additional column mysql> ALTER TABLE table_name -> ADD COLUMN new_column_name datatype [FIRST | AFTER column_name]; *Rename or change column mysql> ALTER TABLE table_name -> CHANGE COLUMN old_column_name new_column_name datatype; [So, particular record only has been deleted.]
  • 11. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 11 of 13 *change data type or modify mysql> ALTER TABLE table_name -> MODIFY old_column_name new_datatype; *delete a column mysql> ALTER TABLE table_name -> DROP COLUMN column_name; *Add primary key mysql> ALTER TABLE table_name -> ADD PRIMARY KEY (column1_name,column2_name,....);
  • 12. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 12 of 13 *Delete primary key mysql> ALTER TABLE table_name -> DROP PRIMARY KEY; *Add index mysql> ALTER TABLE table_name -> ADD INDEX index_name (column_name); *Delete index mysql> ALTER TABLE table_name -> DROP INDEX index_name; *Add foreign key mysql> ALTER TABLE table_name -> ADD CONSTRAINT foreignkey_name FOREIGN KEY (column_name) REFERENCES parent_table(column_name) ON UPDATE [NO ACTION| CASCADE | SET NULL] ON DELETE [NO ACTION| CASCADE | SET NULL]; *Delete foreign key mysql> ALTER TABLE table_name -> DROP FOREIGN KEY foreignkey_name;
  • 13. Quick Reference Guide S.Puvikanth, Temp. Demonstrator – CS, Dept. of Mathematics, EUSL. Page 13 of 13 *Rename table mysql> ALTER TABLE table_name -> RENAME TO new_table_name; 15. Delete a table mysql> DROP TABLE [IF EXISTS] table_name; [Eg.: mysql> DROP TABLE student; ] 16. Delete a database mysql> DROP DATABASE [IF EXISTS] db_name; [Eg.: mysql> DROP DATABASE mytest; ] Before drop tables After dropped tables Before drop databases After dropped databases