Found 6702 Articles for Database

MySQL ORDER BY CASE to display special character in the beginning

AmitDiwan
Updated on 04-Oct-2019 07:10:00

483 Views

Let us first create a table −mysql> create table DemoTable (    StudentId varchar(40) ); Query OK, 0 rows affected (0.55 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values('10'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('~'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('40'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected ... Read More

How to create NVARCHAR column in MySQL?

AmitDiwan
Updated on 04-Oct-2019 07:07:20

3K+ Views

The MySQL converts NVARCHAR() to VARCHAR(). NVARCHAR stands for National Varchar in MySQL. Let us first create a table with one of the columns “StudentName” as NVARCHAR −mysql> create table DemoTable (    StudentName NVARCHAR(40),    StudentCountryName VARCHAR(50) ); Query OK, 0 rows affected, 1 warning (0.49 sec)Let us check the description of the table −mysql> desc DemoTable;This will produce the following output. As you can see below, the StudentName column with NVARCHAR type is automatically converted to VARCHAR in MySQL −+--------------------+-------------+------+-----+---------+-------+ | Field | Type ... Read More

MySQL query to update all the values in a column with numeric incremental values like John1, John2, John3, etc.

AmitDiwan
Updated on 04-Oct-2019 07:03:57

183 Views

To update all the values in a column to John1, John2, etc.; you need to set incremental values 1, 2, 3, etc. and concatenate them to the records. Let us first create a table −mysql> create table DemoTable (    StudentId varchar(80) ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command. Here, for our example, we have set similar names −mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values('John'); Query OK, 1 row ... Read More

How to delete records based on a word with underscore like MONTH_JAN'?

AmitDiwan
Updated on 04-Oct-2019 07:01:23

121 Views

For this, you can use LIKE operator. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(40),    BornMonth varchar(40) ); Query OK, 0 rows affected (0.47 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name, BornMonth) values('Chris', 'MONTH_DEC'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name, BornMonth) values('Bob', 'MONTH_JAN'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable(Name, BornMonth) values('Mike', 'MONTH_FEB'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(Name, BornMonth) values('David', 'MONTH_JAN'); Query OK, 1 ... Read More

MySQL edit and update records including employee salary

AmitDiwan
Updated on 04-Oct-2019 06:58:04

402 Views

The UPDATE command is used in MySQL to update records. With it, the SET command is used to set new values. Let us first create a table −mysql> create table DemoTable (    EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    EmployeeName varchar(50),    EmployeeSalary int ); Query OK, 0 rows affected (0.57 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(EmployeeName, EmployeeSalary) values('Chris', 56780); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(EmployeeName, EmployeeSalary) values('Robert', 45670); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(EmployeeName, EmployeeSalary) values('Mike', 87654); Query OK, 1 ... Read More

Does using SERIAL as column name already includes 'NOT NULL' in MySQL?

AmitDiwan
Updated on 04-Oct-2019 06:54:35

115 Views

In MySQL, SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. So, do not use NOT NULL after SERIAL, since it already includes NOT NULL as part of its definition.Let us see an example and create a table. Here, we have a column with the name “serial” −mysql> create table DemoTable (    Id serial ); Query OK, 0 rows affected (0.42 sec)Insert some records in the table using insert command. We aren’t including any value while inserting −mysql> insert into DemoTable values(); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable values(); Query OK, 1 ... Read More

MySQL query to select the nth highest value in a column by skipping values

AmitDiwan
Updated on 04-Oct-2019 06:53:05

136 Views

To get the nth highest value in a column, you can use LIMIT OFFSET. Here, OFFSET is used to skip the values. Let us first create a table −mysql> create table DemoTable (    Value int ) ; Query OK, 0 rows affected (0.49 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(140); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(90); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.09 ... Read More

How to delete rows older than 14 days in MySQL?

AmitDiwan
Updated on 04-Oct-2019 06:51:08

1K+ Views

To delete, use MySQL DELETE. However, to get records older than 14 days, subtract the current date with date interval of 14 days. The syntax for the same is shown below −delete from yourTableName where yourColumnName< (curdate() - interval 14 day);Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    DueDate date ); Query OK, 0 rows affected (0.50 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(DueDate) values('2019-08-20'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(DueDate) values('2019-08-19'); Query OK, 1 row affected ... Read More

Create a table inside a MySQL stored procedure and insert a record on calling the procedure

AmitDiwan
Updated on 04-Oct-2019 06:49:06

474 Views

Create a table inside the stored procedure and use INSERT as well −mysql> DELIMITER // mysql> CREATE PROCEDURE create_TableDemo(id int,name varchar(100),age int)    BEGIN    CREATE TABLE DemoTable    (       ClientId int NOT NULL,       ClientName varchar(30),       ClientAge int,       PRIMARY KEY(ClientId)    );    INSERT INTO DemoTable VALUES(id,name,age);    SELECT *FROM DemoTable;    END // Query OK, 0 rows affected (0.17 sec) mysql> DELIMITER ;Call the stored procedure using CALL command −mysql> CALL create_TableDemo(100,'Robert',28);This will produce the following output −+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ |      100 | Robert     |        28 | +----------+------------+-----------+ 1 row in set (0.76 sec) Query OK, 0 rows affected (0.78 sec)

How to find last date from records with date values in MySQL?

AmitDiwan
Updated on 04-Oct-2019 06:47:01

207 Views

To get the last date i.e. the latest, use aggregate function MAX() with a subquery. Let us first create a table −mysql> create table DemoTable (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ExpiryDate date ); Query OK, 0 rows affected (1.40 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(ExpiryDate) values('2018-12-31'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-09-01'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(ExpiryDate) values('2019-09-01'); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(ExpiryDate) values('2016-08-30'); Query OK, 1 row affected (0.13 ... Read More

Advertisements