MySQL Articles

Page 47 of 355

Can we use PRIMARY KEY( column1, column2) in MySQL to make pairs?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jun-2020 202 Views

Yes, you can use below syntax. Following is the syntax −PRIMARY KEY(yourColumnName1, yourColumnName2);Let us first create a table −mysql> create table DemoTable    -> (    -> StudentFirstName varchar(100),    -> StudentLastName varchar(100),    -> StudentAge int,    -> StudentCountryName varchar(100),    -> PRIMARY KEY(StudentFirstName, StudentLastName)    -> ); Query OK, 0 rows affected (0.74 sec)Let us check the description of the table −mysql> desc DemoTable;OutputThis will produce the following output −+--------------------+--------------+------+-----+---------+-------+ | Field              | Type         | Null | Key | Default | Extra | +--------------------+--------------+------+-----+---------+-------+ | StudentFirstName   | ...

Read More

Implement multiple COUNT() in a single MySQL query

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jun-2020 806 Views

For this, use CASE statement. Let us first create a table −mysql> create table DemoTable    -> (    -> LastName varchar(100)    -> ); Query OK, 0 rows affected (0.60 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Brown'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Smith'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Taylor'); Query OK, 1 ...

Read More

How to exclude a specific row from a table in MySQL?

Sharon Christine
Sharon Christine
Updated on 30-Jun-2020 717 Views

Use i.e. not equal in MySQL to exclude a specific row from a table. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> FirstName varchar(100)    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(1, 'John'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(2, 'Chris'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3, 'Robert'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable ...

Read More

How to add multiple intervals to DATE_ADD() in MySQL?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jun-2020 526 Views

The current date and time is as follows −mysql> select now();OutputThis will produce the following output −+---------------------+ | now()               | +---------------------+ | 2019-06-15 12:24:06 | +---------------------+ 1 row in set (0.00 sec)Let us first create a table −mysql> create table DemoTable    ->(    -> ArrivalDate datetime    -> ); Query OK, 0 rows affected (1.15 sec)Insert some records in the table using insert command. Here, we are adding multiple intervals to the DATE_ADD() method −mysql> insert into DemoTable values(DATE_ADD(DATE_ADD(NOW(), INTERVAL 6 MONTH), INTERVAL 1 YEAR)); Query OK, 1 row affected (0.13 sec) ...

Read More

What are the difference ways to replace nulls values in MySQL using SELECT statement?

George John
George John
Updated on 30-Jun-2020 1K+ Views

There are lots of options available to replace NULL values using select statement. You can use CASE statement or IFNULL() or COALESCE()Case 1 − Using IFNULL()The syntax of IFNULL() is as follows −SELECT IFNULL(yourColumnName, ’yourValue’) AS anyVariableName from yourTableName;Case 2 − Using COALESCE()The syntax of COALESCE() is as follows −SELECT COALESCE(yourColumnName, ’yourValue’) AS anyVariableName from yourTableName;Case 3 − Using CASE statementThe syntax of CASE statement.SELECT CASE WHEN yourColumnName IS NULL THEN ‘yourValue’ ELSE yourColumnName END AS anyVariableName FROM yourTableNameTo understand what we discussed above, let us create a table. The query to create a table is as follows −mysql> create ...

Read More

Update an entire row in MySQL?

Arjun Thakur
Arjun Thakur
Updated on 30-Jun-2020 2K+ Views

To update an entire row in MySQL, use UPDATE command. You need to know the primary key column. The syntax is as follows to update an entire row.UPDATE yourTableName SET yourColumnName1 = ’yourValue1’ ,yourColumnName2 = ’yourValue2’ ,    yourColumnName3 = ’yourValue3’ ,.......................N    WHERE yourPrimaryKeyColumnName = yourValue;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table UpdateEntireRowDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> Name varchar(20),    -> Age int,    -> Marks int,    -> PRIMARY KEY(Id)    -> ); Query OK, ...

Read More

How can I change root username in MySQL?

George John
George John
Updated on 30-Jun-2020 3K+ Views

To change the root username in MySQL, you need to use UPDATE and SET command. The syntax is as follows −UPDATE user set user = ’yourNewUserName’ WHERE user = ’root’;To understand the above syntax, let us switch the database to MySQL using USE command.The query is as follows to switch the database.mysql> use mysql; Database changedNow list all the users from MySQL.user table. The query is as follows −mysql> select user from MySQL.user;The following is the output −+------------------+ | user             | +------------------+ | Manish           | | User2     ...

Read More

MySQL query to find a list of city names that do not start with vowels?

Arjun Thakur
Arjun Thakur
Updated on 30-Jun-2020 14K+ Views

You can use DISTINCT with RLIKE operator to find a list of city names that do not start with vowels.The syntax is as follows −SELECT DISTINCT yourCityColumnName FROM yourTableName WHERE yourCityColumnName NOT RLIKE ‘ ^[AEIOUaeiou].*$’;To understand the above syntax, let us create a table. Here, we have a column for city names.The query to create a table is as follows −mysql> create table Employee_Information    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> EmployeeName varchar(20),    -> CityName varchar(20),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.76 sec)Insert some records in the ...

Read More

MySQL Query to remove all characters after last comma in string?

Ankith Reddy
Ankith Reddy
Updated on 30-Jun-2020 4K+ Views

To remove all characters after the last comma in the string, you can use SUBSTRING_INDEX().If you do not know the location of the last comma, then you need to find the last comma dynamically using LENGTH().The syntax is as follows −UPDATE yourTableName set yourColumnName = SUBSTRING_INDEX(yourColumnName, ', ', LENGTH(yourColumnName) - LENGTH(REPLACE(yourColumnName, ', ', '')));To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table RemoveAllCharacters    -> (    -> Id int NOT NULL AUTO_INCREMENT,    -> FullInfo varchar(200),    -> PRIMARY KEY(Id)    -> ); Query OK, 0 ...

Read More

Replace 0 with null in MySQL?

George John
George John
Updated on 30-Jun-2020 2K+ Views

You can use NULLIF() from MySQL to replace 0 with NULL. The syntax is as follows −SELECT *, NULLIF(yourColumnName, 0) 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 Replace0WithNULLDemo    -> (    -> Id int NOT NULL auto_increment,    -> Name varchar(20),    -> Marks int,    -> PRIMARY KEY(Id)    -> ); Query OK, 0 rows affected (0.53 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into Replace0WithNULLDemo(Name, Marks) values('John', ...

Read More
Showing 461–470 of 3,543 articles
« Prev 1 45 46 47 48 49 355 Next »
Advertisements