Database Articles

Page 470 of 547

Can we add a column to a table from another table in MySQL?

Samual Sam
Samual Sam
Updated on 30-Jul-2019 2K+ Views

Yes, we can add a column to a table from another table. Let us first create two tables. The query to create a table is as follows −mysql> create table FirstTable    -> (    -> UserId int,    -> UserName varchar(20)    -> ); Query OK, 0 rows affected (1.48 sec)Now create the second table. The query to create the second table is as follows −mysql> create table SecondTable    -> (    -> UserId int,    -> UserAge int    -> ); Query OK, 0 rows affected (1.57 sec)Now, add column Age to the first table. Firstly, ...

Read More

MySQL query to check if database is empty or not?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 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

How can I make a table in MySQL called "order"?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 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

Get total in the last row of MySQL result?

karthikeya Boyini
karthikeya Boyini
Updated on 30-Jul-2019 749 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

How to select most recent date out of a set of several possible timestamps in MySQL?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 212 Views

You can select most recent date out of a set of several possible timestamps with the help of ORDER BY clause.The syntax is as followsSELECT yourColumnName1, yourColumnName2, ...N FROM yourTableName ORDER BY yourTimestampColumnName DESC LIMIT 1;To understand the above syntax, let us create a table. The query to create a table is as followsmysql> create table MostRecentDateDemo    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > Name varchar(10),    - > ShippingDate timestamp    - > ); Query OK, 0 rows affected (0.65 sec)Insert some records in the table using insert command. ...

Read More

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

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 245 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

How to order by date and time in MySQL?

George John
George John
Updated on 30-Jul-2019 2K+ Views

You need to use ORDER BY clause for this. Let us first create a table. The query to create a table is as followsmysql> create table OrderByDateThenTimeDemo - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > ShippingDate date, - > ShippingTime time - > ); Query OK, 0 rows affected (0.56 sec)Now you can insert some records in the table using insert command. Here, we have two similar dates, but different times i.e. 2018-01-24mysql> insert into OrderByDateThenTimeDemo(ShippingDate, ShippingTime) ...

Read More

How to get the second last record from a table in MySQL?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 5K+ Views

To get the record before the last one i.e. the second last record in MySQL, you need to use subquery.The syntax is as followsSELECT *FROM (SELECT *FROM yourTableName ORDER BY yourIdColumnName DESC LIMIT 2) anyAliasName ORDER BY yourIdColumnName LIMIT 1;Let us first create a table. The query to create a table is as followsmysql> create table lastRecordBeforeLastOne    - > (    - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > Name varchar(20) DEFAULT 'John',    - > Age int DEFAULT 18    - > ); Query OK, 0 rows affected (0.79 sec)Now you can insert some ...

Read More

Add 6 hours to now() function without using DATE_ADD() in MySQL?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 296 Views

Let us first create a table wherein one of the columns is with datetime. The query to create a table is as followsmysql> create table Add6Hour - > ( - > Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, - > ArrivalTime datetime - > ); Query OK, 0 rows affected (0.56 sec)Insert some records in the table using insert command.The query is as followsmysql> insert into Add6Hour(ArrivalTime) values(now()); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement.The query is as followsmysql> select ...

Read More

Prevent negative numbers in MySQL?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 2K+ Views

To prevent negative numbers in MySQL, you need to use INT UNSIGNED. Let’s say you created a table with a column as int i.e. UserGameScores heremysql> create table preventNegativeNumberDemo    - > (    - > UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    - > UserName varchar(20),    - > UserGameScores int    - > ); Query OK, 0 rows affected (1.74 sec)Now if you need to prevent negative numbers in it, modify the same column with INT UNSIGNEDmysql> alter table preventNegativeNumberDemo modify column UserGameScores INT UNSIGNED NOT NULL; Query OK, 0 rows affected (3.32 sec) Records: 0 ...

Read More
Showing 4691–4700 of 5,468 articles
« Prev 1 468 469 470 471 472 547 Next »
Advertisements