This document provides a quick reference guide for getting started with MySQL. It outlines 16 steps: 1) installing MySQL, 2) starting the server, 3) logging in, 4) saving commands, 5) showing databases, 6) creating databases and tables, 7) describing and showing table definitions, 8) inserting, updating, and deleting data, 9) encrypting data, 10) altering tables, 11) deleting tables, and 16) deleting databases. The guide includes examples for each step.
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