Found 6702 Articles for Database

How to deal with 'Boolean' values in PHP & MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

3K+ Views

We are using MySQL version 8.0.12. Let us first check the MySQL version:mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12    | +-----------+ 1 row in set (0.00 sec)To deal with Boolean in MySQL, you can use BOOL or BOOLEAN or TINYINT(1). If you use BOOL or BOOLEAN, then MySQL internally converts it into TINYINT(1).In BOOL or BOOLEAN data type, if you use true literal then MySQL represents it as 1 and false literal as 0 like in PHP/ C/ C++ language.To proof that MySQL convert the BOOL or BOOLEAN to TINYINT(1), let us create a table with ... Read More

System variables vs Local Variables in MySQL?

Chandu yadav
Updated on 30-Jun-2020 12:16:18

349 Views

The local variable has the scope for only a set of statements or block of statement. Whenever a set of statements or block of statement has completed then local variable goes out of scope.For ExampleLocal variable can be used in stored procedure, function etc. It is used with the DECLARE keyword.The syntax is as follows to local variables.DECLARE yourVariableName dataType;The global variables or system variables has the scope across connections until server restart. It is set using GLOBAL keyword. The syntax is as follows −SET GLOBAL max_connections=value; OR SET @@global.max_connections=value;If you know port number then you can use system variable ... Read More

System variables vs User-defined variables in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

352 Views

System VariablesThe system variables are tightly typed variable. These are also known as global specific variable.The system variable can be initialized somewhere in global and contains the value of system variables until the server restarts. This value will destroy whenever you restart the MySQL server. The predefined system variable is prefixed with symbol @@.User-defined VariableThe user defined variable is also known as session-specific variable. It is a type of loosely typed variable which can be initialized somewhere in session and contains the value of user defined variable until session ends. The user defined variable is prefixed with symbol @.For Example:@anyVariableNameRead More

User-defined variables vs Local Variables in MySQL?

George John
Updated on 30-Jul-2019 22:30:24

1K+ Views

The user defined variable is also known as session-specific variable. It is a type of loosely typed variable which can be initialized somewhere in session and contains the value of user defined variable until session ends.The user defined variable is prefixed with symbol @. For Example:@anyVariableName;There are two approaches by which you can initialize the user-defined variable. You can use SET command or using SELECT query. The first approach is as follows:SET @anyVariableName=anyValue;The second approach is as follows:SELECT @anyVariableName :=anyValue;If you do not use colon (:) in SELECT query then it evaluates it as expression. The result will either be ... Read More

Remove seconds from time field in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

2K+ Views

You need to use TIME_FORMAT() to remove seconds from time field. The syntax is as follows:SELECT TIME_FORMAT(yourColumnName1, "%H:%i") AS anyVariableName, TIME_FORMAT(yourColumnName2, "%H:%i") AS anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table removeSecondsFromTime    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> StartTime time,    -> EndTime time,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.71 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into removeSecondsFromTime(StartTime, EndTime) values('10:20:45', '11:21:40'); Query OK, ... Read More

Why MySQL NOT NULL shouldn’t be added to primary key field?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

663 Views

You do not need to add NOT NULL to primary key field because it gets NOT NULL automatically. Primary key is combination of both NOT NULL and Unique Key.Here is the demo of primary key field. Let us first create a table. The query to create a table is as follows:mysql> create table NotNullAddDemo    -> (    -> Id int AUTO_INCREMENT,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.81 sec)In the above table, you do not need to add NOT NULL to primary key field because MySQL internally converts it into NOT NULL. To ... Read More

MySQL - Changing year of dates from 2020 to 2011?

Ankith Reddy
Updated on 30-Jul-2019 22:30:24

314 Views

You can change year of dates from 2020 to 2011 using SUBDATE() with INTERVAL of 9 year because there is a difference of 9 years between 2020 to 2011.The syntax is as follows:UPDATE yourTableName SET yourDateColumnName=SUBDATE(yourDateColumnName, INTERVAL 9 YEAR);To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table ChangeYearFrom2020To2011    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> ExpiryDate date,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command. The query to insert ... Read More

How to convert Varchar to Double in SQL?

George John
Updated on 30-Jul-2019 22:30:24

4K+ Views

You can convert varchar to double using CAST() function. The syntax is as follows:SELECT yourColumnName1, yourColumnName2, ......N, CAST(yourColumnName AS DECIMAL(TotalDigit, DigitAfterDecimalPoint)) anyVariableName FROM yourtableName ORDER BY anyVariableName DESC;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table VarcharToDouble    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(10),    -> Amount varchar(10) ,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into VarcharToDouble(Name, Amount) values('John', ... Read More

Datetime to Integer in MySQL?

Arjun Thakur
Updated on 30-Jul-2019 22:30:24

2K+ Views

In MySQL, convert datetime to integer using UNIX_TIMESTAMP() function. The syntax is as follows:SELECT UNIX_TIMESTAMP(yourDatetimeColumnName) as anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table DatetimeToInteger    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> ArrivalTime datetime,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.94 sec)Insert some records in the table using insert command. The query is as follows:mysql> insert into DatetimeToInteger(ArrivalTime) values(now()); Query OK, 1 row affected (0.09 sec) mysql> insert into DatetimeToInteger(ArrivalTime) values(curdate()); Query OK, 1 ... Read More

How to concatenate more than 2 fields with SQL?

Chandu yadav
Updated on 30-Jul-2019 22:30:24

13K+ Views

To concatenate more than 2 fields with SQL, you can use CONCAT() or CONCAT_WS() function. The syntax is as follows. Let us first see using CONCAT().SELECT CONCAT(yourColumnName1, '/', yourColumnName2, '/', yourColumnName3, '/', ......N) AS anyVariableName FROM yourTableName;The syntax is as follows:SELECT CONCAT_WS(‘/’, yourColumnName1, yourColumnName2, .....N) AS anyVariableName FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows:mysql> create table MoreThan2ColumnConcat    -> (    -> Id int,    -> Name varchar(20),    -> Age int,    -> Marks int    -> ); Query OK, 0 rows affected (2.59 sec)Insert some ... Read More

Advertisements