Database Articles

Page 289 of 547

How can I clone/duplicate the table along with its data, trigger and indexes?

Sharon Christine
Sharon Christine
Updated on 20-Jun-2020 400 Views

For creating a new table just like old one along with its data, trigger, and indexes, we need to run following two queriesCREATE TABLE new_table LIKE old_table; INSERT new_table SELECT * from old_table;Examplemysql> Create table employee(ID INT PRIMARY KEY NOT NULL AUTO_INCREMENT, NAME VARCHAR(20)); Query OK, 0 rows affected (0.21 sec) mysql> Describe employee; +-------+-------------+------+-----+---------+----------------+ | Field | Type        | Null | Key | Default | Extra          | +-------+-------------+------+-----+---------+----------------+ | ID    | int(11)     | NO   | PRI | NULL    | auto_increment | | NAME  | varchar(20) | ...

Read More

How do I generate days from the range of dates in MySQL?

Nikitha N
Nikitha N
Updated on 20-Jun-2020 2K+ Views

It can be done with the help of following query which uses adddate() function and we are generating the days between ‘2016-12-15’ and ‘2016-12-31’ −mysql> select * from -> (select adddate('1970-01-01', t4*10000 + t3*1000 + t2*100 + t1*10 + t0) gen_date from -> (select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0, -> (select 0 t1 union select 1 union select 2 union select 3 union ...

Read More

What is the significant difference between MySQL TRUNCATE and DROP command?

Monica Mona
Monica Mona
Updated on 20-Jun-2020 426 Views

The most significant difference between MySQL TRUNCATE and DROP command is that TRUNCATE command will not destroy table’s structure but in contrast DROP command will destroy table’s structure too.Examplemysql> Create table testing(id int PRIMARY KEY NOT NULL AUTO_INCREMENT, Name Varchar(20)); Query OK, 0 rows affected (0.24 sec) mysql> Insert into testing(Name) Values('Ram'), ('Mohan'), ('John'); Query OK, 3 rows affected (0.12 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> Select * from testing; +----+-------+ | id | Name  | +----+-------+ | 1  | Ram   | | 2  | Mohan | | 3  | John  | +----+-------+ 3 rows ...

Read More

How can we add multiple columns, with single command, to an existing MySQL table?

Rama Giri
Rama Giri
Updated on 20-Jun-2020 8K+ Views

We can also add multiple columns to an existing table with the help of ALTER command. The syntax for it would be as follows −SyntaxAlter table table-name ADD (column-name1 datatype, column-name2 datatype,… column-nameN datatype);ExampleIn the example below, with the help of ALTER Command, columns ‘Address’, ‘Phone’ and ‘Email’ are added to the table ‘Student’.mysql> Alter Table Student ADD(Address Varchar(25), Phone INT, Email Varchar(20)); Query OK, 5 rows affected (0.38 sec) Records: 5 Duplicates: 0 Warnings: 0

Read More

Date and Time Functions in DBMS

David Meador
David Meador
Updated on 19-Jun-2020 6K+ Views

The date and time functions in DBMS are quite useful to manipulate and store values related to date and time.The different date and time functions are as follows −ADDDATE(DATE, DAYS)The numbers of days in integer form (DAYS) is added to the specified date. This is the value returned by the function. For example −sql> SELECT ADDDATE('2018-08-01', 31); +---------------------------------------------------------+ | DATE_ADD('2018-08-01', INTERVAL 31 DAY)                 | +---------------------------------------------------------+ | 2018-09-01                                             | +---------------------------------------------------------+ 1 ...

Read More

SELECT Statement and its Clauses in DBMS

David Meador
David Meador
Updated on 19-Jun-2020 5K+ Views

The select statement is used to get the required data from the database according to the conditions, if any. This data is returned in the form of a table.The basic syntax of the select statement is −Select column 1, column 2 ... column N From table_nameAn example of the select statement is −Student_NumberStudent_NameStudent_PhoneStudent_MarksStudent_MajorSubject1Andrew661592728495Literature2Sara658365486565Maths3Harry464756746348Literature4Sally653783708430Literature5Anne745733773288MathsQuery −Select Student_Name From StudentThis query yields the following result −Student_NameAndrewSaraHarrySallyAnneClauses in Select statementThe example of select statement given above is quite simple and not that useful in practice. So, there are many other clauses associated with select statement that make it more meaningful. Some of these are ...

Read More

How is it possible to store date such as February 30 in a MySQL date column?

Ankitha Reddy
Ankitha Reddy
Updated on 19-Jun-2020 247 Views

Suppose we want to store the date such as February 30 in a MySQL table then we must have to first set ALLOW_INVALID_DATES mode enabled.For example, I am trying to add, without enabling ALLOW_INVALID_DATES mode, such kind of date in a table then MySQL will give an error as follows −mysql> Insert into date_testing(date) values('2017-02-30'); ERROR 1292 (22007): Incorrect date value: '2017-02-30' for column 'Date' at row1Now we need to enable ALLOW_INVALID_DATES mode enabled as follows −mysql> SET sql_mode = 'ALLOW_INVALID_DATES'; Query OK, 0 rows affected (0.00 sec) mysql> Insert into date_testing(date) values('2017-02-30'); Query OK, 1 row affected (0.14 ...

Read More

What are the different unit values that can be used with MySQL INTERVAL keyword?

Smita Kapse
Smita Kapse
Updated on 19-Jun-2020 199 Views

Different unit values which can be used with MySQL INTERVAL keyword are as follows −MICROSECONDThis unit will be used for adding or subtracting the number of specified microseconds from the current time or as provided by the user.mysql> Select NOW()+INTERVAL 100 MICROSECOND +--------------------------------+ | NOW()+INTERVAL 100 MICROSECOND | +--------------------------------+ | 2017-10-28 18:47:25.000100     | +--------------------------------+ 1 row in set (0.00 sec)Above query will add 100 microseconds to the current date & time with the help of MySQL INTERVAL keyword.mysql> Select '2017-02-25 05:04:30' + INTERVAL 100 Microsecond; +--------------------------------------------------+ | '2017-02-25 05:04:30' + INTERVAL 100 Microsecond | +--------------------------------------------------+ | ...

Read More

How can I insert the values in columns without specifying the names of the column in MySQL INSERT INTO statement?

Kumar Varma
Kumar Varma
Updated on 19-Jun-2020 3K+ Views

For inserting the values in the column without specifying the names of the columns in INSERT INTO statement, we must give the number of values that matches the number of columns in the table along with taking care about the data type of that column too.ExampleIn the example below we have inserted the values without specifying the name of the column.mysql> Insert into student values(100, 'Gaurav', 'Ph.D'); Query OK, 1 row affected (0.08 sec) mysql> Select * from student; +--------+--------+--------+ | RollNO | Name   | Class  | +--------+--------+--------+ | 100    | Gaurav | Ph.D   | +--------+--------+--------+ ...

Read More

How can we insert current date and time automatically on inserting values in other columns in MySQL?

varun
varun
Updated on 19-Jun-2020 2K+ Views

In MySQL, we can insert the current date and time automatically to a column on inserting the values in another column by declaring that column as DEFAULT CURRENT_TIMESTAMP.Examplemysql> Create table testing    -> (    -> StudentName varchar(20) NOT NULL,    -> RegDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP    -> ); Query OK, 0 rows affected (0.49 sec)Above query will create a table ‘testing’ with a column named StudentName and other column named ‘RegDate’ declared as DEFAULT CURRENT_TIMESTAMP. Now, on inserting the values i.e. names in StudentName column, the current date and time will be inserted in the other column automatically.mysql> Insert ...

Read More
Showing 2881–2890 of 5,468 articles
« Prev 1 287 288 289 290 291 547 Next »
Advertisements