Mention 4 features of MySQL.
· A privilege and password system that is very flexible and secure, and that allows host-based verification.
· Passwords are secure because all password traffic is encrypted when you connect to a server.
Scalability and Limits:
· Handles large databases. We use MySQL Server with databases that contain 50 million records. We also know of users who use MySQL Server with 60,000 tables and about 5,000,000,000 rows.
· Up to 64 indexes per table are allowed (32 before MySQL 4.1.2). Each index may consist of 1 to 16 columns or parts of columns. The maximum index width is 1000 bytes (767 for
InnoDB); before MySQL 4.1.2, the limit is 500 bytes. An index may use a prefix of a column for
TEXT column types.
· Clients can connect to MySQL Server using several protocols:
– Clients can connect using TCP/IP sockets on any platform.
– On Windows systems in the NT family (NT, 2000, XP, 2003, or Vista), clients can connect using named pipes if the server is started with the
– option. In MySQL 4.1 and higher, Windows servers also support shared-memory connections if started with the
– option. Clients can connect through shared memory by using the
– On Unix systems, clients can connect using Unix domain socket files.
· MySQL client programs can be written in many languages. A client library written in C is available for clients written in C or C++, or for any language that provides C bindings.
· APIs for C, C++, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl are available, allowing MySQL clients to be written in many languages.
· The Connector/ODBC (MyODBC) interface provides MySQL support for client programs that use ODBC (Open Database Connectivity) connections.
For example, you can use MS Access to connect to your MySQL server. Clients can be run on Windows or Unix. MyODBC source is available. All ODBC 2.5 functions are supported, as are many others.
· The Connector/J interface provides MySQL support for Java client programs that use JDBC connections. Clients can be run on Windows or Unix. Connector/J source is available.
· MySQL Connector/NET enables developers to easily create .NET applications that require secure, high-performance data connectivity with MySQL. It implements the required ADO.NET interfaces and integrates into ADO.NET aware tools. Developers can build applications using their choice of .NET languages. MySQL Connector/NET is a fully managed ADO.NET driver written in 100% pure C#.
· The server can provide error messages to clients in many languages.
· Full support for several different character sets, including
ujis, and more. For example, the Scandinavian characters “
ä” and “
ö” are allowed in table and column names. Unicode support is available as of MySQL 4.1.
· All data is saved in the chosen character set.
· Sorting and comparisons are done according to the chosen character set and collation (using
latin1 and Swedish collation by default). It is possible to change this when the MySQL server is started. To see an example of very advanced sorting, look at the Czech sorting code. MySQL Server supports many different character sets that can be specified at compile time and runtime.
· As of MySQL 4.1, the server time zone can be changed dynamically, and individual clients can specify their own time zone.
Write the differences between char and varchar data types in MySQL.
The CHAR and VARCHAR types are similar, but differ in the way they are stored and retrieved. They also differ in maximum length and in whether trailing spaces are retained.
The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.
The length of a CHAR column is fixed to the length that you declare when you create the table. The length can be any value from 0 to 255. When CHAR values are stored, they are right-padded with spaces to the specified length. When CHAR values are retrieved, trailing spaces are removed unless the PAD_CHAR_TO_FULL_LENGTH SQL mode is enabled.
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
In contrast to CHAR, VARCHAR values are stored as a one-byte or two-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.
If strict SQL mode is not enabled and you assign a value to a CHAR or VARCHAR column that exceeds the column’s maximum length, the value is truncated to fit and a warning is generated. For truncation of non-space characters, you can cause an error to occur (rather than a warning) and suppress insertion of the value by using strict SQL mode.
For VARCHAR columns, trailing spaces in excess of the column length are truncated prior to insertion and a warning is generated, regardless of the SQL mode in use. For CHAR columns, truncation of excess trailing spaces from inserted values is performed silently regardless of the SQL mode.
VARCHAR values are not padded when they are stored. Trailing spaces are retained when values are stored and retrieved, in conformance with standard SQL.
Write about the operations supported by indexes in MySQL.
Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more is the cost. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. If a table has 1,000 rows, this is at least 100 times faster than reading sequentially. If you need to access most of the rows, it is faster to read sequentially, because this minimizes disk seeks. If a multiple-column index exists on
col2, the appropriate rows can be fetched directly. If separate single-column indexes exist on
col2, the optimizer will attempt to use the Index Merge optimization, or attempt to find the most restrictive index by deciding which index finds fewer rows and using that index to fetch the rows.
Index Merge optimization: The Index Merge method is used to retrieve rows with several
range scans and to merge their results into one. The merge can produce unions, intersections, or unions-of-intersections of its underlying scans. This access method merges index scans from a single table; it does not merge scans across multiple tables.MySQL uses indexes for these operations:
· To find the rows matching a
WHERE clause quickly.
· To eliminate rows from consideration. If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows.
· To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more efficiently if they are declared as the same type and size. In this context,
CHAR are considered the same if they are declared as the same size. For example,
CHAR(10) are the same size, but
CHAR(15) are not.
Comparison of dissimilar columns may prevent use of indexes if values cannot be compared directly without conversion. Suppose that a numeric column is compared to a string column. For a given value such as
1 in the numeric column, it might compare equal to any number of values in the string column such as
'01.e1'. This rules out use of any indexes for the string column.
· To find the MIN() or MAX() value for a specific indexed column
key_col. This is optimized by a preprocessor that checks whether you are using
constant on all key parts that occur before
key_col in the index. In this case, MySQL does a single key lookup for each MIN() or MAX() expression and replaces it with a constant. If all expressions are replaced with constants, the query returns at once.
Write the SQL statements to demonstrate the following using SELECT command:
b. Using table aliases
Define the following types of Joins:
a. Inner Join
they are also known as Equi Joins. They are so called because the where statement generally compares two columns from two tables with the equivalence operator =. Many systems use this type as the default join. This type can be used in situations where selecting only those rows that have values in common in the columns specified in the ON clause, is required. In short, the Inner Join returns all rows from both tables where there is a match.
mysql> SELECT ProdName, Quantity FROM Products, Sales WHERE Products.ProdID = Sales.ProdID AND Sales.Quantity > 2000;
b. Left Outer Join
In this type, all the records from the table on the left side of the join and matching the WHERE clause in appear in the final result set.
mysql> SELECT * FROM users LEFT JOIN users_groups ON users.uid = users_groups.uid;
Right Outer Join
All the records matching the WHERE clause from the table on the right appear.
mysql> SELECT * FROM users_groups RIGHT JOIN groups USING (gid);
Give the advantages of subqueries.
A Subquery is exactly what it sounds like: a SELECT query that is subordinate to another query. This unit covers the various concepts of subqueries.
1. The subquery as scalar operand: A scalar subquery is a simple operand, and you can use it almost anywhere a single column value or literal is legal, and you can expect it to have those characteristics that all operands have: a data type, a length, an indication whether it can be
NULL, and so on.
2. Comparisons Using Subqueries: The output of the subqueries can be used to compare the output of the outer most queries. This can be done using various comparison operators like >, <, IN, and so on.
3. Subqueries with ANY, IN, and SOME: These type of subqueries can be used in cases where a set of values need to be compared and the output determined.
4. Subqueries with ALL: These type of subqueries can be used in cases where a set of values are the output generated by the inner or subqueries and the output is possible only if all the values match with the outer query.
5. Correlated Subqueries: Sometimes a situation arises in which a subquery uses a field from the main query in its clause. Such a reference by a subquery to a field in its enclosing query, is called an outer reference, and the corresponding subquery is called a correlated subquery, because it’s correlated with the result set of one or more of the queries enclosing it.
6. EXISTS and NOT EXISTS: The Exists operator can be used to check if a subquery produces any results at all. The NOT EXISTS operator is exactly the opposite of the output produced by NOT EXISTS.
7. Row Subqueries: A row subquery is a subquery variant that returns a single row and can thus return more than one column value.
Subqueries in the FROM Clause: Subqueries are legal in a
FROMclause. Any columns in the
subqueryselect list must have unique names.
Discuss any five string functions.
String-valued functions return NULL if the length of the result would be greater than the value of the max_allowed_packet system variable. For functions that operate on string positions, the first position is numbered 1.
1,ASCII(str)-Returns the numeric value of the leftmost character of the string str. Returns 0 if str is the empty string. Returns NULL if str is NULL. ASCII() works for characters with numeric values from 0 to 255.1. SELECT ASCII(’2′);
2,BIN(N)-Returns a string representation of the binary value of N, where N is a longlong (BIGINT) number. SELECT BIN(12);
3,BIT_LENGTH(str)-Returns the length of the string str in bits.SELECT BIT_LENGTH(’text’);
4,CHAR(N,…)–CHAR() interprets the arguments as integers and returns a string consisting of the characters given by the code values of those integers. NULL values are skipped.1.SELECT CHAR(77,121,83,81,’76′);SELECT INSERT(’Quadratic’, 3, 4, ‘What’);
5,INSERT(str,pos,len,newstr)–Returns the string str, with the substring beginning at position pos and len characters long replaced by the string newstr.
Describe the operators that support the boolean full-text searches.
MySQL can perform boolean full-text searches using the
+: A leading plus sign indicates that this word must be present in each row that is returned.
-: A leading minus sign indicates that this word must not be present in any of the rows that are returned.
Note: The – operator acts only to exclude rows that are otherwise matched by other search terms. Thus, a boolean-mode search that contains only terms preceded by – returns an empty result. It does not return “all rows except those containing any of the excluded terms.”
· (no operator): By default (when neither + nor – is specified) the word is optional, but the rows that contain it are rated higher. This mimics the behavior of MATCH() … AGAINST() without the IN BOOLEAN MODE modifier.
>: These two operators are used to change a word’s contribution to the relevance value that is assigned to a row. The
> operator increases the contribution and the
< operator decreases it.
Parentheses group words into subexpressions. Parenthesized groups can be nested.
~: A leading tilde acts as a negation operator, causing the word's
contribution to the row's relevance to be negative. This is useful
for marking “noise” words. A row containing such a word is rated
lower than others, but is not excluded altogether, as it would be
with the - operator.
*: The asterisk serves as the truncation (or wildcard) operator. Unlike the other operators, it should be appended to the word to be affected. Words match if they begin with the word preceding the
If a stopword or too-short word is specified with the truncation operator, it will not be stripped from a boolean query. For example, a search for
'+word will likely return fewer rows than a search for
'+word because the former query remains as is and requires
stopword* to be present in a document. The latter query is transformed to
": A phrase that is enclosed within double quote (“
"”) characters matches only rows that contain the phrase literally, as it was typed. The full-text engine splits the phrase into words, performs a search in the
FULLTEXT index for the words. Non-word characters need not be matched exactly: Phrase searching requires only that matches contain exactly the same words as the phrase and in the same order.
Describe the Savepoint and Rollback to Savepoint Syntaxes.
Starting from MySQL 4.0.14 and 4.1.1, InnoDB supports the SQL statements SAVEPOINT and ROLLBACK TO SAVEPOINT.
The SAVEPOINT statement sets a named transaction savepoint with a name of identifier. If the current transaction already has a savepoint with the same name, the old savepoint is deleted and a new one is set.
The ROLLBACK TO SAVEPOINT statement rolls back a transaction to the named savepoint. Modifications that the current transaction made to rows after the savepoint was set are undone in the rollback, but InnoDB does not release the row locks that were stored in memory after the savepoint. (Note that for a new inserted row, the lock information is carried by the transaction ID stored in the row; the lock is not separately stored in memory. In this case, the row lock is released in the undo.) Savepoints that were set at a later time than the named savepoint are deleted.
Give the syntax of BEGIN…END compound statement.
Stored routines may contain multiple statements, using a BEGIN … END compound statement. begin_label and end_label must be the same, if both are specified. Please note that the optional [NOT] ATOMIC clause is not yet supported. This means that no transactional savepoint is set at the start of the instruction block and the BEGIN clause used in this context has no effect on the current transaction.
Multiple statements requires that a client is able to send query strings containing ‘;’. This is handled in the mysql command-line client with the delimiter command. Changing the ‘;’ end-of-query delimiter (for example, to ‘|’) allows ‘;’ to be used in a routine body.