mysql : Interview Questions
MySQL is an open source DBMS which is built, supported and distributed by MySQL AB (now acquired by Oracle)
MySQL database software is a client or server system which includes
- Multithreaded SQL server supporting various client programs and libraries
- Different backend
- Wide range of application programming interfaces and
- Administrative tools.
MySQL database server is reliable, fast and very easy to use. This software can be downloaded as freeware and can be downloaded from the internet.
HEAP tables are present in memory and they are used for high speed storage on temporary
• BLOB or TEXT fields are not allowed
• Only comparison operators can be used =, <,>, = >,=<
• AUTO_INCREMENT is not supported by HEAP tables
• Indexes should be NOT NULL
The default port for MySQL server is 3306.
- MySQL is open source software which is available at any time and has no cost involved.
- MySQL is portable
- GUI with command prompt.
- Administration is supported using MySQL Query Browser
Following are differences for FLOAT and DOUBLE:
• Floating point numbers are stored in FLOAT with eight place accuracy and it has four bytes.
• Floating point numbers are stored in DOUBLE with accuracy of 18 places and it has eight bytes.
CHAR_LENGTH is character count whereas the LENGTH is byte count. The numbers are same for Latin characters but they are different for Unicode and other encodings.
ENUMs and SETs are used to represent powers of two because of storage optimizations.
ENUM is a string object used to specify set of predefined values and that can be used during table creation.
Create table size(name ENUM('Small', 'Medium','Large');
REGEXP is a pattern match in which matches pattern anywhere in the search value.
Following are the differences between CHAR and VARCHAR:
- CHAR and VARCHAR types differ in storage and retrieval
- CHAR column length is fixed to the length that is declared while creating table. The length value ranges from 1 and 255
- When CHAR values are stored then they are right padded using spaces to specific length. Trailing spaces are removed when CHAR values are retrieved.
The string types are:
is used to get the current version of MySQL.
SELECT VERSION ();
Storage engines are called table types and data is stored in files using various techniques.
- Storage mechanism
- Locking levels
- Capabilities and functions.
Following are the drivers available in MySQL:
- PHP Driver
- JDBC Driver
- ODBC Driver
- C WRAPPER
- PYTHON Driver
- PERL Driver
- RUBY Driver
- CAP11PHP Driver
TIMESTAMP column is updated with Zero when the table is created. UPDATE CURRENT_TIMESTAMP modifier updates the timestamp field to current time whenever there is a change in other fields of the table.
Every row of a table is identified uniquely by primary key. There is only one primary key for a table.
Primary Key is also a candidate key. By common convention, candidate key can be designated as primary and which can be used for any foreign key references.
We can login through this command:
# [mysql dir]/bin/mysql -h hostname -u
It compress the MyISAM tables, which reduces their disk or memory usage.
Maximum size of Heal table can be controlled by MySQL config variable called max_heap_table_size.
In MyISAM static all the fields will have fixed width. The Dynamic MyISAM table will have fields like TEXT, BLOB, etc. to accommodate the data types with various lengths.
MyISAM Static would be easier to restore in case of corruption.
Federated tables which allow access to the tables located on other databases on other servers.
Timestamp field gets the current timestamp whenever the row gets altered.
It stops incrementing. Any further inserts are going to produce an error, since the key has been used already.
LAST_INSERT_ID will return the last value assigned by Auto_increment and it is not required to specify the table name.
Indexes are defined for the table by:
SHOW INDEX FROM
% corresponds to 0 or more characters, _ is exactly one character in the LIKE statement.
UNIX_TIMESTAMP is the command which converts from MySQL timestamp to Unix timestamp
FROM_UNIXTIME is the command which converts from Unix timestamp to MySQL timestamp.
The = , <>, <=, <, >=, >,<<,>>, <=>, AND, OR, or LIKE operators are used in column comparisons in SELECT statements.
Number of rows can be obtained by
SELECT COUNT (user_id) FROM users;
All these examples are same. It is not case sensitive.
SELECT VERSION(), CURRENT_DATE; SeLect version(), current_date; seleCt vErSiOn(), current_DATE;
LIKE and REGEXP operators are used to express with ^ and %.
SELECT * FROM employee WHERE emp_name REGEXP "^b"; SELECT * FROM employee WHERE emp_name LIKE "%b";
A BLOB is a binary large object that can hold a variable amount of data. There are four types of BLOB –
- MEDIUMBLOB and
They all differ only in the maximum length of the values they can hold.
A TEXT is a case-insensitive BLOB. The four TEXT types
- MEDIUMTEXT and
They all correspond to the four BLOB types and have the same maximum lengths and storage requirements.
The only difference between BLOB and TEXT types is that sorting and comparison is performed in case-sensitive for BLOB values and case-insensitive for TEXT values.
Following are the differences between mysql_fetch_array and mysql_fetch_object:
mysql_fetch_array() -Returns a result row as an associated array or a regular array from database.
mysql_fetch_object – Returns a result row as object from database.
Following commands are used to run in batch mode:
mysql ; mysql mysql.out
Each MyISAM table is stored on disk in three formats:
- The ‘.frm’ file stores the table definition
- The data file has a ‘.MYD’ (MYData) extension
- The index file has a ‘.MYI’ (MYIndex) extension
Total 5 types of tables are present:
- INNO DB
ISAM is abbreviated as Indexed Sequential Access Method.It was developed by IBM to store and retrieve data on secondary storage systems like tapes.
lnnoDB is a transaction safe storage engine developed by Innobase Oy which is a Oracle Corporation now.
DISTINCT is converted to a GROUP BY on all columns and it will be combined with ORDER BY clause.
SELECT DISTINCT t1.a FROM t1,t2 where t1.a=t2.a;
If you want to enter characters as HEX numbers, you can enter HEX numbers with single quotes and a prefix of (X), or just prefix HEX numbers with (Ox).
A HEX number string will be automatically converted into a character string, if the expression context is a string.
In MySql, top 50 rows are displayed by using this following query:
SELECT * FROM LIMIT 0,50;
Maximum of 16 indexed columns can be created for any standard table.
NOW () command is used to show current year,month,date with hours,minutes and seconds.
CURRENT_DATE() shows current year,month and date only.
Following objects are created using CREATE statement:
SIX triggers are allowed in MySql table. They are as follows:
- BEFORE INSERT
- AFTER INSERT
- BEFORE UPDATE
- AFTER UPDATE
- BEFORE DELETE and
- AFTER DELETE
Following are Non-Standard string types:
CONCAT(A, B) – Concatenates two string values to create a single string output. Often used to combine two or more fields into one single field.
FORMAT(X, D) – Formats the number X to D significant digits.
CURRDATE(), CURRTIME() – Returns the current date or time.
NOW() – Returns the current date and time as one value.
MONTH(), DAY(), YEAR(), WEEK(), WEEKDAY() – Extracts the given data from a date value.
HOUR(), MINUTE(), SECOND() – Extracts the given data from a time value.
DATEDIFF(A, B) – Determines the difference between two dates and it is commonly used to calculate age
SUBTIMES(A, B) – Determines the difference between two times.
FROMDAYS(INT) – Converts an integer number of days into a date value.
An ACL (Access Control List) is a list of permissions that is associated with an object. This list is the basis for MySQL server’s security model and it helps in troubleshooting problems like users not being able to connect.
MySQL keeps the ACLs (also called grant tables) cached in memory. When a user tries to authenticate or run a command, MySQL checks the authentication information and permissions against the ACLs, in a predetermined order.