SlideShare a Scribd company logo
1 of 13
Download to read offline
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)

MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
SQLQueries
SQLQueriesSQLQueries
SQLQueries
 
My SQL
My SQLMy SQL
My SQL
 
Sql 
statements , functions & joins
Sql 
statements , functions  &  joinsSql 
statements , functions  &  joins
Sql 
statements , functions & joins
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
Sql
SqlSql
Sql
 
Dml and ddl
Dml and ddlDml and ddl
Dml and ddl
 
Oracle training in hyderabad
Oracle training in hyderabadOracle training in hyderabad
Oracle training in hyderabad
 
1 ddl
1 ddl1 ddl
1 ddl
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
mysqlHiep.ppt
mysqlHiep.pptmysqlHiep.ppt
mysqlHiep.ppt
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Sqlharshal
SqlharshalSqlharshal
Sqlharshal
 
SQL Queries - DDL Commands
SQL Queries - DDL CommandsSQL Queries - DDL Commands
SQL Queries - DDL Commands
 
Les09
Les09Les09
Les09
 
SQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate TableSQL Tutorial - How To Create, Drop, and Truncate Table
SQL Tutorial - How To Create, Drop, and Truncate Table
 
Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL
 

Similar to Mysql quick guide

MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinhwebhostingguy
 
MYSQL
MYSQLMYSQL
MYSQLARJUN
 
My sql presentation
My sql presentationMy sql presentation
My sql presentationNikhil Jain
 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering CollegeDatabase Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering CollegeDhivyaa C.R
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivityMouli Chandira
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-noteLeerpiny Makouach
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018teachersduniya.com
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLanandology
 
MySql 5.7 Backup Script
MySql 5.7 Backup ScriptMySql 5.7 Backup Script
MySql 5.7 Backup ScriptHızlan ERPAK
 

Similar to Mysql quick guide (20)

Codigos
CodigosCodigos
Codigos
 
Raj mysql
Raj mysqlRaj mysql
Raj mysql
 
My sql1
My sql1My sql1
My sql1
 
MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinh
 
MySQL
MySQLMySQL
MySQL
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
MYSQL
MYSQLMYSQL
MYSQL
 
My sql presentation
My sql presentationMy sql presentation
My sql presentation
 
Sah
SahSah
Sah
 
Mysql cheatsheet
Mysql cheatsheetMysql cheatsheet
Mysql cheatsheet
 
UNIT V (5).pptx
UNIT V (5).pptxUNIT V (5).pptx
UNIT V (5).pptx
 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering CollegeDatabase Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
 
Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
database-querry-student-note
database-querry-student-notedatabase-querry-student-note
database-querry-student-note
 
MySQL Presentation
MySQL PresentationMySQL Presentation
MySQL Presentation
 
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
DATA BASE || INTRODUCTION OF DATABASE \\ SQL 2018
 
MYSQL
MYSQLMYSQL
MYSQL
 
My sql administration
My sql administrationMy sql administration
My sql administration
 
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQLTen Reasons Why You Should Prefer PostgreSQL to MySQL
Ten Reasons Why You Should Prefer PostgreSQL to MySQL
 
MySql 5.7 Backup Script
MySql 5.7 Backup ScriptMySql 5.7 Backup Script
MySql 5.7 Backup Script
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

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