G C Reddy Information Technology

Tutorials and Interview Questions on C, Manual Testing, QTP, LoadRunner, Selenium, Quality Center and SQL.

MySQL

 MySQL Interview Questions and Answers

1) What is MySQL?

MySQL is a open source Relational Database Management System and it is very fast reliable and flexible Database Management  System.

2) What are the Important features of MySQL?

MySQL Features

a) MySQL is fast and reliable RDBMS

b) MySQL Supports indexing and binary objects and it allows changes to structure of table while server is running.

c) It has very fast thread-based memory allocation system.

d) MySQL Written in C and C++ Programming languages

e)  MySQL Supports Windows, OS/2 and UNIX operating environments.

f) C, Python, PHP, Java , Delphi etc Programming libraries are available to connect to MySQL database.

g) It is a Open Source Relational Database Management System

3) When MySQL was launched?

in 1995

4) How to login into MySQL Using UNIX shell?

 By using below method if password is pass and user name is root

# [MySQL dir]/bin/MySQL -h hostname -u root -p pass

5) How we create a database on the MySQL server with UNIX shell?

 MySQL> create database databasename;

6) How to list or view all databases from the MySQL server?

MySQL> show databases;

7) How Switch (select or use) to a database?

MySQL> use databasename;

8) How To see all the tables from a database of MySQL server?

 MySQL> show tables;

9) How to see table’s field formats or description of table?

 MySQL> describe tablename;

10) How to delete a database from MySQL server?

MySQL> drop database databasename;

11) How we get Sum of column?

MySQL> SELECT SUM(*) FROM [table name];

 12) How to delete a table?

MySQL> drop table tablename;

 13) How you will Show all data from a table?

 MySQL> SELECT * FROM tablename;

14) How to returns the columns and column information pertaining to the designated table?

MySQL> show columns from tablename;

15) How to Show certain selected rows with the value “pcds”?

MySQL> SELECT * FROM tablename WHERE fieldname = “pcds”;

16) How will Show all records containing the name “gcreddy” AND the phone number ’9247837478′?

 MySQL> SELECT * FROM tablename WHERE name = “G C Reddy” AND phone_number = ’9247837478′;

17) How you will Show all records not containing the name “gcreddy” AND the phone number ’9247837478′ order by the phone_number field?

 mysql> SELECT * FROM tablename WHERE name != “gcreddy” AND phone_number = ’9247837478′ order by phone_number;

18) How to Show all records starting with the letters ‘gcreddy’ AND the phone number ’9247837478′?

 mysql> SELECT * FROM tablename WHERE name like “gcreddy%” AND phone_number = ’9247837478′;

19) How to show all records starting with the letters ‘gcreddy’ AND the phone number ’9247837478′ limit to records 1 through 5?

 mysql> SELECT * FROM tablename WHERE name like “gcreddy%” AND phone_number = ’9247837478′ limit 1,5;

20) Use a regular expression to find records. Use “REGEXP BINARY” to force case-sensitivity. This finds any record beginning with r?

 mysql> SELECT * FROM tablename WHERE rec RLIKE “^r”;

21) How you will Show unique records?

 mysql> SELECT DISTINCT columnname FROM tablename;

22) How we will Show selected records sorted in an ascending (asc) or descending (desc)?

mysql> SELECT col1,col2 FROM tablename ORDER BY col2 DESC; mysql> SELECT col1,col2 FROM tablename ORDER BY col2 ASC;

23) How to Return total number of rows?

 mysql> SELECT COUNT(*) FROM tablename;

24) How to Join tables on common columns?

mysql> select lookup.illustrationid, lookup.personid,person.birthday from lookup left join person on lookup.personid=person.personid=statement to join birthday in person table with primary illustration id

25) How to Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs?

 # mysql -u root -p mysql> use mysql; mysql> INSERT INTO user (Host,User,Password) VALUES(‘%’,'username’,PASSWORD(‘password’)); mysql> flush privileges;

26) How to Change a users password from UNIX shell?

# [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password ‘new-password’

