Found 6702 Articles for Database

Using the entire expression in MySQL WHERE clause?

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

75 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

380 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

323 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

114 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

122 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

Getting the next primary key without adding a new record is impossible, isn't it in MYSQL

AmitDiwan
Updated on 11-Nov-2019 10:45:43

42 Views

No, it is possible to get the next primary key without adding a new record. Let us first create a −mysql> create table DemoTable1399    -> (    -> StudentId int NOT NULL AUTO_INCREMENT,    -> PRIMARY KEY(StudentId)    -> ); Query OK, 0 rows affected (0.53 sec)Insert some records in the table using insert −mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1399 values(); Query OK, 1 row affected (0.07 sec)Display all records from the table using select −mysql> select ... Read More

Find percentage from marks in MySQL

AmitDiwan
Updated on 11-Nov-2019 10:42:56

371 Views

Let us first create a −mysql> create table DemoTable1398    -> (    -> Marks int    -> ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert −mysql> insert into DemoTable1398 values(78); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1398 values(82); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1398 values(90); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1398 values(98); Query OK, 1 row affected (0.12 sec)Display all records from the table using select −mysql> select * from DemoTable1398;This will produce the following output −+-------+ | ... Read More

Advertisements