Your cart is currently empty!
Category: mysqli
-
Khóa học miễn phí MySQL – Indexes nhận dự án làm có lương
MySQLi – Indexes
A database index is a data structure that improves the speed of operations in a table. Indexes can be created using one or more columns, providing the basis for both rapid random lookups and efficient ordering of access to records.
While creating index, it should be considered that what are the columns which will be used to make SQL queries and create one or more indexes on those columns.
Practically, indexes are also type of tables, which keep primary key or index field and a pointer to each record into the actual table.
The users cannot see the indexes, they are just used to speed up queries and will be used by Database Search Engine to locate records very fast.
INSERT and UPDATE statements take more time on tables having indexes where as SELECT statements become fast on those tables. The reason is that while doing insert or update, database need to insert or update index values as well.
Simple and Unique Index
You can create a unique index on a table. A unique index means that two rows cannot have the same index value. Here is the syntax to create an Index on a table.
CREATE UNIQUE INDEX index_name ON table_name ( column1, column2,...);
You can use one or more columns to create an index. For example, we can create an index on tutorials_inf using NAME_INDEX.
CREATE UNIQUE INDEX NAME_INDEX ON tutorials_inf(name);
You can create a simple index on a table. Just omit UNIQUE keyword from the query to create simple index. Simple index allows duplicate values in a table.
If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name.
mysql> CREATE UNIQUE INDEX NAME_INDEX ON tutorials_inf (name DESC);
ALTER command to add and drop INDEX
There are four types of statements for adding indexes to a table −
-
ALTER TABLE tbl_name ADD PRIMARY KEY (column_list) − This statement adds a PRIMARY KEY, which means that indexed values must be unique and cannot be NULL.
-
ALTER TABLE tbl_name ADD UNIQUE index_name (column_list) − This statement creates an index for which values must be unique (with the exception of NULL values, which may appear multiple times).
-
ALTER TABLE tbl_name ADD INDEX index_name (column_list) − This adds an ordinary index in which any value may appear more than once.
-
ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list) − This creates a special FULLTEXT index that is used for text-searching purposes.
Here is the example to add index in an existing table.
mysql> ALTER TABLE tutorials_inf ADD INDEX (id);
You can drop any INDEX by using DROP clause along with ALTER command. Try out the following example to drop above-created index.
mysql> ALTER TABLE tutorials_inf DROP INDEX (c);
You can drop any INDEX by using DROP clause along with ALTER command. Try out the following example to drop above-created index.
ALTER Command to add and drop PRIMARY KEY
You can add primary key as well in the same way. But make sure Primary Key works on columns, which are NOT NULL.
Here is the example to add primary key in an existing table. This will make a column NOT NULL first and then add it as a primary key.
mysql> ALTER TABLE tutorials_inf MODIFY id INT NOT NULL; mysql> ALTER TABLE tutorials_inf ADD PRIMARY KEY (id);
You can use ALTER command to drop a primary key as follows:
mysql> ALTER TABLE tutorials_inf DROP PRIMARY KEY;
To drop an index that is not a PRIMARY KEY, you must specify the index name.
Displaying INDEX Information
You can use SHOW INDEX command to list out all the indexes associated with a table. Vertical-format output (specified by G) often is useful with this statement, to avoid long line wraparound −
Try out the following example
mysql> SHOW INDEX FROM table_nameG ........
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc
-
Khóa học miễn phí MySQL – Administration nhận dự án làm có lương
MySQLi – Administration
Running and Shutting down MySQL Server
First check if your MySQL server is running or not. You can use the following command to check it −
ps -ef | grep mysqld
If your MySql is running, then you will see mysqld process listed out in your result. If server is not running, then you can start it by using the following command −
root@host# cd /usr/bin ./safe_mysqld &
Now, if you want to shut down an already running MySQL server, then you can do it by using the following command −
root@host# cd /usr/bin ./mysqladmin -u root -p shutdown Enter password: ******
Setting Up a MySQL User Account
For adding a new user to MySQL, you just need to add a new entry to the user table in the database mysql.
The following program is an example of adding a new user guest with SELECT, INSERT and UPDATE privileges with the password guest123; the SQL query is −
root@host# mysql -u root -p Enter password:******* mysql> use mysql; Database changed mysql> INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv) VALUES (''localhost'', ''guest'', PASSWORD(''guest123''), ''Y'', ''Y'', ''Y''); Query OK, 1 row affected (0.20 sec) mysql> FLUSH PRIVILEGES; Query OK, 1 row affected (0.01 sec) mysql> SELECT host, user, password FROM user WHERE user = ''guest +-----------+---------+------------------+ | host | user | password | +-----------+---------+------------------+ | localhost | guest | 6f8c114b58f2ce9e | +-----------+---------+------------------+ 1 row in set (0.00 sec)
When adding a new user, remember to encrypt the new password using PASSWORD() function provided by MySQL. As you can see in the above example, the password mypass is encrypted to 6f8c114b58f2ce9e.
Notice the FLUSH PRIVILEGES statement. This tells the server to reload the grant tables. If you don”t use it, then you won”t be able to connect to MySQL using the new user account at least until the server is rebooted.
You can also specify other privileges to a new user by setting the values of following columns in user table to ”Y” when executing the INSERT query or you can update them later using UPDATE query.
- Select_priv
- Insert_priv
- Update_priv
- Delete_priv
- Create_priv
- Drop_priv
- Reload_priv
- Shutdown_priv
- Process_priv
- File_priv
- Grant_priv
- References_priv
- Index_priv
- Alter_priv
Another way of adding user account is by using GRANT SQL command. The following example will add user zara with password zara123 for a particular database, which is named as TUTORIALS.
root@host# mysql -u root -p password; Enter password:******* mysql> use mysql; Database changed mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP → ON TUTORIALS.* → TO ''zara''@''localhost'' → IDENTIFIED BY ''zara123
This will also create an entry in the MySQL database table called as user.
NOTE − MySQL does not terminate a command until you give a semi colon (;) at the end of the SQL command.
The /etc/my.cnf File Configuration
In most of the cases, you should not touch this file. By default, it will have the following entries −
[mysqld] datadir = /var/lib/mysql socket = /var/lib/mysql/mysql.sock [mysql.server] user = mysql basedir = /var/lib [safe_mysqld] err-log = /var/log/mysqld.log pid-file = /var/run/mysqld/mysqld.pid
Here, you can specify a different directory for the error log, otherwise you should not change any entry in this table.
Administrative MySQL Command
Here is the list of the important MySQL commands, which you will use time to time to work with MySQL database −
-
USE Databasename − This will be used to select a database in the MySQL workarea.
-
SHOW DATABASES − Lists out the databases that are accessible by the MySQL DBMS.
-
SHOW TABLES − Shows the tables in the database once a database has been selected with the use command.
-
SHOW COLUMNS FROM tablename: Shows the attributes, types of attributes, key information, whether NULL is permitted, defaults, and other information for a table.
-
SHOW INDEX FROM tablename − Presents the details of all indexes on the table, including the PRIMARY KEY.
-
SHOW TABLE STATUS LIKE tablenameG − Reports details of the MySQL DBMS performance and statistics.
Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc