Found 6702 Articles for Database

Is the primary key automatically indexed in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

2K+ Views

Yes, primary key is automatically indexed in MySQL because primary key, index, etc gets stored into B-trees. All engines including InnoDB as well as MyISAM automatically supports the primary key to be indexed. The primary key is implicitly indexed in InnoDB, MyISAM, and other engines. Let us create a table with primary key − mysql> create table DemoIndex -> ( -> Id int not null, -> primary key(Id) -> ); Query OK, 0 rows affected (1.21 sec) In the above table, Id is implicitly indexed.

Get record count for all tables in MySQL database?

Arjun Thakur
Updated on 22-Oct-2023 02:47:04

23K+ Views

To get the count of all the records in MySQL tables, we can use TABLE_ROWS with aggregate function SUM. The syntax is as follows. SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'yourDatabaseName'; Apply the above syntax in order to get the count of records for all tables. The query is as follows − mysql> SELECT SUM(TABLE_ROWS) ->FROM INFORMATION_SCHEMA.TABLES ->WHERE TABLE_SCHEMA = 'business'; The following table returns the count of records. +-----------------+ | SUM(TABLE_ROWS) | +-----------------+ | ... Read More

Count the number of occurrences of a string in a VARCHAR field in MySQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:23

4K+ Views

To count the number of occurrences of a string in a VARCHAR, we can use the logic of subtraction with length. First, we will create a table with the help of create command. mysql> create table StringOccurrenceDemo -> ( -> Cases varchar(100), -> StringValue varchar(500) -> ); Query OK, 0 rows affected (0.56 sec) After executing the above table, we will insert records into the table. The query is as follows − mysql> insert into StringOccurrenceDemo values('First', 'This is MySQL Demo and MySQL is ... Read More

MySQL status in terms of active or total connections?

George John
Updated on 30-Jul-2019 22:30:23

7K+ Views

The active or total connection can be known with the help of threads_connected variable. The variable tells about the number of currently open connections. The query is as follows − mysql> show status where `variable_name` = 'Threads_connected'; Here is the output. +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Threads_connected | 1 | +-------------------+-------+ 1 row in set (0.06 sec) We can check the same with the help of show command. The query is as follows − mysql> show processlist; Here is the output. +----+-----------------+-----------------+----------+---------+--------+------------------------+------------------+ ... Read More

Find rows that have the same value on a column in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:23

2K+ Views

First, we will create a table and insert some values into the table. Let us create a table. mysql> create table RowValueDemo -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.69 sec) Insert records using the insert command. We have added duplicate values as well for our example. mysql> insert into RowValueDemo values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowValueDemo values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into RowValueDemo values('Carol'); Query OK, 1 row affected ... Read More

length() vs char_length() in MySQL?

Ankith Reddy
Updated on 27-Jun-2020 07:39:24

773 Views

The char_length() can be used to display the length of a string. Let us see an example to get the length of the string included as a parameter.mysql> select char_length('John');The following is the output.+---------------------+ | char_length('John') | +---------------------+ | 4 | +---------------------+ 1 row in set (0.00 sec)The length() function can be used to display the length of string measured in bytes. In many cases characters and bytes gives the same length.Here is an example of length()mysql> select length('Tim'); The following is ... Read More

Get the new record key ID from MySQL insert query?

George John
Updated on 30-Jul-2019 22:30:23

474 Views

We can get new record key with the help of LAST_INSERT_ID() function from MySQL. First, we will create a table and for inserting record, we will use LAST_INSERT_ID(). Let us create a table with the help of create command. The query is as follows − mysql> create table LastInsertRecordIdDemo -> ( -> id int auto_increment, -> value varchar(100), -> primary key(id) -> ); Query OK, 0 rows affected (0.52 sec) After creating a table, we will insert records and set it using LAST_INSERT_ID() ... Read More

How to subtract 10 days from the current datetime in MySQL?

George John
Updated on 30-Jul-2019 22:30:23

2K+ Views

Firstly, let us get the current datetime with the help of the now() function. mysql> select now(); The following is the output. +---------------------+ | now()               | +---------------------+ | 2018-11-01 19:55:56 | +---------------------+ 1 row in set (0.00 sec) Syntax to subtract 10 days with the help of DATE_SUB() select DATE_SUB(now(),interval integer_value day ); Applying the above syntax to subtract 10 days from the current datetime. mysql> select DATE_SUB(now(),interval 10 day); Here is the output. +---------------------------------+ | DATE_SUB(now(),interval 10 day) | +---------------------------------+ | 2018-10-22 19:56:07             | +---------------------------------+ 1 row in set (0.00 sec)

How can I tell when a MySQL table was last updated?

Ankith Reddy
Updated on 30-Jul-2019 22:30:23

8K+ Views

We can know that with the help of the column name ‘UPDATED_TIME’ using information_schema.tables with WHERE clause. Let us first create a table for our example. mysql> create table MyISAMTableDemo   -> (   -> id int   -> ); Query OK, 0 rows affected (0.56 sec) Inserting some records into table. mysql> insert into MyISAMTableDemo values(1); Query OK, 1 row affected (0.72 sec) mysql> insert into MyISAMTableDemo values(2); Query OK, 1 row affected (0.16 sec) Syntax to know the last updated time. SELECT UPDATE_TIME FROM   information_schema.tables WHERE  TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = ... Read More

How do I show the schema of a table in a MySQL database?

Arjun Thakur
Updated on 01-Sep-2023 02:31:23

91K+ Views

To show the schema, we can use the DESC command. This gives the description about the table structure. The following is the syntax. DESCRIBE yourDatabasename.yourTableName; Let us implement the above syntax. mysql> DESCRIBE business.student; The following is the output. +-------+--------------+------+-----+---------+-------+ | Field | Type         | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id    | int(11)      | YES | MUL | NULL    | | | Name  | varchar(100) | YES  | MUL | NULL |  | +-------+--------------+------+-----+---------+-------+ 2 rows in set (0.05 ... Read More

Advertisements