MySQL Programming

  Home  Databases Programming  MySQL Programming


“Learn MySQL Programming with interview questions and answers”



110 MySQL Programming Questions And Answers

21⟩ What Is Row?

A row is a unit of data with related data items stored as one item in one column in a table.

 191 views

22⟩ Explain what Is MySQL?

MySQL is an open source database management system developed by MySQL AB, http://www.mysql.com. MySQL has the following main features:

* Works on many different platforms.

* APIs for C, C++, Eiffel, Java, Perl, PHP, Python, Ruby, and Tcl are available.

* Fully multi-threaded using kernel threads. It can easily use multiple CPUs if they are available.

* Provides transactional and non-transactional storage engines.

* Uses very fast B-tree disk tables (MyISAM) with index compression.

* A very fast thread-based memory allocation system.

* Very fast joins using an optimized one-sweep multi-join.

* In-memory hash tables, which are used as temporary tables.

* SQL functions are implemented using a highly optimized class library and should be as fast as possible. Usually there is no memory allocation at all after query initialization.

* The server is available as a separate program for use in a client/server networked environment. It is also available as a library that can be embedded (linked) into standalone applications. Such applications can be used in isolation or in environments where no network is available.

 195 views

23⟩ How To Create a Test Table in Your MySQL Server?

If you want to create a test table in your MySQL server, you can use the "mysql" program in a command window as shown in the following tutorial:

>cd mysqlin

>mysql -u root

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 8 to server version: 5.0.24-

mysql> use test

Database changed

mysql> CREATE TABLE test (message VARCHAR(80));

Query OK, 0 rows affected (0.13 sec)

mysql> INSERT INTO test

-> VALUES ('Welcome to GlobalGuideLine.com');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM test;

+---------------------------------+

| message |

+---------------------------------+

| Welcome to GlobalGuideLine.come |

+---------------------------------+

1 row in set (0.00 sec)

mysql> DROP TABLE test;

Query OK, 0 rows affected (0.01 sec)

mysql> quit

Bye

 216 views

24⟩ What Is Primary Key?

A primary key is a single column or multiple columns defined to have unique values that can be used as row identifications.

 204 views

25⟩ How To Shutdown MySQL Server?

If you want to shutdown your MySQL server, you can run the "mysqladmin" program in a command window as shown in the following tutorial:

>cd mysqlin

>mysqladmin shutdown

 200 views

26⟩ How Do You Know If Your MySQL Server Is Alive?

If you want to know whether your MySQL server is alive, you can use the "mysqladmin" program in a command window as shown in the following tutorial:

>cd mysqlin

>mysqladmin -u root ping

mysqld is alive

The "mysqld is alive" message tells you that your MySQL server is running ok. If your MySQL server is not running, you will get a "connect ... failed" message.

 195 views

28⟩ What Tools Available for Managing MySQL Server?

MySQL comes with the following programs as administration tools for you to manage your MySQL server:

► mysqld - MySQL server daemon. You can use "mysqld" to start your MySQL server.

► mysqladmin - A command-line interface for administrators to perform server administration tasks.

► mysql - A command-line interface for end users to manage user data objects.

► mysqlcheck - A command-line interface for administrators to check and repair tables.

► mysqlshow - A command-line interface for end users to see information on tables and columns.

► mysqldump - A command-line interface for administrators or end users to export data from the server to files.

► mysqlimport - A command-line interface for administrators or end users to load data files into tables program tool to load data into tables.

 212 views

29⟩ What Is "mysqld"?

"mysqld" is MySQL server daemon program which runs quietly in background on your computer system. Invoking "mysqld" will start the MySQL server on your system. Terminating "mysqld" will shutdown the MySQL server. Here is a tutorial example of invoking "mysqld" with the "--console" option:

>cd mysqlin

>mysqld --console

... 21:52:54 InnoDB: Started; log sequence number 0 43655

... 21:52:54 [Note] mysqld: ready for connections.

Version: '5.0.24-community' socket: '' port: 3306

MySQL Community Edition (GPL)

The returning message indicates that "mysqld" running now, and your MySQL server is ready to take client connections. To know about "mysqld", read other parts of this FAQ collection.

 205 views

30⟩ What Is "mysqladmin" in MySQL?

"mysqladmin" is a command-line interface for administrators to perform server administration tasks. It support a number of commonly used commands like:

► "mysqladmin shutdown" - Shuts down the server.

► "mysqladmin ping" - Checks if the server is alive or not.

► "mysqladmin status" - Displays several important server status values.

► "mysqladmin version" - Displays version information of the server.

► "mysqladmin create databaseName" - Creates a new database.

► "mysqladmin drop databaseName" - Drops an existing database.

 221 views

31⟩ How To Check Server Status with "mysqladmin"?

If you want to check the server status by with "mysqladmin", you can following this tutorial example:

>cd mysqlin

>mysqladmin -u root status

Uptime: 223 Threads: 1 Questions: 1 Slow queries: 0

Opens: 12 Flush tables: 1 Open tables: 6

Queries per second avg: 0.004

The returning message indicates that the server is almost doing nothing at this moment.

 210 views

32⟩ How To Shut Down the Server with "mysqladmin"?

If you want to shut down the server with "mysqladmin", you can use the command "mysqladmin shutdown" as shown in the following tutorial example:

