Found 4378 Articles for MySQL

Which datatype should I use for flag in MySQL?

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

1K+ Views

To set a flag, you can set the type as tinyint(1) type. Following is the syntax −yourColumnName tinyint(1) DEFAULT 1;Let us first create a table −mysql> create table DemoTable (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    isMarried tinyint(1) DEFAULT 1 ); Query OK, 0 rows affected (0.64 sec)Insert some records in the table using insert command −mysql> INSERT INTO DemoTable(ClientName, isMarried) values('Larry', 0); Query OK, 1 row affected (0.16 sec) mysql> INSERT INTO DemoTable(ClientName) values('David'); Query OK, 1 row affected (0.12 sec) mysql> INSERT INTO DemoTable(ClientName, isMarried) values('Mike', 1); Query OK, 1 row affected (0.19 ... Read More

How to order by certain part of a string in MySQL?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use ORDER BY SUBSTRING() to order by certain part of a string in MySQL. Let us first create a table:mysql> create table DemoTable (UserId varchar(200)); Query OK, 0 rows affected (0.68 sec)Following is the query to insert records in the table using insert command:mysql> insert into DemoTable values('USER_1234'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('USER_John'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('USER_Sam'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('USER_Carol'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('USER_Bob'); Query OK, ... Read More

What is the alias to Show Tables in MySQL Result?

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

319 Views

You can use AS command for alias to show tables in MySQL result. Following is the syntax −SELECT TABLE_NAME AS anyAliasName FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE();Let us implement the above syntax −mysql> SELECT TABLE_NAME AS MY_TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = DATABASE();This will produce the following output −+------------------------------------+ | MY_TABLE_NAME                      | +------------------------------------+ | a                                  | | accumulateddemo                    | | add10minutedemo           ... Read More

How to GROUP BY in a select query on positive or negative values?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

432 Views

Following is the syntax to GROUP BY in a select query on positive or negative values:select *from yourTableName group by -yourColumnName;Let us first create a table:mysql> create table DemoTable (Value int); Query OK, 0 rows affected (0.60 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(-10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(-20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.13 sec) mysql> insert ... Read More

How to update a column of varchar type in MySQL to increase its length?

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

514 Views

Let us first create a table. Here, we have two columns with varchar type −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserFirstName varchar(10),    UserLastName varchar(20) ,    UserAge int ); Query OK, 0 rows affected (0.96 sec)Let us check the description of table using DESC command −mysql> desc DemoTable;This will produce the following output −+---------------+-------------+------+-----+---------+----------------+ | Field         | Type        | Null | Key | Default | Extra          | +---------------+-------------+------+-----+---------+----------------+ | UserId        | int(11)     | NO   ... Read More

What is the syntax for input parameters (variables) in a MySQL query?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

2K+ Views

To set a variable in MySQL, you need to use the SET command. Following is the syntax:set @yourVariableName:=yourValue; select *from yourTableName where yourColumnName=@yourVariableName;Let us first create a table:mysql> create table DemoTable (    Id int,    FirstName varchar(20),    LastName varchar(20) ); Query OK, 0 rows affected (0.83 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(10, 'Carol', 'Taylor'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(20, 'John', 'Doe'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(30, 'John', 'Smith'); Query OK, 1 row ... Read More

MySQL query to add 0's to numbers with less than 9 digits?

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

276 Views

Use LPAD() to add 0's to numbers with less than 9 digits. Let us first create a table −mysql> create table DemoTable (    Value varchar(20) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('3646465'); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values('9485757591'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('485756'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('959585'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('124'); Query OK, 1 row affected ... Read More

Alter row_format to dynamic in MySQL?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

885 Views

To alter row_format to dynamic in MySQL, following is the syntax:ALTER TABLE yourTableName ROW_FORMAT=DYNAMIC;Let us first create a table:mysql> create table DemoTable (    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerName varchar(200),    CustomerAge int,    CustomerAddress varchar(200) ); Query OK, 0 rows affected (0.73 sec)Let us check the description of table using DESC command:mysql> desc DemoTable;This will produce the following output:+-----------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra ... Read More

Write a MySQL query where length of the records is longer than 1?

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

209 Views

Here, we will use OCTET_LENGTH to check the length of a record since we want the records with length more than 1. Let us first create a table −mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    UserName varchar(20),    UserGender varchar(20) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(UserName, UserGender) values('John', 'M'); Query OK, 1 row affected (0.82 sec) mysql> insert into DemoTable(UserName, UserGender) values('Carol', 'Male'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(UserName, UserGender) values('Mia', 'Female'); Query OK, ... Read More

MySQL query to delete a record with the lowest ID?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

677 Views

To delete record with the lowest id, you can use the following syntax:delete from yourTableName order by yourColumnName limit 1;Let us first create a table:mysql> create table DemoTable (    Id int,    Name varchar(20) ); Query OK, 0 rows affected (0.75 sec)Following is the query to insert records in the table using insert command:mysql> insert into DemoTable values(10, 'Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(100, 'Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(30, 'Sam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(90, 'Chris'); Query ... Read More

Advertisements