Found 4378 Articles for MySQL

How to add duplicate varchar values without displaying error in MySQL?

AmitDiwan
Updated on 11-Nov-2019 11:08:27

70 Views

For this, let us see an example and first create a −mysql> create table DemoTable1409    -> (    -> FirstName varchar(20),    -> UNIQUE KEY UN_FirstName(FirstName)    -> ); Query OK, 0 rows affected (0.79 sec)Following is the query to add duplicate varchar −mysql> alter table DemoTable1409 drop index  UN_FirstName; Query OK, 0 rows affected (0.40 sec) Records: 0  Duplicates: 0  Warnings: 0Insert some records in the table using insert −mysql> insert into DemoTable1409 values('Chris'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1409 values('Chris'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1409 values('David'); ... Read More

How to include quotes in comma separated column with MySQL?

AmitDiwan
Updated on 11-Nov-2019 11:04:22

731 Views

Let us first create a −mysql> create table DemoTable1407    -> (    -> Name text    -> ); Query OK, 0 rows affected (0.51 sec)Insert some records in the table using insert −mysql> insert into DemoTable1407 values('John, Bob'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1407 values('Carol, David, Adam'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1407 values('Mike, Sam, Chris'); Query OK, 1 row affected (0.19 sec)Display all records from the table using select −mysql> select * from DemoTable1407;This will produce the following output −+------------------+ | Name             ... Read More

Using the entire expression in MySQL WHERE clause?

AmitDiwan
Updated on 11-Nov-2019 11:02:48

74 Views

Let us see an example and create a −mysql> create table DemoTable1406    -> (    -> Value int    -> ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert −mysql> insert into DemoTable1406 values(10); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1406 values(50); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable1406 values(40); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable1406 values(30); Query OK, 1 row affected (0.11 sec)Display all records from the table using select −mysql> select * from DemoTable1406;This will produce the following ... Read More

Update all rows in MySQL and remove all the unnecessary whitespaces in and around the string?

AmitDiwan
Updated on 11-Nov-2019 11:01:29

64 Views

To remove unnecessary whitespaces, use TRIM() in MySQL. Let us first create a −mysql> create table DemoTable1405    -> (    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert −mysql> insert into DemoTable1405 values('   Chris', ' Brown '); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1405 values('David      ', ' Miller '); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1405 values('     Carol ', ' Taylor '); Query OK, 1 row affected (0.19 sec)Display all ... Read More

Display month names and year from a column with date records with MySQL

AmitDiwan
Updated on 11-Nov-2019 10:57:08

379 Views

Let us first create a −mysql> create table DemoTable1619    -> (    -> ArrivalTime datetime    -> ); Query OK, 0 rows affected (0.45 sec)Insert some records in the table using insert −mysql> insert into DemoTable1619 values(now()); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable1619 values(curdate()); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1619 values('2019-12-31'); Query OK, 1 row affected (0.17 sec)Display all records from the table using select −mysql> select * from DemoTable1619;This will produce the following output −+---------------------+ | ArrivalTime         | +---------------------+ | 2019-10-20 15:02:12 | | ... Read More

MySQL query to copy IP address from varchar column to integer in the same table?

AmitDiwan
Updated on 11-Nov-2019 10:55:05

193 Views

For this, you can use INET_ATON(). Let us first create a −mysql> create table DemoTable1404    -> (    -> IpAddress varchar(40)    -> ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert −mysql> insert into DemoTable1404 values('192.168.120.0'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable1404 values('192.168.120.20'); Query OK, 1 row affected (0.60 sec) mysql> insert into DemoTable1404 values('224.0.0.0'); Query OK, 1 row affected (0.42 sec)Display all records from the table using select −mysql> select * from DemoTable1404;This will produce the following output −+----------------+ | IpAddress      | +----------------+ | ... Read More

Update table and order dates in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:52:35

322 Views

You cannot use UPDATE command with ORDER BY clause, but you can use SELECT statement with ORDER BY DESC.Let us first create a −mysql> create table DemoTable1403    -> (    -> DueDate timestamp    -> ); Query OK, 0 rows affected (1.26 sec)Insert some records in the table using insert −mysql> insert into DemoTable1403 values('2019-09-29'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1403 values('2016-02-21'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable1403 values('2018-01-31'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable1403 values('2017-12-01'); Query OK, 1 row affected (0.27 sec)Display all ... Read More

Display substring in MySQL if the string is less than a specific length or display a custom message if it is more?

AmitDiwan
Updated on 11-Nov-2019 10:50:57

113 Views

For this, you can use substring() function in MySQL. For conditions, use MySQL CASE statement. Let us first create a −mysql> create table DemoTable1402    -> (    -> EmployeeName varchar(40)    -> ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert −mysql> insert into DemoTable1402 values('Adam Smith'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1402 values('Chris Brown'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1402 values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1402 values('Carol Taylor'); Query OK, 1 row affected (0.10 sec)Display ... Read More

Why do backticks won’t work in the SET part of an UPDATE query (for MySQL)?

AmitDiwan
Updated on 11-Nov-2019 10:48:35

121 Views

Backticks would work if written correctly as in the below syntax −update `yourTableName` set `yourTableName`.`yourColumnName`='yourNewValue' where yourCondition;Let us first create a −mysql> create table `DemoTable_1401`    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.52 sec)Insert some records in the table using insert −mysql> insert into `DemoTable_1401` values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into `DemoTable_1401` values('David'); Query OK, 1 row affected (0.10 sec) mysql> insert into `DemoTable_1401` values('Bob'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select −mysql> select * from `DemoTable_1401`;This will produce the ... Read More

How to implement CANDIDATE key in any MySQL table?

AmitDiwan
Updated on 11-Nov-2019 10:47:16

3K+ Views

Each relation may have one or more candidate key. One of these candidate keys is called Primary Key. Each candidate key qualifies for Primary Key. Therefore, candidates for Primary Key is called Candidate Key. To implement candidate key in MySQL, set more than one column as unique key. These keys would qualify for candidate key as in the below syntax −alter table yourTableName add unique key anyName(yourColumnName1, yourColumnName2);Let us first create a −mysql> create table DemoTable1400    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(40),    -> Age int    -> ); Query ... Read More

Advertisements