Found 6705 Articles for Database

How to generate a “create table” command based on an existing table in MySQL?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

186 Views

You can generate a create table command based on an existing table in MySQL with the help of SHOW CREATE command.The syntax is as followsSHOW CREATE TABLE yourTableName;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table StudentInformation    - > (    - > StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > StudentName varchar(20),    - > StudentAge int DEFAULT 18,    - > StudentRollNo int,    - > StudentAddress varchar(200),    - > StudentMarks int,    - > StudentDOB datetime,    - > StudentAdmissionDate datetime ... Read More

Is PHP deg2rad() equal to MySQL radians()?

George John
Updated on 30-Jul-2019 22:30:25

113 Views

Yes, both of these methods convert a degree value to radian. Let us create a table to understand MySQL radians. The query to create a table is as followsmysql> create table RadiansDemo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > Value int    - > ); Query OK, 0 rows affected (0.58 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into RadiansDemo(Value) values(0); Query OK, 1 row affected (0.14 sec) mysql> insert into RadiansDemo(Value) values(45); Query OK, 1 row affected (0.17 sec) mysql> insert into ... Read More

How can I make a table in MySQL called “order”?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

5K+ Views

As you know, order is a keyword in MySQL, you cannot give table name order directly. You need to use backtick around the table name order. Backtick allow a user to consider the keyword as table or column name.The syntax is as followsCREATE TABLE `order` (    yourColumnName1 dataType,    yourColumnName2 dataType,    yourColumnName3 dataType,    .    .    .    .    N );Let us create a table. The query to create a table is as followsmysql> create table `order`    - > (    - > Id int,    - > Price int    - > ); ... Read More

How to combine date and time from different MySQL columns to compare with the entire DateTime?

Samual Sam
Updated on 26-Jun-2020 10:31:06

1K+ Views

You can combine date and time from different MySQL columns to compare with the entire date time with the help of CONCAT() function. The syntax is as follows −SELECT *FROM yourTableName WHERE CONCAT(yourDateColumnName, '', yourTimeColumnName) > 'yourDateTimeValue';To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table DifferentDateTime     -> (     -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,     -> ArrivalDate date,     -> ArrivalTime time     -> ); Query OK, 0 rows affected (1.53 sec)Insert some records in the table using ... Read More

Get total in the last row of MySQL result?

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

674 Views

To get total in the last row of MySQL result, use the following syntax −(    SELECT yourColumnName1,    yourColumnName2,    yourColumnName3,    .    .    N    FROM yourTableName ) UNION (    SELECT "yourMessage" AS anyAliasName1,    SUM(yourColumnName1) AS anyAliasName2,    SUM(yourColumnName2) AS anyAliasName3,    .    .    N    FROM yourTableName );To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table ProductDemo    -> (    -> ProductId varchar(10),    -> ProductQuantity int,    -> ProductValue int    -> ); Query OK, 0 ... Read More

Fetch rows where a field value is less than 5 chars in MySQL?

Samual Sam
Updated on 26-Jun-2020 10:29:35

733 Views

To fetch rows where a field value is less than 5 chars, you need to use LENGTH() function. The syntax is as follows −SELECT *FROM yourTableName WHERE LENGTH(yourColumnName) < 5;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table fieldLessThan5Chars    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> yourZipCode varchar(10)    -> ); Query OK, 0 rows affected (0.52 sec)Now you can insert some records in the table using insert command. The query is as follows −mysql> insert into fieldLessThan5Chars(yourZipCode) values('35801'); Query ... Read More

MySQL query to check if database is empty or not?

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

2K+ Views

You can use INFORMATION_SCHEMA.COLUMNS to check if a database is empty or not. The syntax is as follows −SELECT COUNT(DISTINCT `TABLE_NAME`) AS anyAliasName FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `table_schema` = 'yourDatabaseName';The above syntax returns 0 if the database has notable otherwise it returns the number of tables. For our example, we are using the databases ‘sample’ and ‘test3’, which we created before.The first database ‘sample’ has more tables, therefore the above query will return a number of tables. The second database ‘test3’ does not have any tables, therefore the above query will return 0.Case 1 − Database sampleThe query is as follows ... Read More

MySQL query to include more than one column in a table that doesn't already exist

Samual Sam
Updated on 26-Jun-2020 10:28:03

141 Views

You can easily add more than one column that does not exist in a query using multiple AS keywords.Let us first create a table. The query to create a table is as follows −mysql> create table ColumnDoesNotExists    -> (    -> UserId int,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (0.67 sec)Insert some records in the table using insert command. The query is as follows −mysql> insert into ColumnDoesNotExists(UserId, UserName) values(100, 'Larry'); Query OK, 1 row affected (0.14 sec) mysql> insert into ColumnDoesNotExists(UserId, UserName) values(101, 'Sam'); Query OK, 1 row affected (0.22 sec) mysql> ... Read More

Adding a column that doesn't exist in a query?

karthikeya Boyini
Updated on 26-Jun-2020 10:26:12

1K+ Views

Add a column that does not exist in a query, with the help of AS keyword. The syntax is as follows −SELECT yourColumnName1, yourColumnName2, ....N, yourValue AS yourColumnName, ....N' FROM yourTableName;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table ColumnDoesNotExists     -> (     -> UserId int,     -> UserName varchar(20)     -> ); Query OK, 0 rows affected (0.67 sec)ExampleInsert some records in the table using insert command. The query is as follows −mysql> insert into ColumnDoesNotExists(UserId, UserName) values(100, 'Larry'); Query OK, ... Read More

How to select first and last data row from a MySQL result?

Samual Sam
Updated on 26-Jun-2020 10:23:45

2K+ Views

You can select the first and last data row using MIN() and MAX(). The syntax is as follows −SELECT * FROM yourTableName WHERE yourColumnName = (SELECT MIN(yourColumnName) FROM yourTableName) UNION SELECT * FROM yourTableName WHERE yourColumnName = (SELECT MAX(yourColumnName) FROM yourTableName) ;To understand the above syntax, let us create a table. The query to create a table is as follows −mysql> create table FirstAndLastDataDemo    -> (    -> EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> EmployeeName varchar(20),    -> EmployeeAge int    -> ); Query OK, 0 rows affected (0.59 sec)ExampleInsert some records in the table using ... Read More

Advertisements