Found 4378 Articles for MySQL

Drop a MySQL Table after x hours?

Samual Sam
Updated on 30-Jul-2019 22:30:25

108 Views

You need to create event to drop table after x hours. The syntax is as follows −CREATE EVENT yourEventName ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL x HOUR DO DROP TABLE IF EXISTS yourTableName;Let us first create a table −mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudnetFirstName varchar(20),    StudentLastName varchar(20),    StudnetAge int ); Query OK, 0 rows affected (0.52 sec)Now implement the above event in order to drop table after 2 hours −mysql> CREATE EVENT drop_table_event_after2HoursDemo ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 2 HOUR DO DROP TABLE IF EXISTS DemoTable; Query OK, ... Read More

Is it possible to divide records in both ascending and descending order in MySQL and display them alternatively?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

66 Views

Yes, you can perform this in MySQL by first getting the middle value. Let us first create a table:mysql> create table DemoTable (    UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY ); Query OK, 0 rows affected (0.65 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable values(); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(); Query OK, 1 row affected (0.16 sec) mysql> ... Read More

Order randomly in MySQL with a random value column?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

194 Views

Let us first create a table. After that we will create a new random value column and order the record randomly:mysql> create table DemoTable (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20) ); Query OK, 0 rows affected (0.57 sec)Following is the query to insert some records in the table using insert command:mysql> insert into DemoTable(StudentName) values('Larry'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(StudentName) values('Sam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(StudentName) values('Mike'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable(StudentName) values('Carol'); Query OK, 1 ... Read More

How to check if a table exists in MySQL and create if it does not already exist?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

If you try to create a table and the table name already exist then MySQL will give a warning message. Let us verify the concept.Here, we are creating a table that already exist −mysql> CREATE TABLE IF NOT EXISTS DemoTable    (    CustomerId int,    CustomerName varchar(30),    CustomerAge int    ); Query OK, 0 rows affected, 1 warning (0.05 sec)The table name DemoTable is already present. Let us check the warning message.Following is the query −mysql> show warnings;This will produce the following output i.e. the warning message −+-------+------+------------------------------------+ | Level | Code | Message ... Read More

How to get field name types from a MySQL database?

Samual Sam
Updated on 30-Jul-2019 22:30:25

253 Views

You can use INFORMATION_SCHEMA.COLUMNS for this. Following is the syntax −SELECT COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='yourTableName';Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(60),    ClientAge int,    ClientSalary DECIMAL(10, 4),    isRegularClient bool    ); Query OK, 0 rows affected (0.44 sec)Following is the query to get field name types from a SQL database −mysql> SELECT COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='DemoTable';This will produce the following output −+-----------------+---------------+ | COLUMN_NAME | COLUMN_TYPE | +-----------------+---------------+ | Id ... Read More

Select current time with MySQL now() and convert it to GMT 0?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

389 Views

You can use utc_timestamp() for this. Following is the syntax −SELECT utc_timestamp();Let us check the current time using now().Following is the query −mysql> select now();This will produce the following output −+---------------------+ | now() | +---------------------+ | 2019-04-11 17:50:05 | +---------------------+ 1 row in set (0.00 sec)Following is the query to get GMT 0 −mysql> SELECT utc_timestamp();This will produce the following output −+---------------------+ | utc_timestamp()   | +---------------------+ | 2019-04-11 12:20:08 | +---------------------+ 1 row in set (0.00 sec)

How to convert all the records in a MySQL table from uppercase to lowercase?

Samual Sam
Updated on 30-Jul-2019 22:30:25

664 Views

Convert all the records in a MySQL table from uppercase to lowercase using UPDATE command along with LOWER() method.Let us first create a table −mysql> create table DemoTable    (    Id varchar(100),    StudentFirstName varchar(20),    StudentLastName varchar(20),    StudentCountryName varchar(10)    ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('STU-101', 'John', 'Smith', 'US'); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values('STU-102', 'John', 'Doe', 'UK'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('STU-103', 'David', 'Miller', 'AUS'); Query OK, 1 ... Read More

Display IDs in a particular order with MySQL IN()?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

154 Views

To display IDs in a particular order i.e. the order of your choice use FIELD() method.Let us first create a table −mysql> create table DemoTable    (    UserId int    ); Query OK, 0 rows affected (0.64 sec)Following is the query to insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(40); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.13 sec) ... Read More

Add a temporary column in MySQL where the values depend on another column?

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

You can use CASE statement for this and set conditions to get result in the temporary column.Let us first create a table −mysql> create table DemoTable    (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeName varchar(20),    EmployeeSalary int,    EmployeeExperience int    ); Query OK, 0 rows affected (0.64 sec)Following is the query to insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, EmployeeSalary, EmployeeExperience) values('Larry', 4500, 5); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(EmployeeName, EmployeeSalary, EmployeeExperience) values('Mike', 130000, 8); Query OK, 1 row affected (0.21 sec) mysql> ... Read More

How do I find out the default server character set in MySQL?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

284 Views

You can use system variables character_set_server to know the default server character set in MySQL. Following is the syntax −SHOW VARIABLES LIKE 'character_set_server';Additionally, to u can use collation_server system variable to know the default collation in MySQL. Following is the syntax −SHOW VARIABLES LIKE 'collation_server';Let us execute the above syntaxes to know the default character set and collation.Following is the query −mysql> SHOW VARIABLES LIKE 'character_set_server';This will produce the following output −+----------------------+-------+ | Variable_name | Value | +----------------------+-------+ | character_set_server | utf8 | +----------------------+-------+ 1 row in set (0.25 sec)Following is the ... Read More

Advertisements