27) How to Change a users password from MySQL prompt. Login as root. Set the password. Update privs?

 # mysql -u root -p mysql> SET PASSWORD FOR ‘user’@'hostname’ = PASSWORD(‘passwordhere’);

 28) How to Recover a MySQL root password. Stop the MySQL server process. Start again with no grant tables. Login to MySQL as root. Set new password. Exit MySQL and restart MySQL server?

 # /etc/init.d/mysql stop # mysqld_safe –skip-grant-tables & # mysql -u root mysql> use mysql; mysql> update user set password=PASSWORD(“newrootpassword”) where User=’root’; mysql> flush privileges; mysql> quit # /etc/init.d/mysql stop # /etc/init.d/mysql start

 29) How to Set a root password if there is on root password?

 # mysqladmin -u root password newpassword

 30) How to Update a root password?

 # mysqladmin -u root -p oldpassword newpassword

 31) How to allow the user “gcreddy” to connect to the server from localhost using the password “passwd”. Login as root. Switch to the MySQL db. Give privs. Update privs?

 # mysql -u root -p mysql> use mysql; mysql> grant usage on *.* to gcreddy@localhost identified by ‘passwd’; mysql> flush privileges;

32) How to give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs?

 # mysql -u root -p mysql> use mysql; mysql> INSERT INTO user (Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv) VALUES (‘%’,'databasename’,'username’,'Y’,'Y’,'Y’,'Y’,'Y’,'N’); mysql> flush privileges; or mysql> grant all privileges on databasename.* to username@localhost; mysql> flush privileges;

33) How To update info already in a table and Delete a row(s) from a table?

 mysql> UPDATE [table name] SET Select_priv = ‘Y’,Insert_priv = ‘Y’,Update_priv = ‘Y’ where [field name] = ‘user’; mysql> DELETE from [table name] where [field name] = ‘whatever’;

34) How to Update database permissions/privileges?

 mysql> flush privileges;

35) How to Delete a column and Add a new column to database?

mysql> alter table [table name] drop column [column name]; mysql> alter table [table name] add column [new column name] varchar (20);

36) Change column name and Make a unique column so we get no dupes?

 mysql> alter table [table name] change [old column name] [new column name] varchar (50);

37) How to make a column bigger and Delete unique from table?

 mysql> alter table [table name] modify [column name] VARCHAR(3); mysql> alter table [table name] drop index [colmn name];

38) How to Load a CSV file into a table?

mysql> LOAD DATA INFILE ‘/tmp/filename.csv’ replace INTO TABLE [table name] FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘n’ (field1,field2,field3);

38) How to dump all databases for backup. Backup file is sql commands to recreate all db’s?

 # [mysql dir]/bin/mysqldump -u root -ppassword –opt >/tmp/alldatabases.sql

39) How to dump one database for backup?

 # [mysql dir]/bin/mysqldump -u username -ppassword –databases databasename >/tmp/databasename.sql

 40) How to dump a table from a database?

 # [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql

41) Restore database (or database table) from backup?

# [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql

42) How to Create Table show Example?

mysql> CREATE TABLE [table name] (firstname VARCHAR(20), middleinitial VARCHAR(3), lastname VARCHAR(35),suffix VARCHAR(3),officeid VARCHAR(10),userid VARCHAR(15),username VARCHAR(8),email VARCHAR(35),phone VARCHAR(25), groups VARCHAR(15),datestamp DATE,timestamp time,pgpemail VARCHAR(255));

43) How to search second maximum(second highest) salary value(integer)from table employee (field salary)in the manner so that mysql gets less load?

 By using query we will get second maximum(second highest) salary value(integer)from table employee (field salary)in the manner so that mysql gets less load? SELECT DISTINCT(salary) FROM employee order by salary desc limit 1 , 1 ; (This way we will able to find out 3rd highest , 4th highest salary so on just need to change limit condtion like LIMIT 2,1 for 3rd highest and LIMIT 3,1 for 4th some one may finding this way useing below query that taken more time as compare to above query SELECT salary FROM employee where



Tags: , , , , ,


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to G C Reddy QTP Group
Email:
Visit this group

gc