Found 4469 Articles for MySQLi

How can we emulate CHECK CONSTRAINT by using triggers?

Rama Giri
Updated on 19-Jun-2020 13:38:03

122 Views

As we know that MySQL supports foreign key for referential integrity but it does not support CHECK constraint. But we can emulate them by using triggers. It can be illustrated with the help of an example given below −ExampleSuppose we have a table named ‘car’ which can have the fix syntax registration number like two letters, a dash, three digits, a dash, two letters as follows −mysql> Create table car (number char(9)); Query OK, 0 rows affected (0.32 sec) mysql> Insert into car values('AB-235-YZ'); Query OK, 1 row affected (0.10 sec)The above value is a valid one but what ... Read More

How can we assign FOREIGN KEY constraint on multiple columns?

Monica Mona
Updated on 19-Jun-2020 13:37:15

4K+ Views

MySQL allows us to add a FOREIGN KEY constraint on multiple columns in a table. The condition is that each Foreign Key in the child table must refer to the different parent table.ExampleSuppose we have a table ‘customer2’ which have a Primary Key constraint on the field ‘cust_unq_id’ as follows −mysql> describe customer2; +-------------+-------------+------+-----+---------+-------+ | Field       | Type        | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | cust_id     | int(11)     | YES  |     | NULL    |       | | First_name  | varchar(20) | ... Read More

What is the difference between MySQL DATETIME and TIMESTAMP data type?

Nancy Den
Updated on 19-Jun-2020 13:33:54

9K+ Views

Both the data types store data in “YYYY-MM-DD HH:MM:SS” format and include date as well as time. In spite of these similarities they are having the following differences −Range − Datetime data type supports a date along with time in the range between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. But timestamp data type supports a date along with time in the range between ‘1970-01-01 00:00:01’ to ‘2038-01-19 08:44:07’.Size − Datetime requires 5 bytes along with 3 additional bytes for fractional seconds’ data storing. On the other hand, timestamp datatype requires 4 bytes along with 3 additional bytes for fractional seconds’ data ... Read More

How can we apply AUTO_INCREMENT to a column?

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

98 Views

AUTO_INCREMENT means that the column will get the value automatically. To illustrate it we have created a table ‘employees’ as follows − mysql> Show Create Table employees\G *************************** 1. row *************************** Table: employees Create Table: CREATE TABLE `employees` ( `Id` int(11) NOT NULL AUTO_INCREMENT, `Name` varchar(35) DEFAULT NULL, PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) From the above result set, we can see that column id is given the auto-increment option. Now, when we will insert the value in Name ... Read More

How to search a record by date in MySQL table?

Prabhas
Updated on 28-Jan-2020 09:22:39

732 Views

Suppose we have a table ‘Order123’ having ProductName, Quantity and OrderDate columns as follows −mysql> Select * from Order123; +-------------+----------+------------+ | ProductName | Quantity | OrderDate  | +-------------+----------+------------+ | A           | 100      | 2017-05-25 | | B           | 105      | 2017-05-25 | | C           | 55       | 2017-05-25 | | D           | 250      | 2017-05-26 | | E           | 500      | 2017-05-26 | | ... Read More

Why we cannot use MySQL DATE data type along with time value?

seetha
Updated on 28-Jan-2020 09:23:21

65 Views

The default format for MySQL DATE data type is “YYYY-MM-DD” and in this format, there is no possibility to store the time value. Hence, we can say that we cannot use DATE data type along with time value.As we can see in the following example MySQL returns only date value even on using time along with the date.mysql> select DATE("2017-09-25 09:34:21"); +-----------------------------------+ | DATE("2017-09-25 09:34:21")       | +-----------------------------------+ | 2017-09-25                        | +-----------------------------------+ 1 row in set (0.04 sec)However, in DATETIME and TIMESTAMP date data types we can use the time to date.

What is the use of DEFAULT constraint? How can it be applied to a column while creating a table?

Ankith Reddy
Updated on 19-Jun-2020 13:38:37

54 Views

DEFAULT constraint is used set a default value for a column in MySQL table. If it is applied on a column then it will take the default value of not giving any value for that column. Its syntax would be as follows −SyntaxDEFAULT default_valueHere, default_value is the default value set for the column.ExampleThe query below will create a table named workers where we assign the column id a DEFAULT value 1000.mysql> Create table workers(Name Varchar(25), Id INT NOT NULL DEFAULT 1000); Query OK, 0 rows affected (0.47 sec) mysql> Insert into workers(Name, Id) values('Ram', 101); Query OK, 1 row ... Read More

How many DATE data types are supported by MySQL?

Daniol Thomas
Updated on 19-Jun-2020 13:32:12

80 Views

MySQL supports following 5 types of DATE data type −DATE - A date is in the range between 1000-01-01 and 9999-12-31. “YYYY-MM-DD” is the default DATE format. For example, January 17th, 1984 would be stored as 1984-01-17.DATETIME − This data type supports a date along with time in the range between 1000-01-01 00:00:00 and 9999-12-31 23:59:59. “YYYY-MM-DD HH:MM:SS” is the default DATETIME format. For example, 2:20 in the afternoon on January 17th, 1984 would be stored as 1984-01-17 14:20:00.TIMESTAMP − A timestamp data type supports a date along with time in the range between ‘1970-01-01 00:00:01’ to ‘2038-01-19 08:44:07’. It ... Read More

How can I see the constraints which are applied to a table stored in another database then I am currently using?

Sai Nath
Updated on 19-Jun-2020 13:32:38

42 Views

MySQL SHOW CREATE TABLE statement will provide us the constraints applied to a particular table along with some other details about that table stored in another database then I am currently using. Its syntax would be as follows −SyntaxSHOW CREATE TABLE db_name.table_name;Here table_name is the name of the table on which we want to see the constraints.Db_name is the name of the database name in which table is stored.ExampleIn this example we are getting the detail of the table named ‘arena’ stored in MySQL database −mysql> Show Create table mysql.arena\G *************************** 1. row *************************** Table: arena Create Table: CREATE TABLE ... Read More

How can I see the constraints which are applied to a table stored in the database I am currently using?

Samual Sam
Updated on 19-Jun-2020 13:30:26

47 Views

MySQL SHOW CREATE TABLE statement will provide us the constraints applied to a particular table along with some other details about that table. Its syntax would be as follows −SyntaxSHOW CREATE TABLE table_name;Here table_name is the name of the table on which we want to see the constraints.ExampleIn this example we are getting the detail of the table named ‘employees’ −mysql> Show Create table employees\G *************************** 1. row ***************************        Table: employees Create Table: CREATE TABLE `employees` (    `Id` int(11) NOT NULL AUTO_INCREMENT,    `Name` varchar(35) DEFAULT NULL,    PRIMARY KEY (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 ... Read More

Advertisements