Found 4219 Articles for MySQLi

Validate Date in MySQL using a custom function

AmitDiwan
Updated on 07-Apr-2020 11:39:21

945 Views

Let us create a custom function to validate date in MySQL −mysql> set global log_bin_trust_function_creators=1; Query OK, 0 rows affected (0.03 sec) mysql> delimiter // mysql> create function isValidDate(actualDate varchar(255)) returns int    -> begin    -> declare flag int;    -> if (select length(date(actualDate)) IS NOT NULL ) then    -> set flag = 1;    -> else    -> set flag = 0;    -> end if;    -> return flag;    -> end    -> // Query OK, 0 rows affected (0.11 sec) mysql> delimiter ;Case 1 −When parameter is null value i.e. the date to be ... Read More

Display TRUE FALSE records as 0 1 in MySQL

AmitDiwan
Updated on 07-Apr-2020 11:36:20

322 Views

Set the column as BOOLEAN to display 0 and 1 values. Let us create a table −mysql> create table DemoTable2035    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> isMarried boolean,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.72 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2035(Name, isMarried) values('Chris', true); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2035(Name, isMarried) values('David', false); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable2035(Name, isMarried) values('Bob', true); Query OK, 1 ... Read More

Store a column's value into a MySQL stored procedure’s variable

AmitDiwan
Updated on 07-Apr-2020 11:33:59

857 Views

To declare a variable, use DECLARE in a MySQL stored procedure. Let us first create a table −mysql> create table DemoTable2034    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAge int    -> ); Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2034(StudentName, StudentAge) values('Chris', 23); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable2034(StudentName, StudentAge) values('David', 21); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable2034(StudentName, StudentAge) values('Robert', 25); Query OK, 1 row ... Read More

Implement Dynamic SQL query inside a MySQL stored procedure?

AmitDiwan
Updated on 07-Apr-2020 11:30:58

3K+ Views

For dynamic SQL query in a stored procedure, use the concept of PREPARE STATEMENT. Let us first create a table −mysql> create table DemoTable2033    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (1.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2033(Name) values('Chris'); Query OK, 1 row affected (0.85 sec) mysql> insert into DemoTable2033(Name) values('Bob'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2033(Name) values('David'); Query OK, 1 row affected (0.24 sec) mysql> insert into ... Read More

How do I detect if a table exist in MySQL?

AmitDiwan
Updated on 07-Apr-2020 11:27:54

219 Views

To detect the existence of a table, use the concept of INFORMATION_SCHEMA.TABLES. Following is the syntax −select table_name from information_schema.tables where table_schema=database() and table_name=yourTableName;To understand the above syntax, let us create a table −mysql> create table DemoTable2032    -> (    -> ClientId int,    -> ClientName varchar(20),    -> ClientAge int,    -> ClientCountryName varchar(20)    -> ); Query OK, 0 rows affected (1.07 sec)Here is the query to detect if a table exist in a database −mysql> select table_name from information_schema.tables -> where table_schema=database() -> and table_name='DemoTable2032';This will produce the following output ... Read More

Calculating byte values to megabyte (MB) in MySQL?

AmitDiwan
Updated on 06-Apr-2020 13:44:41

1K+ Views

Here, we are taking BIGINT type, since it takes 8 byte signed integer. Let us first create a table with column as BIGINT type −mysql> create table DemoTable2031    -> (    -> ByteValue bigint    -> ); Query OK, 0 rows affected (1.17 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2031 values(1048576); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2031 values(1073741824); Query OK, 1 row affected (0.13 sec)Display all records from the table using select statement −mysql> select *from DemoTable2031;This will produce the following output −+------------+ | ByteValue | ... Read More

Find maximum value from a VARCHAR column in MySQL

AmitDiwan
Updated on 06-Apr-2020 13:43:16

1K+ Views

To find maximum value, use MAX() along with CAST(), since the values are of VARCHAR type. Let us first create a table −mysql> create table DemoTable2030    -> (    -> Value varchar(20)    -> ); Query OK, 0 rows affected (0.44 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2030 values('8'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2030 values('1'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2030 values('10001'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2030 values('901'); Query OK, 1 row affected ... Read More

Update the content of a specific cell in MySQL

AmitDiwan
Updated on 06-Apr-2020 13:42:37

461 Views

Let us first create a table −mysql> create table DemoTable2029    -> (    -> Id int,    -> FirstName varchar(20),    -> LastName varchar(20)    -> ); Query OK, 0 rows affected (0.98 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2029 values(1, 'Chris', 'Brown') -> ; Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable2029 values(2, 'David', 'Miller'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2029 values(3, 'John', 'Smith'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable2029 values(4, 'John', 'Brown'); Query OK, 1 ... Read More

How to correctly use delimiter in a MySQL stored procedure and insert values?

AmitDiwan
Updated on 06-Apr-2020 13:38:34

354 Views

Let us first create a table −mysql> create table DemoTable2028    -> (    -> StudentFirstName varchar(20),    -> StudentLastName varchar(20)    -> ); Query OK, 0 rows affected (0.87 sec)Here is the query to create a stored procedure and insert values (using delimiter correctly) −mysql> delimiter // mysql> create procedure insert_name(in fname varchar(20), in lname varchar(20))    -> begin    -> insert into DemoTable2028 values(fname, lname);    -> end    -> // Query OK, 0 rows affected (0.17 sec) mysql> delimiter ;Call the stored procedure using CALL command −mysql> call insert_name('Chris', 'Brown'); Query OK, 1 row affected (0.17 sec)Display ... Read More

How to write a valid MySQL query and update with a custom variable?

AmitDiwan
Updated on 06-Apr-2020 13:35:56

338 Views

Let us first create a table −mysql> create table DemoTable2027    -> (    -> UserId int    -> ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command −mysql> insert into DemoTable2027 values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable2027 values(20); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable2027 values(31); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable2027 values(11); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable2027;This will produce ... Read More

Advertisements