Found 4378 Articles for MySQL

What will happen if we have set UNIQUE and multiple insertion with duplicate values

AmitDiwan
Updated on 16-Dec-2019 06:35:52

85 Views

An error will arise and nothing will get inserted in the table Let us see an example and create a table −mysql> create table DemoTable1585    -> (    -> StudentId int,    -> StudentMarks int,    -> UNIQUE(StudentId)    -> ); Query OK, 0 rows affected (1.02 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1585 values(1,87),(2,98),(3,91),(3,48); ERROR 1062 (23000): Duplicate entry '3' for key 'StudentId'Display all records from the table using select statement −mysql> select * from DemoTable1585;This will produce the following output. Nothing gets inserted:Empty set (0.00 sec)

How to insert DATE in MySQL table with TRIGGERS?

AmitDiwan
Updated on 16-Dec-2019 06:27:58

866 Views

Let us first create a table −mysql> create table DemoTable1584    -> (    -> DueDate datetime    -> ); Query OK, 0 rows affected (1.79 sec)Here is the query to insert DATE in a MySQL −mysql> create trigger insertDate before insert on DemoTable1584    ->  for each row set new.DueDate=curdate(); Query OK, 0 rows affected (0.22 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1584 values(); Query OK, 1 row affected (0.66 sec) mysql> insert into DemoTable1584 values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1584 values(); Query OK, 1 row affected ... Read More

Deleting partial data from a field in MySQL?

AmitDiwan
Updated on 16-Dec-2019 06:26:55

193 Views

To delete partial data, use UPDATE command along with REPLACE(). Let us first create a table −mysql> create table DemoTable1583    -> (    -> GameDetails text    -> ); Query OK, 0 rows affected (1.38 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1583 values('=Candy, 2000'); Query OK, 1 row affected (0.53 sec) mysql> insert into DemoTable1583 values('=Lucky29, 10000'); Query OK, 1 row affected (0.25 sec)Display all records from the table using select statement −mysql> select * from DemoTable1583;This will produce the following output −+------------------------------------------------------------+ | GameDetails                 ... Read More

Query MySQL table and fetch rows posted before the last 3 days?

AmitDiwan
Updated on 16-Dec-2019 06:25:24

110 Views

Let’s say the current date is −'2019-10-20We will first see an example and create a table −mysql> create table DemoTable1582    -> (    -> PostedDate datetime    -> ); Query OK, 0 rows affected (13.36 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1582 values('2019-01-21 12:34:40'); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable1582 values('2019-10-15 11:00:00'); Query OK, 1 row affected (0.87 sec) mysql> insert into DemoTable1582 values('2019-10-25 1:10:00'); Query OK, 1 row affected (1.14 sec)Display all records from the table using select statement −mysql> select * from DemoTable1582;This will produce the ... Read More

How to ensure that MySQL rows are unique?

AmitDiwan
Updated on 16-Dec-2019 06:19:54

348 Views

To ensure that MySQL rows are unique, you need to use UNIQUE constraint. Let us first create a table −mysql> create table DemoTable1580    -> (    -> id int,    -> Name varchar(20),    -> Age int    -> ); Query OK, 0 rows affected (0.73 sec)Here is the query to create unique constraints to ensure MySQL rows are unique −mysql> alter table DemoTable1580 add unique index(id, Name, Age); Query OK, 0 rows affected (0.45 sec) Records: 0  Duplicates: 0  Warnings: 0Insert some records in the table using insert command −mysql> insert into DemoTable1580 values(101, 'Chris', 21); Query OK, ... Read More

How to select the last three rows of a table in ascending order with MySQL?

AmitDiwan
Updated on 16-Dec-2019 06:15:52

560 Views

To select the last three rows in ascending order, use ORDER BY DESC LIMIT as in the below syntax −select * from (select * from yourTableName order by yourColumnName desc limit 3) anyAliasName order by yourColumnName ;Let us first create a table −mysql> create table DemoTable1579    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1579(Name) values('Robert'); Query OK, 1 row affected (0.37 sec) mysql> insert into DemoTable1579(Name) values('Bob'); Query OK, 1 ... Read More

MySQL select and insert in two tables with a single query

AmitDiwan
Updated on 16-Dec-2019 06:11:45

294 Views

Here is the query to create first table.mysql> create table DemoTable1    -> (    -> StudentName varchar(20),    -> StudentMarks int    -> ); Query OK, 0 rows affected (0.67 sec)To understand the above concept, let us create second table.mysql> create table DemoTable2    -> (    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2 values('Chris'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable2;This will produce the following output −+-------+ ... Read More

Easiest way to get number of rows in a MySQL table?

AmitDiwan
Updated on 16-Dec-2019 06:10:14

139 Views

The easiest way to get number of rows, use aggregate function COUNT(*). Let us first create a table −mysql> create table DemoTable1575    -> (    -> FirstName varchar(20)    -> ); Query OK, 0 rows affected (1.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1575 values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1575 values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1575 values('Adam'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1575 values('Robert'); Query OK, 1 row affected (0.15 sec)Display all records from the table ... Read More

MySQL query to replace special characters from column value

AmitDiwan
Updated on 16-Dec-2019 06:08:49

788 Views

Let us first create a table −mysql> create table DemoTable1574    -> (    -> StudentCode varchar(20)    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1574 values('111_Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1574 values('______'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable1574 values('David_12345'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1574 values('______'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select * from DemoTable1574;This will produce the following ... Read More

How to retrieve table names from a database in MySQL?

AmitDiwan
Updated on 16-Dec-2019 06:07:34

260 Views

To retrieve table names from a database in MySQL, the syntax is as follows −show tables from yourDatabaseName;Let us implement the above query in order to retrieve table names from a database in MySQL −mysql> show tables from hb_student_tracker;This will produce the following output −+------------------------------+ | Tables_in_hb_student_tracker | +------------------------------+ | demotable192                 | | demotable193                 | | demotable194                 | | demotable195                 | | demotable196           ... Read More

Advertisements