Database Articles

Page 293 of 546

Update MySQL column based on email address?

AmitDiwan
AmitDiwan
Updated on 18-Dec-2019 2K+ Views

Let us first create a table −mysql> create table DemoTable    -> (    -> EmailAddress varchar(20),    -> Score int    -> ); Query OK, 0 rows affected (1.05 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris@gmail.com', 67); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('Robert@gmail.com', 57); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('David@gmail.com', 98); Query OK, 1 row affected (0.14 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+------------------+-------+ | EmailAddress     ...

Read More

How to update MySQL table storage engine

AmitDiwan
AmitDiwan
Updated on 18-Dec-2019 310 Views

To update MySQL table engine, following the below syntax −Syntaxalter table yourTableName ENGINE=InnoDB;Let us first create a table −mysql> create table DemoTable    -> (    -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> StudentName varchar(20),    -> StudentAge int,    -> StudentCountryName varchar(20)    -> )ENGINE=MyISAM, AUTO_INCREMENT=101; Query OK, 0 rows affected (0.18 sec)Let us check the description of table −mysql> show create table DemoTable;This will produce the following output −+---------------+-----------------------------------------------------------------------------------------+ | Table         | Create Table                                   ...

Read More

Fetch maximum individual marks for a student with marks1 and marks2 records in MySQL?

AmitDiwan
AmitDiwan
Updated on 18-Dec-2019 312 Views

For this, use MAX() along with GROUP BY clause. Let us first create a table −mysql> create table DemoTable    -> (    -> StudentEmailId varchar(20),    -> Marks1 int,    -> Marks2 int -> ); Query OK, 0 rows affected (0.90 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John@gmail.com', 45, 32); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('John@gmail.com', 32, 45); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('Carol@gmail.com', 32, 45); Query OK, 1 row affected (1.64 sec) mysql> insert into DemoTable values('David@gmail.com', 45, 32); ...

Read More

Select the maximum for each value in a MySQL table?

AmitDiwan
AmitDiwan
Updated on 18-Dec-2019 241 Views

For this, use GROUP BY clause along with MAX(). Let us first create a table −mysql> create table DemoTable    -> (    -> CountryName varchar(20),    -> Population int    -> ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('US', 560); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('UK', 10090); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('UK', 8794); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('US', 1090); Query OK, 1 row affected (0.21 sec)Display ...

Read More

Does grant on *.* apply to databases created after the grant in MySQL?

AmitDiwan
AmitDiwan
Updated on 18-Dec-2019 153 Views

Yes, since this is a global privilege. Let us first create a user −mysql> CREATE USER 'Jace'@'localhost' IDENTIFIED BY 'Jace123'; Query OK, 0 rows affected (0.67 sec)Here is the query to grant for global privileges with *.*:mysql> GRANT SELECT ON *.* TO 'Jace'@'localhost'; Query OK, 0 rows affected (0.58 sec)Now you can show all grants for a user −mysql> show grants for 'Jace'@'localhost';This will produce the following output −+-------------------------------------------+ | Grants for Jace@localhost                 | +-------------------------------------------+ | GRANT SELECT ON *.* TO `Jace`@`localhost` | +-------------------------------------------+ 1 row in set (0.14 sec)

Read More

MySQL query to sort by both timestamp and enum?

AmitDiwan
AmitDiwan
Updated on 17-Dec-2019 150 Views

For this, you can use ORDER BY DATE(). Let us first create a table. Here, we have a column with type DATE and another with type ENUM −mysql> create table DemoTable    -> (    -> JoiningDate date,    -> Status ENUM('Good', 'Excellent', 'Bad')    -> ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2019-01-21', 'Excellent'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Bad'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Status) values('Good'); Query OK, 1 row affected (0.13 sec)Display all ...

Read More

How to use a select statement while updating in MySQL?

AmitDiwan
AmitDiwan
Updated on 17-Dec-2019 409 Views

For this, use sub query along with WHERE clause while using the MySQL UPDATE command. Let us first create a table −mysql> create table DemoTable    -> (    -> Id int,    -> Name varchar(20)    -> ); Query OK, 0 rows affected (0.82 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(250, 'David'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable values(150, 'Mike'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select ...

Read More

Query results that have less than X characters in MySQL?

AmitDiwan
AmitDiwan
Updated on 17-Dec-2019 237 Views

You can use CHAR_LENGTH() along with the WHERE clause. Let us first create a table −mysql> create table DemoTable    -> (    -> FullName varchar(50)    -> ); Query OK, 0 rows affected (1.75 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('Chris Brown'); Query OK, 1 row affected (0.40 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.91 sec) mysql> insert into DemoTable values('Robert Miller'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable values('John Smith'); Query OK, 1 row affected (0.89 sec)Display all records from the ...

Read More

How to split a column in MySQL?

AmitDiwan
AmitDiwan
Updated on 17-Dec-2019 2K+ Views

To split a column, you need to use SUBSTRING_INDEX() in MySQL. Let us first create a table −mysql> create table DemoTable    -> (    -> Name varchar(40)    -> ); Query OK, 0 rows affected (1.80 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('John_Smith'); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('Carol_Taylor'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('David_Miller'); Query OK, 1 row affected (0.54 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+--------------+ ...

Read More

Add a single year to all the date records in a MySQL table column

AmitDiwan
AmitDiwan
Updated on 17-Dec-2019 258 Views

Let us first create a table −mysql> create table DemoTable    -> (    -> JoiningDate datetime    -> ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('2015-01-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2017-04-02'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.16 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following output −+---------------------+ | JoiningDate         | +---------------------+ | 2015-01-21 00:00:00 ...

Read More
Showing 2921–2930 of 5,456 articles
« Prev 1 291 292 293 294 295 546 Next »
Advertisements