PHP Interview Questions

Q- 1 how to do login in mysql with unix shell
A-1 By below method if password is pass and user name is root
# [mysql dir]/bin/mysql -h hostname -u root -p pass
Q- 2 how you will Create a database on the mysql server with unix shell
A- 2 mysql> create database databasename;
Q- 3 how to list or view all databases from the mysql server.
A- 3 mysql> show databases;
Q- 4 How Switch (select or use) to a database.
A- 4 mysql> use databasename;
Q- 5 How To see all the tables from a database of mysql server.
A- 5 mysql> show tables;
Q- 6 How to see table's field formats or description of table .
A- 6 mysql> describe tablename;
Q- 7 How to delete a database from mysql server.
A- 7 mysql> drop database databasename;
Q- 8 How we get Sum of column
A- 8 mysql> SELECT SUM(*) FROM [table name];
Q- 9 How to delete a table
A- 9 mysql> drop table tablename;
Q- 10 How you will Show all data from a table.
A- 10 mysql> SELECT * FROM tablename;
Q- 11 How to returns the columns and column information pertaining to the designated table
A- 11 mysql> show columns from tablename;
Q- 12 How to Show certain selected rows with the value "pcds"
A- 12 mysql> SELECT * FROM tablename WHERE fieldname = "pcds";
Q- 13 How will Show all records containing the name "sonia" AND the phone number '9876543210'
A- 13 mysql> SELECT * FROM tablename WHERE name = "sonia" AND phone_number = '9876543210';
Q- 14 How you will Show all records not containing the name "sonia" AND the phone number '9876543210' order by the phone_number field.
A- 14 mysql> SELECT * FROM tablename WHERE name != "sonia" AND
phone_number = '9876543210' order by phone_number;
Q- 15 How to Show all records starting with the letters 'sonia' AND the phone number '9876543210'
A- 15 mysql> SELECT * FROM tablename WHERE name like "sonia%" AND phone_number = '9876543210';
Q- 16 How to show all records starting with the letters 'sonia' AND the phone number '9876543210' limit to records 1 through 5.
A- 16 mysql> SELECT * FROM tablename WHERE name like "sonia%" AND phone_number = '9876543210' limit 1,5;
Q- 16 Use a regular expression to find records. Use "REGEXP BINARY" to force case-sensitivity. This finds any record beginning with r.
A- 16 mysql> SELECT * FROM tablename WHERE rec RLIKE "^r";
Q- 17 How you will Show unique records.
A- 17 mysql> SELECT DISTINCT columnname FROM tablename;
Q- 18 how we will Show selected records sorted in an ascending (asc) or descending (desc)
A- 18 mysql> SELECT col1,col2 FROM tablename ORDER BY col2 DESC;

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

Q- 19 how to Return total number of rows.
A- 19 mysql> SELECT COUNT(*) FROM tablename;
Q- 20 How to Join tables on common columns.
A- 20 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
Q- 21 How to Creating a new user. Login as root. Switch to the MySQL db. Make the user. Update privs.
A- 21 # mysql -u root -p

mysql> use mysql;

mysql> INSERT INTO user (Host,User,Password) VALUES('%','username',PASSWORD('password'));

mysql> flush privileges;
Q- 22 How to Change a users password from unix shell.
A- 22 # [mysql dir]/bin/mysqladmin -u username -h hostname.blah.org -p password 'new-password'
Q- 23 How to Change a users password from MySQL prompt. Login as root. Set the password. Update privs.
A- 23 # mysql -u root -p

mysql> SET PASSWORD FOR 'user'@'hostname' = PASSWORD('passwordhere');

