Found 4378 Articles for MySQL

How to set delay for MySQL trigger/procedure execution?

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

3K+ Views

To set delay, you can use SLEEP(). Let us implement SLEEP() in the procedure execution delay.First, we will create a stored procedure −mysql> DELIMITER // mysql> CREATE PROCEDURE delayInMessage()    -> BEGIN    -> SELECT SLEEP(20);    -> SELECT "AFTER SLEEPING 20 SECONDS, BYE!!!";    -> END    -> // Query OK, 0 rows affected (0.30 sec) mysql> DELIMITER ;Now you can call the stored procedure with the help of CALL command. Following is the syntax −CALL yourStoredProcedureName();Following is the query to call the above-stored procedure and check the execution delay −mysql> call delayInMessage();This will produce the following output −+-----------+ ... Read More

Can we enforce compound uniqueness in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

37 Views

Yes, we can do that. To understand, let us first create a table −mysql> create table enforceCompoundUniqueness    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(40) NOT NULL,    -> StudentMobileNumber varchar(12) NOT NULL,    -> UNIQUE StudentName_StudentMobileNumber(StudentName, StudentMobileNumber)    -> ); Query OK, 0 rows affected (0.60 sec)Following is the query to insert records in the table using insert command −mysql> insert into enforceCompoundUniqueness(StudentName, StudentMobileNumber) values('Larry', '2322245676'); Query OK, 1 row affected (0.18 sec) mysql> insert into enforceCompoundUniqueness(StudentName, StudentMobileNumber) values('Larry', '2322245676'); ERROR 1062 (23000): Duplicate entry 'Larry-2322245676' for key 'StudentName_StudentMobileNumber' ... Read More

How to display some columns (not all) in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

195 Views

In order to show some columns, use NOT IN and set those columns which you do not want to display. Let us first create a table. Following is the query −mysql> create table student_Information    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(50),    -> StudentAge int,    -> StudentAddress varchar(100),    -> StudentAllSubjectScore int    -> ); Query OK, 0 rows affected (0.69 sec)Following is the query to display a description about the above table −mysql> desc student_Information;This will produce the following output −+------------------------+--------------+------+-----+---------+----------------+ | Field             ... Read More

How to SELECT min and max value from the part of a table in MySQL?

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

334 Views

To select min and max value from the part of a table in MySQL, use the following syntax −select min(yourColumnName) as yourAliasName1, max(yourColumnName) as yourAliasName2 from (select yourColumnName from yourTableName limit yourLimitValue) tbl1;Let us first create a table. Following is the query −mysql> create table MinAndMaxValueDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Value int    -> ); Query OK, 0 rows affected (0.52 sec)Insert records in the table using insert command. Following is the query −mysql> insert into MinAndMaxValueDemo(Value) values(10); Query OK, 1 row affected (0.16 sec) mysql> insert into MinAndMaxValueDemo(Value) ... Read More

Merge two tables with union in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

534 Views

To merge two tables with UNION, you can use create table select statement. Following is the syntax −create table yourNewTableName select * from yourFirstTableName UNION select * from yourSecondTableName;Let us first create a table. Following is the query −mysql> create table FirstTable    -> (    -> Id int,    -> PersonName varchar(20)    -> ); Query OK, 0 rows affected (2.10 sec)Following is the query to insert some records in the table using insert command −mysql> insert into FirstTable values(10, 'Larry'); Query OK, 1 row affected (0.12 sec) mysql> insert into FirstTable values(20, 'David'); Query OK, 1 row ... Read More

Comparing two strings in MySQL?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

155 Views

To compare two strings, which are numbers, let us first create a table. Following is the query −mysql> create table compareTwoStringsDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Value varchar(100)    -> ); Query OK, 0 rows affected (0.52 sec)Following is the query to insert some records in the table using insert command −mysql> insert into compareTwoStringsDemo(Value) values('1235667'); Query OK, 1 row affected (0.66 sec) mysql> insert into compareTwoStringsDemo(Value) values('999999'); Query OK, 1 row affected (0.11 sec) mysql> insert into compareTwoStringsDemo(Value) values('999888'); Query OK, 1 row affected (0.17 sec) mysql> ... Read More

Can we use current_date() for table with column timestamp default in MySQL?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

76 Views

Use the CURRENT_TIMESTAMP instead of current_date() in MySQL for timestamp default current_date. Let us first create a table. Following is the query −mysql> create table defaultCurrent_DateDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAdmissionDate timestamp default CURRENT_TIMESTAMP    -> ); Query OK, 0 rows affected (0.55 sec)Following is the query to insert some records in the table using insert command −mysql> insert into defaultCurrent_DateDemo(StudentName) values('Larry'); Query OK, 1 row affected (0.52 sec) mysql> insert into defaultCurrent_DateDemo(StudentName, StudentAdmissionDate) values('Chris', '2019-01-31'); Query OK, 1 row affected (0.18 sec)Following is the ... Read More

View the auto_increment value for a table in MySQL without using SHOW TABLE?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

105 Views

To view the auto_increment value for a table, you can use INFORMATION_SCHEMA.TABLES.Let us first create a table −mysql> create table viewtheauto_incrementValueForATableDemo    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20)    -> ); Query OK, 0 rows affected (0.84 sec)Following is the query to insert some records in the table using insert command −mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Ramit'); Query OK, 1 row affected (0.23 sec) mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Bob'); Query OK, 1 row affected (0.14 sec) mysql> insert into viewtheauto_incrementValueForATableDemo(StudentName) values('Chris'); Query OK, 1 row affected (0.15 sec) mysql> ... Read More

How to form a composite key to be unique in MySQL?

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

1K+ Views

To form a composite key to be unique, you need to use ADD UNIQUE command. Following is the syntax −alter table yourTableName add unique yourUniqueName( yourColumnName1, yourColumnName2, .......N);Let us first create a table. Following is the query −mysql> create table makeCompositeKeyDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(40),    -> StudentAge int,    -> StudentGrade char(1)    -> ); Query OK, 0 rows affected (2.34 sec)Now check the description of the table using DESC command. Following is the query −mysql> desc makeCompositeKeyDemo;This will produce the following output −+--------------+-------------+------+-----+---------+----------------+ | Field   ... Read More

Get the average row length of a MySQL table

Smita Kapse
Updated on 30-Jul-2019 22:30:25

139 Views

In order to get the average row length of a table in MySQL, you can use INFORMATION_SCHEMA.TABLES. Let us first create a table. Following is the query −mysql> create table Client_information    -> (    -> Id int,    -> Name varchar(10)    -> ); Query OK, 0 rows affected (0.48 sec)Following is the query to insert some records in the table using insert command −mysql> insert into Client_information values(1, 'Larry'); Query OK, 1 row affected (0.14 sec) mysql> insert into Client_information values(2, 'Mike'); Query OK, 1 row affected (0.17 sec) mysql> insert into Client_information values(3, 'Sam'); Query ... Read More

Advertisements