Found 4378 Articles for MySQL

MySQL UPDATE column names and set None values with N/A?

AmitDiwan
Updated on 30-Dec-2019 06:14:54

201 Views

Let us first create a table −mysql> create table DemoTable1903    (    FirstName varchar(20),    LastName varchar(20) ,    Age int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1903 values('John', 'Smith', 23); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1903 values('None', 'Miller', 28); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1903 values('None', 'Taylor', 26); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1903 values('Chris', 'Brown', 26); Query OK, 1 row affected (0.00 sec)Display all records from the table ... Read More

How to search for particular strings between comma separated values in MySQL?

AmitDiwan
Updated on 27-Dec-2019 07:11:07

517 Views

For this, use REGEXP. Let us first create a table −mysql> create table DemoTable1902    (    Subjects text    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1902 values('MongoDB, Java, Python'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1902 values('SQL Server, MySQL, PL/SQL'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1902 values('Hibernate, Spring, JPA'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1902;This will produce the following output −+-------------------------+ | Subjects ... Read More

MySQL query to copy records from one table to another with different columns

AmitDiwan
Updated on 27-Dec-2019 07:09:41

519 Views

For this you can use INSERT INTO SELECT statement. Let us first create a table −mysql> create table DemoTable1900    (    ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    ClientName varchar(20),    ClientAge int default 29    ) auto_increment=1000; Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1900(ClientName, ClientAge) values('Chris', 45); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1900(ClientName, ClientAge) values('David', 29); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1900(ClientName, ClientAge) values('Mike', 37); Query OK, 1 row affected (0.00 sec) mysql> insert into ... Read More

How to replace a particular character in a MySQL column?

AmitDiwan
Updated on 27-Dec-2019 07:06:24

293 Views

To replace a particular character, use REPLACE() and to update, use the UPDATE command. Let us first create a table −mysql> create table DemoTable1899    (    Code varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1899 values('John_123'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1899 values('32189_Sam_987'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1899 values('Miller_David_456_909'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1899;This will produce the following output ... Read More

MySQL ORDER BY with numeric user-defined variable?

AmitDiwan
Updated on 27-Dec-2019 07:05:19

267 Views

Let us first create a table −mysql> create table DemoTable1898    (    Number int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1898 values(10); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1898 values(70); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1898 values(30); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1898 values(50); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1898 values(40); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement ... Read More

Get field value and convert a particular character from it to uppercase with MySQL

AmitDiwan
Updated on 27-Dec-2019 07:04:08

82 Views

Let us first create a table −mysql> create table DemoTable1897    (    Name varchar(20)    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1897 values('john'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('jace'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1897 values('Chris'); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement ... Read More

Get only a single value from a specific MySQL row?

AmitDiwan
Updated on 27-Dec-2019 07:02:32

1K+ Views

For this, use SELECT INTO variable with where clause. Let us first create a table −mysql> create table DemoTable1896    (    StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    StudentName varchar(20),    StudentMarks int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1896(StudentName, StudentMarks) values('Chris', 56); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName, StudentMarks) values('David', 98); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName, StudentMarks) values('Mike', 89); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1896(StudentName, StudentMarks) values('Sam', ... Read More

What to assign to a MySQL column that must not be empty?

AmitDiwan
Updated on 27-Dec-2019 06:58:48

298 Views

Define with NOT NULL, if a column must not be empty. Let us first create a table with one of the columns as NOT NULL −mysql> create table DemoTable1895    (    Id int NOT NULL,    FirstName varchar(20),    LastName varchar(20) NOT NULL    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1895 values(100, 'John', 'Smith'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1895 values(NULL, 'Chris', 'Brown'); ERROR 1048 (23000): Column 'Id' cannot be null mysql> insert into DemoTable1895 values(102, 'Carol', NULL); ERROR 1048 (23000): ... Read More

Set a MySQL field with the current date (UNIX_TIMESTAMP(now))

AmitDiwan
Updated on 27-Dec-2019 06:57:19

486 Views

For this, use unix_timestamp(). Let us first create a table −mysql> create table DemoTable1894    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    DueTime int    ); Query OK, 0 rows affected (0.00 sec)Insert some records in the table using insert command −mysql> insert into DemoTable1894 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1894 values(); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1894 values(); Query OK, 1 row affected (0.00 sec)Display all records from the table using select statement −mysql> select * from DemoTable1894;This will produce the following output −+----+---------+ | ... Read More

Java application to insert null value into a MySQL database?

AmitDiwan
Updated on 27-Dec-2019 06:55:33

785 Views

To set null value with Java, the statement is as follows −ps.setNull(yourIndex, Types.NULL);Let us first create a table −mysql> create table DemoTable1893    (    FirstName varchar(20)    ); Query OK, 0 rows affected (0.00 sec)The Java code is as follows −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.Types; public class InsertNullValueIntoDatabase{    public static void main(String[] args){       Connection con=null;       PreparedStatement ps=null;       try{          con=DriverManager.getConnection("jdbc:mysql://localhost:3306/web?useSSL=false",          "root", "123456");          String query="insert into DemoTable1893(FirstName) values(?) ";          ps= con.prepareStatement(query);   ... Read More

Advertisements