Found 4220 Articles for MySQLi

How can we emulate CHECK CONSTRAINT by using MySQL GENERATED COLUMN?

Akshaya Akki
Updated on 19-Jun-2020 13:39:16

65 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 car2 (number char(9)); Query OK, 0 rows affected (0.32 sec) mysql> Insert into car2 values('AB-235-YZ'); Query OK, 1 row affected (0.10 sec)The above value is a valid one but what ... Read More

In MySQL, how we can compute date by providing the year, week number and day of the week?day of the week?

Vrundesha Joshi
Updated on 28-Jan-2020 10:04:30

65 Views

We can compute the date as follows −mysql> SET @year=2017, @week=15, @day=4; Query OK, 0 rows affected (0.00 sec)The above query will pass the value’2017’ ,’15’, ‘4’ in ‘year’, ’week’ and ‘day’ variables respectively. Then after applying the formula in the query below, we can get the date.mysql> SELECT Str_To_Date( Concat(@year,'-',@week,'-',If(@day=7,0,@day) ), '%Y-%U-%w' ) AS Date; +--------------+ | Date         | +--------------+ | 2017-04-13   | +--------------+ 1 row in set (0.00 sec)

How can we emulate CHECK CONSTRAINT by using views?

Vikyath Ram
Updated on 19-Jun-2020 13:39:38

53 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 ‘car1’ which can have the fix syntax registration number like two letters, a dash, three digits, a dash, two letters as follows −mysql> Create table car1 (number char(9)); Query OK, 0 rows affected (0.32 sec) mysql> Insert into car1 values('AB-235-YZ'); Query OK, 1 row affected (0.10 sec)The above value is a valid one but what ... Read More

How to calculate age in years from birthdate in MySQL?

usharani
Updated on 28-Jan-2020 10:08:56

224 Views

We can calculate age in years from birthdate as follows −mysql> SET @dob = '1984-01-17'; Query OK, 0 rows affected (0.00 sec)This above query will pass the value’1984-01-17’ in the ‘dob’ variable. Then after applying the formula in the query below, we can get the age in years.mysql> Select Date_format( From_Days( To_Days(Curdate()) - To_Days(@dob) ), '%Y' ) + 0 AS ‘Age in years; +---------------+ | ‘Age in years’| +---------------+ |   33          | +---------------+ 1 row in set (0.00 sec)

How can we allow MySQL to store invalid dates?

Rishi Rathor
Updated on 28-Jan-2020 10:11:12

226 Views

After enabling the SQL MODE to ALLOW_INVALID_DATES, MySQL will also be able to store invalid dates in the table. The example is given below to understand it −mysql> Insert into order1234(ProductName, Quantity, Orderdate) values('B',500,'2015-11-31'); Query OK, 1 row affected (0.06 sec) mysql> Select * from order1234; +-------------+----------+--------------+ | ProductName | Quantity | OrderDate    | +-------------+----------+--------------+ | A           | 500      | 0000-00-00   | | B           | 500      | 2015-11-31   | +-------------+----------+--------------+ 2 rows in set (0.00 sec)We can see MySQL also inserts the invalid date in a table.

What happens when MySQL encounters an out-of-range date?

varun
Updated on 28-Jan-2020 10:13:49

306 Views

The response of MySQL on encountering out-of-range or invalid date will depend upon SQL MODE. If we have enabled ALLOW_INVALID_DATES mode then MySQL will convert the out of range values into all zeros (i.e. ‘0000:00:00 00:00:00’) and also stores the same in the table without producing any error or warning.For example, we can change SQL MODE as follows and then insert the out-of-range −mysql> set sql_mode = 'ALLOW_INVALID_DATES'; Query OK, 0 rows affected (0.00 sec) mysql> Insert into order1234(productname, quantity, orderdate) values('A', 500, '999-05-100'); Query OK, 1 row affected, 1 warning (0.13 sec) mysql> Select * from order1234; ... Read More

How can we emulate CHECK CONSTRAINT by using triggers?

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

131 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

101 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

Advertisements