mysql> flush privileges;
Q- 24 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.
A- 24 # /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
Q- 25 How to Set a root password if there is on root password.
A- 25 # mysqladmin -u root password newpassword
Q- 26 How to Update a root password.
A- 26 # mysqladmin -u root -p oldpassword newpassword
Q- 27 How to allow the user "sonia" to connect to the server from localhost using the password "passwd". Login as root. Switch to the MySQL db. Give privs. Update privs.
A- 27 # mysql -u root -p
mysql> use mysql;
mysql> grant usage on *.* to sonia@localhost identified by 'passwd';
mysql> flush privileges;
Q- 28 How to give user privilages for a db. Login as root. Switch to the MySQL db. Grant privs. Update privs.
A- 28 # 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;
Q- 29 How To update info already in a table and Delete a row(s) from a table.
A- 29 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';
Q- 30 How to Update database permissions/privilages.
A- 30 mysql> flush privileges;
Q- 31 How to Delete a column and Add a new column to database
A- 31 mysql> alter table [table name] drop column [column name];
mysql> alter table [table name] add column [new column name] varchar (20);
Q- 32 Change column name and Make a unique column so we get no dupes.
A- 32 mysql> alter table [table name] change [old column name] [new column name] varchar (50);
mysql> alter table [table name] add unique ([column name]);
Q- 33 How to make a column bigger and Delete unique from table.
A- 33 mysql> alter table [table name] modify [column name] VARCHAR(3);
mysql> alter table [table name] drop index [colmn name];
Q- 34 How to Load a CSV file into a table
A- 34 mysql> LOAD DATA INFILE '/tmp/filename.csv' replace INTO TABLE [table name] FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1,field2,field3);
Q- 35 How to dump all databases for backup. Backup file is sql commands to recreate all db's.
A- 35 # [mysql dir]/bin/mysqldump -u root -ppassword --opt >/tmp/alldatabases.sql
Q- 36 How to dump one database for backup.
A- 36 # [mysql dir]/bin/mysqldump -u username -ppassword --databases databasename >/tmp/databasename.sql
Q- 37 How to dump a table from a database.
A- 37 # [mysql dir]/bin/mysqldump -c -u username -ppassword databasename tablename > /tmp/databasename.tablename.sql
Q- 38 Restore database (or database table) from backup.
A- 38 # [mysql dir]/bin/mysql -u username -ppassword databasename < /tmp/databasename.sql
Q- 39 How to Create Table show Example
A- 39 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));
Q- 40 How to search second maximum(second highest) salary value(integer)from table employee (field salary)in the manner so that mysql gets less load?
A- 40 By below 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 salary < (select max(salary) from employe) order by salary DESC limit 1 ;

6 comments:

  1. How do you handle Transactions in MySql?

    ReplyDelete
    Replies
    1. COMMIT and ROLLBACK:
      These two keywords Commit and Rollback are mainly used for MySQL Transactions.

      When a successful transaction is completed, the COMMIT command should be issued so that the changes to all involved tables will take effect.

      If a failure occurs, a ROLLBACK command should be issued to return every table referenced in the transaction to its previous state.
      You can control the behavior of a transaction by setting session variable called AUTOCOMMIT. If AUTOCOMMIT is set to 1 (the default), then each SQL statement (within a transaction or not) is considered a complete transaction, and committed by default when it finishes. When AUTOCOMMIT is set to 0, by issuing the SET AUTOCOMMIT=0 command, the subsequent series of statements acts like a transaction, and no activities are committed until an explicit COMMIT statement is issued.

      Delete
  2. Describe MyISAM table?
    What is Query Cache in MySQL?
    What are the Performance and Scalability? characteristics of MySQL?

    ReplyDelete
    Replies
    1. MyISAM:
      Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type. An .frm file stores the table format. The data file has an .MYD (MYData) extension. The index file has an .MYI (MYIndex) extension.

      To specify explicitly that you want a MyISAM table, indicate that with an ENGINE table option:

      CREATE TABLE t (i INT) ENGINE = MYISAM;
      QUERY CACHE:
      The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.

      The query cache can be useful in an environment where you have tables that do not change very often and for which the server receives many identical queries. This is a typical situation for many Web servers that generate many dynamic pages based on database content.

      Delete
  3. What is the difference between split() and explode()

    split can use regex, while explode uses a fixed string.
    Split has been DEPRECATED as of PHP 5.3.0. Use explode() instead.

    ReplyDelete
  4. what are difference between PHP 5.2 and 5.3

    Backward Incompatible changes

    clearstatcache() no longer clears the realpath cache by default.
    realpath() is now fully platform-independent. Consequence of this is that invalid relative paths such as __FILE__ . "/../x" do not work anymore.
    The array functions natsort(), natcasesort(), usort(), uasort(), uksort(), array_flip(), and array_unique() no longer accept objects passed as arguments. To apply these functions to an object, cast the object to an array first.
    The magic methods __get, __set, __isset, __unset, and __call must always be public and can no longer be static. Method signatures are now enforced.

    New Features

    Support for jump labels (limited goto) has been added.
    The ternary operator now has a shorthand form: ?:.
    The HTTP stream wrapper now considers all status codes from 200 to 399 to be successful.
    Exceptions can now be nested.
    There are two new magic methods, __callStatic and __invoke.
    It is now possible to use Heredocs to initialize static variables and class properties/constants.

    Deprecated Functions

    session_register() (use the $_SESSION superglobal instead)
    session_unregister() (use the $_SESSION superglobal instead)
    session_is_registered() (use the $_SESSION superglobal instead)
    split() (use preg_split() instead)
    mysql_db_query() (use mysql_select_db() and mysql_query() instead)
    mysql_escape_string() (use mysql_real_escape_string() instead)
    ereg() (use preg_match() instead)

    ReplyDelete