>cd mysqlin

>mysqladmin -u root shutdown

If this command returns no messages, your MySQL server should be terminated successfully.

 189 views

33⟩ How To Use "mysql" to Run SQL Statements?

If you want to run SQL statement to your server with "mysql", you need to start "mysql" and enter your SQL statement at the "mysql" prompt. Here is a good tutorial exercise that shows you how to run two SQL statements with "mysql":

>cd mysqlin

>mysql -u root test

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 4 to server version: 5.0.24

mysql> CREATE TABLE links (name VARCHAR(80));

Query OK, 0 rows affected (0.10 sec)

mysql> INSERT INTO links VALUES ('www.GlobalGuideLine.com');

Query OK, 1 row affected (0.02 sec)

mysql> quit;

Bye

 208 views

34⟩ How To Show All Tables with "mysql"?

If you want to see all the tables in a database, you run the non-SQL command "SHOW TABLES" at the "mysql" prompt. See the following tutorial exercise for example:

>cd mysqlin

>mysql -u root test

Welcome to the MySQL monitor. Commands end with ; or g.

Your MySQL connection id is 14 to server version: 5.0.24

mysql> SHOW TABLES;

+----------------+

| Tables_in_test |

+----------------+

| links |

+----------------+

1 row in set (0.01 sec)

The output shows you that there is only one table in the "test" database.

 223 views

35⟩ What Is "mysqlcheck"?

"mysqlcheck" is a command-line interface for administrators to check and repair tables. Here are some sample commands supported by "mysqlcheck":

► "mysqlcheck databaseName tableName" - Checks the specified table in the specified database.

► "mysqlcheck databaseName" - Checks all tables in the specified database.

► "mysqlcheck --all-databases" - Checks all tables in all databases.

► "mysqlcheck --analyze databaseName tableName" - Analyzes the specified table in the specified database.

► "mysqlcheck --repair databaseName tableName" - Repairs the specified table in the specified database.

 213 views

36⟩ How To Analyze Tables with "mysqlcheck"?

If you want analyze tables with "mysqlcheck", you need to use the "--analyze" option. The following tutorial exercise shows you how to analyze all tables in "mysql" database:

>cd mysqlin

>mysqlcheck -u root --analyze mysql

mysql.columns_priv   Table is already up to date

mysql.db Table is already up to date

mysql.func Table is already up to date

mysql.help_category Table is already up to date

mysql.help_keyword Table is already up to date

mysql.help_relation Table is already up to date

mysql.help_topic Table is already up to date

mysql.host Table is already up to date

mysql.proc Table is already up to date

mysql.tables_priv Table is already up to date

mysql.time_zone Table is already up to date

mysql.time_zone_leap_second Table is already up to date

mysql.time_zone_name Table is already up to date

mysql.time_zone_transition Table is already up to date

mysql.time_zone_transition_type Table is already up to date

mysql.user Table is already up to date

 203 views

37⟩ What Is "mysqlshow"?

"mysqlshow" is a command-line interface for end users to see information on tables and columns. Here are some sample commands supported by "mysqlshow":

► "mysqlshow" - Shows all the databases.

► "mysqlshow databaseName" - Shows all the tables in the specified database.

► "mysqlshow databaseName tableName" - Shows all the columns in the specified table.

► "mysqlshow --verbose" - Shows all the databases with extra information.

► "mysqlshow --verbose my%" - Shows all the databases who's names match the pattern "my%" with extra information.

► "mysqlshow --verbose mysql time%" - Shows all the tables who's names match the pattern "time%" in "mysql" database with extra information.

 203 views

38⟩ How To Show Table Names with "mysqlshow"?

If you want to show table names with "mysqlshow", you need to specify a database name. The followings tutorial exercise shows you how to get all table names that match a pattern:

If you want analyze tables with "mysqlcheck", you need to use the "--analyze" option. The following tutorial exercise shows you how to analyze all tables in "mysql" database:

>cd mysqlin

>mysqlshow --verbose mysql time%

Database: mysql Wildcard: time%

+---------------------------+----------+

| Tables | Columns |

+---------------------------+----------+

| time_zone | 2 |

| time_zone_leap_second | 2 |

| time_zone_name | 2 |

| time_zone_transition | 3 |

| time_zone_transition_type | 5 |

+---------------------------+----------+

5 rows in set.

 200 views

39⟩ What Is "mysqldump"?

"mysqldump" - A command-line interface for administrators or end users to export data from the server to files. Here are some sample commands supported by "mysqldump":

► "mysqldump databaseName tableName" - Dumps the specified table in the specified database.

► "mysqldump databaseName" - Dumps all the tables in the specified database.

 213 views

40⟩ How To Dump a Table to a File with "mysqldump"?

If you want to dump all rows in a table from the server to a file, you can use "mysqldump" with the "-f fileName" option as show in the following tutorial exercise:

>cd mysqlin

>mysqldump -u root -r emplinks.txt test links

>type emplinks.txt

>type emplinks.txt | more

-- MySQL dump 10.10

--

-- Host: localhost Database: test

-- ------------------------------------------------------

-- Server version 5.0.24-community

...

The dump file contains SQL statements that you can use to restore the table and its data content.

 204 views