Found 4381 Articles for MySQL

How to get the next auto-increment id in MySQL?

Arjun Thakur
Updated on 14-Sep-2023 01:25:33

34K+ Views

MySQL has the AUTO_INCREMENT keyword to perform auto-increment. The starting value for AUTO_INCREMENT is 1, which is the default. It will get increment by 1 for each new record. To get the next auto increment id in MySQL, we can use the function last_insert_id() from MySQL or auto_increment with SELECT. Creating a table, with "d" as auto-increment. mysql> create table NextIdDemo -> ( -> id int auto_increment, -> primary key(id) -> ); Query OK, 0 rows affected (1.31 sec) Inserting records into the table. mysql> insert into NextIdDemo ... Read More

How can I get enum possible values in a MySQL database?

Venu Madhavi
Updated on 27-Jan-2025 19:28:12

672 Views

To improve query efficiency, MySQL has a specific data type called ENUM that stores string values as numeric indexes. By limiting values to particular possibilities, ENUM, in contrast to standard string types, ensures data consistency and streamlines comparisons. The SHOW COLUMNS statement is used to fetch all the possible values of ENUM, under the type column this is a straightforward way to understand, and validate data and also to check the constraints in the database schema. Retrieving the possible values of ENUM In the below example let us see how to create an ENUM column, fetch the data, and ... Read More

How to quit/ exit from MySQL stored procedure?

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

3K+ Views

We can quit/ exit from MySQL stored procedure with the help of the LEAVE command. The following is the syntax. Leave yourLabelName; The following is an example. Here, we are creating a new procedure. mysql> delimiter // mysql> CREATE PROCEDURE ExitQuitDemo2(IN Var1 VARCHAR(20)) -> proc_Exit:BEGIN -> IF Var1 IS NULL THEN -> LEAVE proc_Exit; -> END IF; -> END // Query OK, 0 rows affected (0.16 sec) Above, we have set the following LEAVE command to exit from the procedure. If ... Read More

How can I restore the MySQL root user full privileges?

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

2K+ Views

We can restore the MySQL root user full privileges with the help of UPDATE command. Firstly, you need to stop mysqld and restart it with the --skip-grant-tables option. After that, connect to the mysqld server with only mysql (i.e. no -p option, and username may not be required). Issue the below given command in the mysql client to restore the MySQL root user with full privileges. mysql> UPDATE mysql.user SET Grant_priv = 'Y', Super_priv = 'Y' WHERE User = 'root'; Query OK, 0 rows affected (0.04 sec) Rows matched: 1 Changed: 0 Warnings: 0 Above, ... Read More

How to repair MySQL tables from the command line?

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

322 Views

The repair of MySQL tables is only applicable to MyISAM engine type, not for InnoDB. Therefore, we need to change the Engine type to MyISAM. The following is an example. Creating a table mysql> create table RepairTableDemo -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.51 sec) To convert this table to engine type MyISAM, use ALTER. mysql> ALTER TABLE RepairTableDemo ENGINE = MyISAM; Query OK, 0 rows affected (1.14 sec) Records: 0 Duplicates: 0 Warnings: ... Read More

How to use union and order by clause in MySQL?

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

6K+ Views

Union is a type of operator in MySQL. We can use ORDER BY with this to filter records. Use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set. Let us see an example. Creating first table mysql> create table UnionDemo1 -> ( -> id int -> ); Query OK, 0 rows affected (0.59 sec) Inserting records into first table. mysql> insert into UnionDemo1 values(1), (4), (10); Query OK, ... Read More

How to create boolean column in MySQL with false as default value?

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

7K+ Views

To create a column with ‘false’ as the default value, we can use the concept of “default” at the time of creation of the table. Note − 0 represents false and 1 represents true. Creating a table using “default” false. mysql> create table TrueFalseTable -> ( -> Adult boolean default false -> ); Query OK, 0 rows affected (0.65 sec) Inserting records with no value, since we have set “default” above. mysql> insert into TrueFalseTable values(); Query OK, 1 row affected (0.16 sec) To display records. ... Read More

How to count the number of tables in a MySQL database?

Arjun Thakur
Updated on 14-Sep-2023 15:39:55

39K+ Views

To count the total number of tables, use the concept of count(*) with table_schema. First, to check how many tables are present in our database "business", we need to use the 'show' command. mysql> show tables; The following is the output that displays all the tables in the database "business". +--------------------------+ | Tables_in_business | +--------------------------+ | addcheckconstraintdemo | | addcolumntable | | addconstraintdemo | | addnotnulldemo ... Read More

What does it mean by select 1 from MySQL table?

Chandu yadav
Updated on 22-Oct-2023 02:07:45

32K+ Views

The statement select 1 from any table name means that it returns only 1. For example, If any table has 4 records then it will return 1 four times. Let us see an example. Firstly, we will create a table using the CREATE command. mysql> create table StudentTable -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.51 sec) Inserting records mysql> insert into StudentTable values(1, 'John'), (2, 'Carol'), (3, 'Smith'), (4, 'Bob'); Query OK, 4 rows affected (0.21 ... Read More

How to get a list of MySQL views?

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

3K+ Views

To get a list of MySQL views, we can use the SELECT command with LIKE operator. Let us see the syntax first. mysql> SELECT TABLE_SCHEMA, TABLE_NAME -> FROM information_schema.tables -> WHERE TABLE_TYPE LIKE 'VIEW'; The following is the output that displays the total number of views. +--------------+-----------------------------------------------+ | TABLE_SCHEMA | TABLE_NAME | +--------------+-----------------------------------------------+ | sys ... Read More

Advertisements