Found 4378 Articles for MySQL

Set DEFAULT values for columns while creating a table in MySQL

AmitDiwan
Updated on 09-Sep-2019 09:10:32

2K+ Views

To set default values for columns while creating a table, DEFAULT. Let us first see an example and create a table. As you can see below, while creating the table, we have set DEFAULT −mysql> create table DemoTable803 ( UserId int DEFAULT 101, UserName varchar(100) DEFAULT 'Chris' ); Query OK, 0 rows affected (1.18 sec)Insert some records in the table using insert command. For the values we are not inserting, the default values will get set automatically −mysql> insert into DemoTable803 values(102, 'Chris'); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable803(UserName) ... Read More

Display and concatenate records ignoring NULL values in MySQL

AmitDiwan
Updated on 09-Sep-2019 09:08:45

582 Views

Use CONCAT() to concatenate records whereas IFNULL() to check for NULL values.Let us first create a table −mysql> create table DemoTable802 ( FirstName varchar(100), LastName varchar(100) ); Query OK, 0 rows affected (1.01 sec)Insert some records in the table using insert command −mysql> insert into DemoTable802 values('Adam', 'Smith'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable802 values('Carol', NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable802 values(NULL, 'Taylor'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable802 values(NULL, NULL); Query OK, 1 row affected (0.21 sec)Display ... Read More

MySQL query to perform sort order on same field

AmitDiwan
Updated on 09-Sep-2019 09:06:18

96 Views

For this, use ORDER BY IF().Let us first create a table −mysql> create table DemoTable801 ( Score int ); Query OK, 0 rows affected (0.69 sec)Insert some records in the table using insert command −mysql> insert into DemoTable801 values(30); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable801 values(99); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable801 values(45); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable801 values(55); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable801 values(99); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable801 values(69); ... Read More

MySQL select only a single value from 5 similar values?

AmitDiwan
Updated on 09-Sep-2019 09:04:29

145 Views

Let us first create a table −mysql> create table DemoTable800 ( Value int ); Query OK, 0 rows affected (0.59 sec)Insert some records in the table using insert command. Here, we have inserted 5 similar values −mysql> insert into DemoTable800 values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable800 values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable800 values(10); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable800 values(10); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable800 values(10); Query OK, 1 row affected (0.10 sec)Display all ... Read More

How to concatenate strings using both GROUP_CONCAT() and CONCAT() in the same MySQL query?

AmitDiwan
Updated on 09-Sep-2019 09:03:15

362 Views

The CONCAT() method is used to concat, whereas GROUP_CONCAT() is used to concatenate strings from a group in a single string.Let us first create a table −mysql> create table DemoTable799 ( UserId int, UserName varchar(100), UserAge int ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command −mysql> insert into DemoTable799 values(101, 'John', 21); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable799 values(102, 'Chris', 26); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable799 values(101, 'Robert', 23); Query OK, 1 ... Read More

Why the #1054 - Unknown column error occurs in MySQL and how to fix it?

AmitDiwan
Updated on 09-Sep-2019 09:01:35

3K+ Views

Let’s see when the #1054 error occurs in MySQL. While inserting a varchar value, if you will forget to add single quotes, then this error will arise. Following is the error −mysql> insert into DemoTable798 values(100, Adam); ERROR 1054 (42S22): Unknown column 'Adam' in 'field list'You need to use single quotes around the string value to fix this error as shown below −mysql> insert into DemoTable798 values(100, ’Adam’);Let us first create a table −mysql> create table DemoTable798 ( StudentId int, StudentName varchar(100) ); Query OK, 0 rows affected (0.51 sec)Insert some records in the ... Read More

What is the correct DateTime format for a MySQL Database?

AmitDiwan
Updated on 09-Sep-2019 08:59:55

176 Views

The correct datetime format for MySQL database is as follows −‘YYYY-MM-DD HH:M:SS’Let us first create a table −mysql> create table DemoTable797 ( ArrivalDatetime datetime ); Query OK, 0 rows affected (0.68 sec)Insert some records in the table using insert command −mysql> insert into DemoTable797 values(NOW()); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable797 values('2016-12-21 12:50:34'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable797 values('2017-03-01 17:40:21'); Query OK, 1 row affected (0.24 sec)Display all records from the table using select statement −mysql> select *from DemoTable797;This will produce the following output. We have ... Read More

How to display the day name on the basis of Date of Birth records in MySQL?

AmitDiwan
Updated on 09-Sep-2019 08:52:22

877 Views

Use the DAYNAME() to display the day name from records with Date of Birth.Let us first create a table −mysql> create table DemoTable795 ( DateOfBirth date ); Query OK, 0 rows affected (0.61 sec)Insert some records in the table using insert command −mysql> insert into DemoTable795 values('1996-01-21'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable795 values('2004-11-01'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable795 values('1990-03-31'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable795 values('2010-12-03'); Query OK, 1 row affected (0.13 sec)Display all records from the table using select ... Read More

Get number of fields in MySQL table?

AmitDiwan
Updated on 09-Sep-2019 08:49:46

147 Views

To display number of fields in MySQL, use the COUNT(*). Following is the syntax −select COUNT(*) AS anyAliasName from INFORMATION_SCHEMA.COLUMNS where table_name = yourTableName AND TABLE_SCHEMA = yourDatabaseName;Let us first create a table −mysql> create table DemoTable794 ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(100), ClientAge int, ClientCountryName varchar(100) ); Query OK, 0 rows affected (1.61 sec)Following is the query to get the number of fields in MySQL −mysql> select COUNT(*) AS TOTAL_NUMBER_OF_COLUMNS from INFORMATION_SCHEMA.COLUMNS where table_name = 'DemoTable794' AND ... Read More

Write a single MySQL query to exclude a record and display NULL value

AmitDiwan
Updated on 09-Sep-2019 08:47:49

2K+ Views

To check records which are NULL, use IS NULL. However, to exclude any of the records, use the NOT IN clause. Use both of them in the same query.Let us first create a table −mysql> create table DemoTable793 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(100) ); Query OK, 0 rows affected (0.81 sec)Insert some records in the table using insert command −mysql> insert into DemoTable793(StudentName) values('Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable793(StudentName) values('Bob'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable793(StudentName) values(null); Query ... Read More

Advertisements