mariadb/mysql-test/r/default.result
Alexander Barkov b5870a5dd3 More test for MDEV-10134 Add full support for DEFAULT
Functions DATABASE() and USER().
2016-06-30 11:43:02 +02:00

2005 lines
59 KiB
Text

drop table if exists t1,t2,t3,t4,t5,t6;
drop database if exists mysqltest;
set sql_mode="";
CREATE TABLE t1 (a varchar(30) binary NOT NULL DEFAULT ' ',
b varchar(1) binary NOT NULL DEFAULT ' ',
c varchar(4) binary NOT NULL DEFAULT '0000',
d tinyblob NULL,
e tinyblob NULL,
f tinyblob NULL,
g tinyblob NULL,
h tinyblob NULL,
i tinyblob NULL,
j tinyblob NULL,
k tinyblob NULL,
l tinyblob NULL,
m tinyblob NULL,
n tinyblob NULL,
o tinyblob NULL,
p tinyblob NULL,
q varchar(30) binary NOT NULL DEFAULT ' ',
r varchar(30) binary NOT NULL DEFAULT ' ',
s tinyblob NULL,
t varchar(4) binary NOT NULL DEFAULT ' ',
u varchar(1) binary NOT NULL DEFAULT ' ',
v varchar(30) binary NOT NULL DEFAULT ' ',
w varchar(30) binary NOT NULL DEFAULT ' ',
x tinyblob NULL,
y varchar(5) binary NOT NULL DEFAULT ' ',
z varchar(20) binary NOT NULL DEFAULT ' ',
a1 varchar(30) binary NOT NULL DEFAULT ' ',
b1 tinyblob NULL)
ENGINE=InnoDB DEFAULT CHARACTER SET = latin1 COLLATE latin1_bin;
set sql_mode=default;
INSERT into t1 (b) values ('1');
SHOW WARNINGS;
Level Code Message
SELECT * from t1;
a b c d e f g h i j k l m n o p q r s t u v w x y z a1 b1
1 0000 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
CREATE TABLE t2 (a varchar(30) binary NOT NULL DEFAULT ' ',
b varchar(1) binary NOT NULL DEFAULT ' ',
c varchar(4) binary NOT NULL DEFAULT '0000',
d tinyblob NULL,
e tinyblob NULL,
f tinyblob NULL,
g tinyblob NULL,
h tinyblob NULL,
i tinyblob NULL,
j tinyblob NULL,
k tinyblob NULL,
l tinyblob NULL,
m tinyblob NULL,
n tinyblob NULL,
o tinyblob NULL,
p tinyblob NULL,
q varchar(30) binary NOT NULL DEFAULT ' ',
r varchar(30) binary NOT NULL DEFAULT ' ',
s tinyblob NULL,
t varchar(4) binary NOT NULL DEFAULT ' ',
u varchar(1) binary NOT NULL DEFAULT ' ',
v varchar(30) binary NOT NULL DEFAULT ' ',
w varchar(30) binary NOT NULL DEFAULT ' ',
x tinyblob NULL,
y varchar(5) binary NOT NULL DEFAULT ' ',
z varchar(20) binary NOT NULL DEFAULT ' ',
a1 varchar(30) binary NOT NULL DEFAULT ' ',
b1 tinyblob NULL)
ENGINE=MyISAM DEFAULT CHARACTER SET = latin1 COLLATE latin1_bin;
SHOW CREATE TABLE t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` varchar(30) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`b` varchar(1) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`c` varchar(4) COLLATE latin1_bin NOT NULL DEFAULT '0000',
`d` tinyblob DEFAULT NULL,
`e` tinyblob DEFAULT NULL,
`f` tinyblob DEFAULT NULL,
`g` tinyblob DEFAULT NULL,
`h` tinyblob DEFAULT NULL,
`i` tinyblob DEFAULT NULL,
`j` tinyblob DEFAULT NULL,
`k` tinyblob DEFAULT NULL,
`l` tinyblob DEFAULT NULL,
`m` tinyblob DEFAULT NULL,
`n` tinyblob DEFAULT NULL,
`o` tinyblob DEFAULT NULL,
`p` tinyblob DEFAULT NULL,
`q` varchar(30) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`r` varchar(30) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`s` tinyblob DEFAULT NULL,
`t` varchar(4) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`u` varchar(1) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`v` varchar(30) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`w` varchar(30) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`x` tinyblob DEFAULT NULL,
`y` varchar(5) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`z` varchar(20) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`a1` varchar(30) COLLATE latin1_bin NOT NULL DEFAULT ' ',
`b1` tinyblob DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_bin
INSERT into t2 (b) values ('1');
SHOW WARNINGS;
Level Code Message
SELECT * from t2;
a b c d e f g h i j k l m n o p q r s t u v w x y z a1 b1
1 0000 NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
drop table t1;
drop table t2;
create table bug20691 (i int, d datetime NOT NULL, dn datetime not null default '0000-00-00 00:00:00');
insert into bug20691 values (1, DEFAULT, DEFAULT), (1, '1975-07-10 07:10:03', '1978-01-13 14:08:51'), (1, DEFAULT, DEFAULT);
Warnings:
Warning 1364 Field 'd' doesn't have a default value
Warning 1364 Field 'd' doesn't have a default value
insert into bug20691 (i) values (2);
Warnings:
Warning 1364 Field 'd' doesn't have a default value
desc bug20691;
Field Type Null Key Default Extra
i int(11) YES NULL
d datetime NO NULL
dn datetime NO 0000-00-00 00:00:00
insert into bug20691 values (3, DEFAULT, DEFAULT), (3, '1975-07-10 07:10:03', '1978-01-13 14:08:51'), (3, DEFAULT, DEFAULT);
Warnings:
Warning 1364 Field 'd' doesn't have a default value
Warning 1364 Field 'd' doesn't have a default value
insert into bug20691 (i) values (4);
Warnings:
Warning 1364 Field 'd' doesn't have a default value
insert into bug20691 values (5, DEFAULT, DEFAULT), (5, '1975-07-10 07:10:03', '1978-01-13 14:08:51'), (5, DEFAULT, DEFAULT);
Warnings:
Warning 1364 Field 'd' doesn't have a default value
Warning 1364 Field 'd' doesn't have a default value
SET sql_mode = 'ALLOW_INVALID_DATES';
insert into bug20691 values (6, DEFAULT, DEFAULT), (6, '1975-07-10 07:10:03', '1978-01-13 14:08:51'), (6, DEFAULT, DEFAULT);
Warnings:
Warning 1364 Field 'd' doesn't have a default value
Warning 1364 Field 'd' doesn't have a default value
SET sql_mode = 'STRICT_ALL_TABLES';
insert into bug20691 values (7, DEFAULT, DEFAULT), (7, '1975-07-10 07:10:03', '1978-01-13 14:08:51'), (7, DEFAULT, DEFAULT);
ERROR HY000: Field 'd' doesn't have a default value
select * from bug20691 order by i asc;
i d dn
1 0000-00-00 00:00:00 0000-00-00 00:00:00
1 1975-07-10 07:10:03 1978-01-13 14:08:51
1 0000-00-00 00:00:00 0000-00-00 00:00:00
2 0000-00-00 00:00:00 0000-00-00 00:00:00
3 0000-00-00 00:00:00 0000-00-00 00:00:00
3 1975-07-10 07:10:03 1978-01-13 14:08:51
3 0000-00-00 00:00:00 0000-00-00 00:00:00
4 0000-00-00 00:00:00 0000-00-00 00:00:00
5 0000-00-00 00:00:00 0000-00-00 00:00:00
5 1975-07-10 07:10:03 1978-01-13 14:08:51
5 0000-00-00 00:00:00 0000-00-00 00:00:00
6 0000-00-00 00:00:00 0000-00-00 00:00:00
6 1975-07-10 07:10:03 1978-01-13 14:08:51
6 0000-00-00 00:00:00 0000-00-00 00:00:00
drop table bug20691;
SET sql_mode = '';
create table bug20691 (
a set('one', 'two', 'three') not null,
b enum('small', 'medium', 'large', 'enormous', 'ellisonego') not null,
c time not null,
d date not null,
e int not null,
f long not null,
g blob not null,
h datetime not null,
i decimal not null,
x int);
insert into bug20691 values (2, 3, 5, '0007-01-01', 11, 13, 17, '0019-01-01 00:00:00', 23, 1);
insert into bug20691 (x) values (2);
Warnings:
Warning 1364 Field 'a' doesn't have a default value
Warning 1364 Field 'c' doesn't have a default value
Warning 1364 Field 'd' doesn't have a default value
Warning 1364 Field 'e' doesn't have a default value
Warning 1364 Field 'f' doesn't have a default value
Warning 1364 Field 'g' doesn't have a default value
Warning 1364 Field 'h' doesn't have a default value
Warning 1364 Field 'i' doesn't have a default value
insert into bug20691 values (2, 3, 5, '0007-01-01', 11, 13, 17, '0019-01-01 00:00:00', 23, 3);
insert into bug20691 values (DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, 4);
Warnings:
Warning 1364 Field 'a' doesn't have a default value
Warning 1364 Field 'c' doesn't have a default value
Warning 1364 Field 'd' doesn't have a default value
Warning 1364 Field 'e' doesn't have a default value
Warning 1364 Field 'f' doesn't have a default value
Warning 1364 Field 'g' doesn't have a default value
Warning 1364 Field 'h' doesn't have a default value
Warning 1364 Field 'i' doesn't have a default value
select * from bug20691 order by x asc;
a b c d e f g h i x
two large 00:00:05 0007-01-01 11 13 17 0019-01-01 00:00:00 23 1
small 00:00:00 0000-00-00 0 0000-00-00 00:00:00 0 2
two large 00:00:05 0007-01-01 11 13 17 0019-01-01 00:00:00 23 3
small 00:00:00 0000-00-00 0 0000-00-00 00:00:00 0 4
drop table bug20691;
create table t1 (id int not null);
insert into t1 values(default);
Warnings:
Warning 1364 Field 'id' doesn't have a default value
create view v1 (c) as select id from t1;
insert into t1 values(default);
Warnings:
Warning 1364 Field 'id' doesn't have a default value
drop view v1;
drop table t1;
create table t1 (a int unique);
create table t2 (b int default 10);
insert into t1 (a) values (1);
insert into t2 (b) values (1);
insert into t1 (a) select b from t2 on duplicate key update a=default;
select * from t1;
a
NULL
insert into t1 (a) values (1);
insert into t1 (a) select b from t2 on duplicate key update a=default(b);
select * from t1;
a
NULL
10
drop table t1, t2;
End of 5.0 tests.
#
# Start of 10.1 tests
#
CREATE TABLE t1 (a INT DEFAULT 100, b INT DEFAULT NULL);
INSERT INTO t1 VALUES ();
SELECT * FROM t1 WHERE DEFAULT(a);
a b
100 NULL
SELECT * FROM t1 WHERE DEFAULT(b);
a b
DROP TABLE IF EXISTS t1;
#
# End of 10.1 tests
#
#
# Start of 10.2 tests
#
Check that CURRENT_TIMESTAMP works as before
CREATE or replace TABLE t1 (event_time TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
CREATE or replace TABLE t1 (event_time TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`event_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
CREATE or replace TABLE t1 (event_time TIMESTAMP(6) NOT NULL DEFAULT SYSDATE(2) ON UPDATE CURRENT_TIMESTAMP);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`event_time` timestamp(6) NOT NULL DEFAULT SYSDATE(2) ON UPDATE CURRENT_TIMESTAMP(6)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
Check default expressions
create or replace table t1 (a int default 1, b int default a+1, c int default a+b) engine myisam;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT '1',
`b` int(11) DEFAULT a+1,
`c` int(11) DEFAULT a+b
) ENGINE=MyISAM DEFAULT CHARSET=latin1
insert into t1 values ();
insert into t1 (a) values (2);
insert into t1 (a,b) values (10,20);
insert into t1 (a,b,c) values (100,200,300);
select * from t1;
a b c
1 2 3
2 3 5
10 20 30
100 200 300
truncate table t1;
insert delayed into t1 values ();
insert delayed into t1 (a) values (2);
insert delayed into t1 (a,b) values (10,20);
insert delayed into t1 (a,b,c) values (100,200,300);
flush tables t1;
select * from t1;
a b c
1 2 3
2 3 5
10 20 30
100 200 300
create or replace table t1 (a int, b blob default (1), c blob default ("hello"), t text default (concat(a,b,c))) engine=myisam;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` blob DEFAULT (1),
`c` blob DEFAULT ("hello"),
`t` text DEFAULT (concat(a,b,c))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
insert into t1 (a) values (2);
insert into t1 (a,b) values (10,"test1");
insert into t1 (a,b,c) values (10,"test2","test3");
insert delayed into t1 (a,b) values (10,"test4");
flush tables t1;
select * from t1;
a b c t
2 1 hello 21hello
10 test1 hello 10test1hello
10 test2 test3 10test2test3
10 test4 hello 10test4hello
drop table t1;
create or replace table t1 (a bigint default uuid_short());
insert into t1 values();
select a > 0 from t1;
a > 0
1
drop table t1;
create or replace table t1 (param_list int DEFAULT (1+1) NOT NULL);
create or replace table t1 (param_list int DEFAULT 1+1 NOT NULL);
create or replace table t1 (param_list blob DEFAULT "" NOT NULL);
drop table t1;
create table t1 (a int);
insert into t1 values(-1);
alter table t1 add b int default 1, add c int default -1, add d int default 1+1, add e timestamp;
select a,b,c,d,e > 0 from t1;
a b c d e > 0
-1 1 -1 2 1
insert into t1 values(10,10,10,10,0);
alter table t1 add f int default 1+1+1 null, add g int default 1+1+1+1 not null,add h int default (2+2+2+2);
select a,b,c,d,e > 0,f,g,h from t1;
a b c d e > 0 f g h
-1 1 -1 2 1 3 4 8
10 10 10 10 0 3 4 8
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT '1',
`c` int(11) DEFAULT '-1',
`d` int(11) DEFAULT 1+1,
`e` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`f` int(11) DEFAULT 1+1+1,
`g` int(11) NOT NULL DEFAULT 1+1+1+1,
`h` int(11) DEFAULT (2+2+2+2)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
create table t2 like t1;
show create table t2;
Table Create Table
t2 CREATE TABLE `t2` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT '1',
`c` int(11) DEFAULT '-1',
`d` int(11) DEFAULT 1+1,
`e` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`f` int(11) DEFAULT 1+1+1,
`g` int(11) NOT NULL DEFAULT 1+1+1+1,
`h` int(11) DEFAULT (2+2+2+2)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
insert into t2 (a) values (100);
select a,b,c,d,e > 0,f,g,h from t2;
a b c d e > 0 f g h
100 1 -1 2 1 3 4 8
drop table t1,t2;
create table t1 (a int default 1----1);
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT 1----1
) ENGINE=MyISAM DEFAULT CHARSET=latin1
insert into t1 values();
insert into t1 values();
select * from t1;
a
2
2
drop table t1;
create or replace can delete a table on error
create table t1 (a int);
create or replace table t1 (a int default b, b int default a);
ERROR 01000: Expression for field `a` is refering to uninitialized field `b`
show create table t1;
ERROR 42S02: Table 'test.t1' doesn't exist
Refering to other columns
create or replace table t1 (a int default 1, b int default a);
create or replace table t1 (a int default 1, b int as (a));
create or replace table t1 (a int default b, b int default 1);
create or replace table t1 (a int as (b), b int default 1);
create or replace table t1 (a int as (b), b int default 1+1);
create or replace table t1 (a int default 1, b int as (c), c int default (a+1));
create or replace table t1 (a int default 1+1, b int as (c), c int default (a+1));
create or replace table t1 (a VARCHAR(128) DEFAULT @@version);
create or replace table t1 (a int not null, b int as (a));
create or replace table t1 (a int not null, b int default a+1);
create or replace table t1 (a int default a);
ERROR 01000: Expression for field `a` is refering to uninitialized field `a`
create or replace table t1 (a int default b, b int default 1+1);
ERROR 01000: Expression for field `a` is refering to uninitialized field `b`
create or replace table t1 (a int default 1, b int as (c), c int as (a+1));
ERROR 01000: Expression for field `b` is refering to uninitialized field `c`
CREATE TABLE t1 (a INT DEFAULT a);
ERROR 01000: Expression for field `a` is refering to uninitialized field `a`
CREATE TABLE t1 (a INT DEFAULT (DEFAULT(a)));
ERROR HY000: Field 'a' doesn't have a default value
CREATE TABLE t1 (a INT DEFAULT(DEFAULT(b)), b INT DEFAULT(DEFAULT(a)));
ERROR HY000: Field 'b' doesn't have a default value
CREATE TABLE t1 (a INT DEFAULT(DEFAULT(b)) NOT NULL, b INT DEFAULT(DEFAULT(a)) NOT NULL);
ERROR HY000: Field 'b' doesn't have a default value
drop table if exists t1;
Warnings:
Note 1051 Unknown table 'test.t1'
Allow defaults to refer to not default fields
create or replace table t1 (a int as (b), b int not null);
insert into t1 values();
Warnings:
Warning 1364 Field 'b' doesn't have a default value
insert into t1 (a) values(1);
Warnings:
Warning 1364 Field 'b' doesn't have a default value
Warning 1906 The value specified for computed column 'a' in table 't1' ignored
insert into t1 (b) values(2);
insert into t1 (a,b) values(3,4);
Warnings:
Warning 1906 The value specified for computed column 'a' in table 't1' ignored
select * from t1;
a b
0 0
0 0
2 2
4 4
drop table t1;
Error handling
create or replace table t1 (a bigint default xxx());
ERROR HY000: Function or expression '`xxx`' is not allowed for 'DEFAULT' of column/constraint 'a'
create or replace table t1 (a bigint default (select (1)));
ERROR HY000: Function or expression 'subselect' is not allowed for 'DEFAULT' of column/constraint 'a'
create or replace table t1 (a bigint default (1,2,3)));
ERROR 21000: Operand should contain 1 column(s)
drop table if exists t1;
Warnings:
Note 1051 Unknown table 'test.t1'
#
# Invalid DEFAULT expressions
#
CREATE TABLE t1 (a INT DEFAULT (SELECT 1));
ERROR HY000: Function or expression 'subselect' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT (EXISTS (SELECT 1)));
ERROR HY000: Function or expression 'subselect' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT (1=ANY (SELECT 1)));
ERROR HY000: Function or expression 'subselect' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT ROW(1,1));
ERROR 21000: Operand should contain 1 column(s)
CREATE TABLE t1 (a INT DEFAULT (1,1));
ERROR 21000: Operand should contain 1 column(s)
CREATE TABLE t1 (a INT DEFAULT ((1,1)));
ERROR 21000: Operand should contain 1 column(s)
CREATE TABLE t1 (a INT DEFAULT ?);
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?)' at line 1
CREATE TABLE t1 (a INT DEFAULT(?));
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?))' at line 1
CREATE TABLE t1 (a INT DEFAULT (b), b INT DEFAULT(a));
ERROR 01000: Expression for field `a` is refering to uninitialized field `b`
CREATE TABLE t1 (a INT DEFAULT @v);
ERROR HY000: Function or expression 'user_var' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT @v:=1);
ERROR HY000: Function or expression 'user_var' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT(NAME_CONST('xxx', 'yyy'));
ERROR HY000: Function or expression 'name_const' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT COUNT(*));
ERROR HY000: Function or expression 'count(' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT COUNT(1));
ERROR HY000: Function or expression 'count(' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT AVG(1));
ERROR HY000: Function or expression 'avg(' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT MIN(1));
ERROR HY000: Function or expression 'min(' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT GROUP_CONCAT(1));
ERROR HY000: Function or expression 'group_concat' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT ROW_NUMBER() OVER ());
ERROR HY000: Function or expression 'row_number' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE FUNCTION f1() RETURNS INT RETURN 1;
CREATE TABLE t1 (a INT DEFAULT f1());
ERROR HY000: Function or expression '`f1`' is not allowed for 'DEFAULT' of column/constraint 'a'
DROP FUNCTION f1;
CREATE PROCEDURE p1(par INT) CREATE TABLE t1 (a INT DEFAULT par);
ERROR HY000: Function or expression '???' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT DEFAULT par);
ERROR 42S22: Unknown column 'par' in 'virtual column function'
CREATE PROCEDURE p1() CREATE TABLE t1 (a INT DEFAULT par);
CALL p1;
ERROR 42S22: Unknown column 'par' in 'virtual column function'
DROP PROCEDURE p1;
CREATE TABLE t1 (a INT DEFAULT VALUES(a));
ERROR HY000: Function or expression 'values' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1 AFTER INSERT ON t1 FOR EACH ROW CREATE TABLE t2 (a INT DEFAULT NEW.a);
ERROR HY000: Function or expression 'trigger' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TRIGGER tr1 AFTER INSERT ON t1 FOR EACH ROW CREATE TEMPORARY TABLE t2 (a INT DEFAULT NEW.a);
ERROR HY000: Function or expression 'trigger' is not allowed for 'DEFAULT' of column/constraint 'a'
DROP TABLE t1;
#
# Prepared statements
#
PREPARE stmt FROM 'CREATE TABLE t1 (a INT DEFAULT(?))';
set @a=1;
execute stmt using @a;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
set @a=-1;
execute stmt using @a;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT '-1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'CREATE TABLE t1 (a INT DEFAULT(?), b INT DEFAULT(?))';
set @a=1, @b=2;
execute stmt using @a,@b;
show create table t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT '1',
`b` int(11) DEFAULT '2'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
drop table t1;
DEALLOCATE PREPARE stmt;
PREPARE stmt FROM 'CREATE TABLE t1 (a INT DEFAULT(?+?))';
set @a=1;
execute stmt using @a,@a;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?+?))' at line 1
DEALLOCATE PREPARE stmt;
#
# Parenthesized Item_basic_constant
#
CREATE TABLE t1 (
i01 INT DEFAULT (((1))),
i02 INT DEFAULT (((0x3939))),
i03 INT DEFAULT (((1.0))),
i04 INT DEFAULT (((1e0))),
i05 INT DEFAULT (((NULL))),
f01 DOUBLE DEFAULT (((PI()))),
s01 VARCHAR(10) DEFAULT (((_latin1'test'))),
s02 VARCHAR(10) DEFAULT ((('test'))),
s03 VARCHAR(10) DEFAULT (((0x40))),
s04 VARCHAR(10) DEFAULT (((X'40'))),
s05 VARCHAR(10) DEFAULT (((B'1000000'))),
d01 TIME DEFAULT (((TIME'10:20:30'))),
d02 DATE DEFAULT (((DATE'2001-01-01'))),
d03 DATETIME DEFAULT (((TIMESTAMP'2001-01-01 10:20:30')))
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i01` int(11) DEFAULT '1',
`i02` int(11) DEFAULT '14649',
`i03` int(11) DEFAULT '1',
`i04` int(11) DEFAULT '1',
`i05` int(11) DEFAULT NULL,
`f01` double DEFAULT '3.141592653589793',
`s01` varchar(10) DEFAULT 'test',
`s02` varchar(10) DEFAULT 'test',
`s03` varchar(10) DEFAULT '@',
`s04` varchar(10) DEFAULT '@',
`s05` varchar(10) DEFAULT '@',
`d01` time DEFAULT '10:20:30',
`d02` date DEFAULT '2001-01-01',
`d03` datetime DEFAULT '2001-01-01 10:20:30'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
i01 1
i02 14649
i03 1
i04 1
i05 NULL
f01 3.141592653589793
s01 test
s02 test
s03 @
s04 @
s05 @
d01 10:20:30
d02 2001-01-01
d03 2001-01-01 10:20:30
DROP TABLE t1;
#
# COALESCE(Item_basic_constant)
#
CREATE TABLE t1 (
i01 INT DEFAULT COALESCE(1),
i02 INT DEFAULT COALESCE(0x3939),
i03 INT DEFAULT COALESCE(1.0),
i04 INT DEFAULT COALESCE(1e0),
i05 INT DEFAULT COALESCE(NULL),
f01 DOUBLE DEFAULT COALESCE(PI()),
s01 VARCHAR(10) DEFAULT COALESCE(_latin1'test'),
s02 VARCHAR(10) DEFAULT COALESCE('test'),
s03 VARCHAR(10) DEFAULT COALESCE(0x40),
s04 VARCHAR(10) DEFAULT COALESCE(X'40'),
s05 VARCHAR(10) DEFAULT COALESCE(B'1000000'),
d01 TIME DEFAULT COALESCE(TIME'10:20:30'),
d02 DATE DEFAULT COALESCE(DATE'2001-01-01'),
d03 DATETIME DEFAULT COALESCE(TIMESTAMP'2001-01-01 10:20:30')
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`i01` int(11) DEFAULT COALESCE(1),
`i02` int(11) DEFAULT COALESCE(0x3939),
`i03` int(11) DEFAULT COALESCE(1.0),
`i04` int(11) DEFAULT COALESCE(1e0),
`i05` int(11) DEFAULT COALESCE(NULL),
`f01` double DEFAULT COALESCE(PI()),
`s01` varchar(10) DEFAULT COALESCE(_latin1'test'),
`s02` varchar(10) DEFAULT COALESCE('test'),
`s03` varchar(10) DEFAULT COALESCE(0x40),
`s04` varchar(10) DEFAULT COALESCE(X'40'),
`s05` varchar(10) DEFAULT COALESCE(B'1000000'),
`d01` time DEFAULT COALESCE(TIME'10:20:30'),
`d02` date DEFAULT COALESCE(DATE'2001-01-01'),
`d03` datetime DEFAULT COALESCE(TIMESTAMP'2001-01-01 10:20:30')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
i01 1
i02 99
i03 1
i04 1
i05 NULL
f01 3.141592653589793
s01 test
s02 test
s03 @
s04 @
s05 @
d01 10:20:30
d02 2001-01-01
d03 2001-01-01 10:20:30
DROP TABLE t1;
#
# TINYINT: out of range
#
CREATE TABLE t1 (a TINYINT DEFAULT 300 NOT NULL);
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a TINYINT DEFAULT 128 NOT NULL);
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a TINYINT DEFAULT -500 NOT NULL);
ERROR 42000: Invalid default value for 'a'
#
# INT: simple numeric expressions
#
CREATE TABLE t1 (a INT DEFAULT 1 NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT COALESCE(1) NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL DEFAULT COALESCE(1)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
1
DROP TABLE t1;
#
# INT: simple string expressions
#
CREATE TABLE t1 (a INT DEFAULT '1' NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT CONCAT('1') NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL DEFAULT CONCAT('1')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT COALESCE('1') NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL DEFAULT COALESCE('1')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
1
DROP TABLE t1;
#
# INT: string expressions with garbage
#
CREATE TABLE t1 (a INT DEFAULT 'x');
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a INT DEFAULT CONCAT('x'));
insert into t1 values();
Warnings:
Warning 1366 Incorrect integer value: 'x' for column 'a' at row 1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT COALESCE('x'));
insert into t1 values();
Warnings:
Warning 1366 Incorrect integer value: 'x' for column 'a' at row 1
DROP TABLE t1;
#
# INT: string expressions with numbers + garbage
#
CREATE TABLE t1 (a INT DEFAULT '1x');
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a INT DEFAULT COALESCE('1x'));
insert into t1 values();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT CONCAT('1x'));
insert into t1 values();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
DROP TABLE t1;
#
# INT: string expressions with numbers + trailing space
#
CREATE TABLE t1 (a INT DEFAULT '1 ');
Warnings:
Note 1265 Data truncated for column 'a' at row 1
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT CONCAT('1 '));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT CONCAT('1 ')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
Warnings:
Note 1265 Data truncated for column 'a' at row 1
SELECT * FROM t1;
a
1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT COALESCE('1 '));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT COALESCE('1 ')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
Warnings:
Note 1265 Data truncated for column 'a' at row 1
SELECT * FROM t1;
a
1
DROP TABLE t1;
#
# INT: a HEX value
#
CREATE TABLE t1 (a INT DEFAULT 0x61 NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) NOT NULL DEFAULT '97'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
97
DROP TABLE t1;
#
# VARCHAR: good defaults
#
CREATE TABLE t1 (a VARCHAR(30) DEFAULT 'xxx' NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` varchar(30) NOT NULL DEFAULT 'xxx'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
xxx
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(2) DEFAULT 0x41 NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` varchar(2) NOT NULL DEFAULT 'A'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(2) DEFAULT CONCAT(0x41) NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` varchar(2) NOT NULL DEFAULT CONCAT(0x41)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
A
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(2) DEFAULT COALESCE(0x41) NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` varchar(2) NOT NULL DEFAULT COALESCE(0x41)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
A
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(2) DEFAULT CONCAT(_utf8 0x41) NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` varchar(2) NOT NULL DEFAULT CONCAT(_utf8 0x41)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
A
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(2) DEFAULT CONCAT(_utf8 X'41') NOT NULL);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` varchar(2) NOT NULL DEFAULT CONCAT(_utf8 X'41')
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT);
SELECT * FROM t1;
a
A
DROP TABLE t1;
#
# VARCHAR: Too long default
#
CREATE TABLE t1 (a VARCHAR(2) DEFAULT 'xxx' NOT NULL);
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a VARCHAR(2) DEFAULT CONCAT('xxx') NOT NULL);
insert into t1 values();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
DROP TABLE t1;
#
# VARCHAR: Too long default with non-important data
#
CREATE TABLE t1 (a VARCHAR(2) DEFAULT 'xx ' NOT NULL);
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a VARCHAR(2) DEFAULT CONCAT('xx ') NOT NULL);
insert into t1 values();
Warnings:
Note 1265 Data truncated for column 'a' at row 1
DROP TABLE t1;
#
# VARCHAR: conversion failures
#
CREATE TABLE t1 (a VARCHAR(2) CHARACTER SET latin1 DEFAULT _utf8 X'D18F' NOT NULL);
ERROR 42000: Invalid default value for 'a'
CREATE TABLE t1 (a VARCHAR(2) CHARACTER SET latin1 DEFAULT CONCAT(_utf8 X'D18F') NOT NULL);
insert into t1 values();
Warnings:
Warning 1366 Incorrect string value: '\xD1\x8F' for column 'a' at row 1
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(2) CHARACTER SET latin1 DEFAULT CONCAT(_utf8 0xD18F) NOT NULL);
insert into t1 values();
Warnings:
Warning 1366 Incorrect string value: '\xD1\x8F' for column 'a' at row 1
DROP TABLE t1;
#
# Field as a default value
#
CREATE TABLE t1 (a INT, b INT DEFAULT (a));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT (a)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (1, 1);
INSERT INTO t1 VALUES (DEFAULT, DEFAULT);
INSERT INTO t1 VALUES (1, DEFAULT);
INSERT INTO t1 VALUES (DEFAULT, 1);
SELECT * FROM t1;
a b
1 1
NULL NULL
1 1
NULL 1
DROP TABLE t1;
#
# Function DEFAULT(field)
#
CREATE TABLE t1 (a INT DEFAULT(DEFAULT(b)), b INT DEFAULT 1);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT (DEFAULT(b)),
`b` int(11) DEFAULT '1'
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT, DEFAULT);
SELECT * FROM t1;
a b
1 1
DROP TABLE t1;
CREATE TABLE t1 (a INT DEFAULT 1, b INT DEFAULT(DEFAULT(a)));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` int(11) DEFAULT '1',
`b` int(11) DEFAULT (DEFAULT(a))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES (DEFAULT, DEFAULT);
SELECT * FROM t1;
a b
1 1
DROP TABLE t1;
#
# SQL Standard <datetime value function> as a <default option>
#
CREATE TABLE t1 (a DATETIME DEFAULT CURRENT_TIMESTAMP);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` datetime DEFAULT CURRENT_TIMESTAMP
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
CREATE TABLE t1 (a TIME DEFAULT CURRENT_TIME);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` time DEFAULT CURRENT_TIME
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
CREATE TABLE t1 (a DATE DEFAULT CURRENT_DATE);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` date DEFAULT CURRENT_DATE
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
#
# DECIMAL + CURRENT_TIMESTAMP, no truncation
#
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.123456');
CREATE TABLE t1 (a DECIMAL(30,6) DEFAULT CURRENT_TIMESTAMP(6));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` decimal(30,6) DEFAULT CURRENT_TIMESTAMP(6)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
a
20010101102030.123456
DROP TABLE t1;
CREATE TABLE t1 (a DECIMAL(30,6) DEFAULT COALESCE(CURRENT_TIMESTAMP(6)));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` decimal(30,6) DEFAULT COALESCE(CURRENT_TIMESTAMP(6))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
INSERT IGNORE INTO t1 VALUES();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
SET sql_mode = 'STRICT_ALL_TABLES';
INSERT INTO t1 VALUES();
ERROR 01000: Data truncated for column 'a' at row 1
SET sql_mode = DEFAULT;
DROP TABLE t1;
SET timestamp=DEFAULT;
#
# DECIMAL + CURRENT_TIME, no truncation
#
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.123456');
CREATE TABLE t1 (a DECIMAL(30,6) DEFAULT COALESCE(CURRENT_TIME(6)));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` decimal(30,6) DEFAULT COALESCE(CURRENT_TIME(6))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
DROP TABLE t1;
SET timestamp=DEFAULT;
#
# DECIMAL + CURRENT_DATE, no truncation
#
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.123456');
CREATE TABLE t1 (a DECIMAL(30,6) DEFAULT COALESCE(CURRENT_DATE));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` decimal(30,6) DEFAULT COALESCE(CURRENT_DATE)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES();
Warnings:
Warning 1265 Data truncated for column 'a' at row 1
DROP TABLE t1;
SET timestamp=DEFAULT;
#
# COALESCE for SQL Standard <datetime value function>
#
CREATE TABLE t1 (a TIMESTAMP DEFAULT COALESCE(CURRENT_TIMESTAMP));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` timestamp NOT NULL DEFAULT COALESCE(CURRENT_TIMESTAMP)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
CREATE TABLE t1 (a DATE DEFAULT COALESCE(CURRENT_DATE));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` date DEFAULT COALESCE(CURRENT_DATE)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
CREATE TABLE t1 (a TIME DEFAULT COALESCE(CURRENT_TIME));
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` time DEFAULT COALESCE(CURRENT_TIME)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
DROP TABLE t1;
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.123456');
CREATE TABLE t1 (
a TIMESTAMP DEFAULT CURRENT_TIMESTAMP(6),
b TIMESTAMP DEFAULT COALESCE(CURRENT_TIMESTAMP(6))
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`b` timestamp NOT NULL DEFAULT COALESCE(CURRENT_TIMESTAMP(6))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES ();
SELECT CURRENT_TIMESTAMP(6);
CURRENT_TIMESTAMP(6)
2001-01-01 10:20:30.123456
SELECT * FROM t1;
a b
2001-01-01 10:20:30 2001-01-01 10:20:30
DROP TABLE t1;
SET timestamp=DEFAULT;
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.123456');
CREATE TABLE t1 (
a DECIMAL(30,0) DEFAULT CURRENT_TIMESTAMP(6),
b DECIMAL(30,0) DEFAULT COALESCE(CURRENT_TIMESTAMP(6))
);
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`a` decimal(30,0) DEFAULT CURRENT_TIMESTAMP(6),
`b` decimal(30,0) DEFAULT COALESCE(CURRENT_TIMESTAMP(6))
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES ();
Warnings:
Note 1265 Data truncated for column 'a' at row 1
Warning 1265 Data truncated for column 'b' at row 1
SELECT * FROM t1;
a b
20010101102030 2001
DROP TABLE t1;
#
# Miscelaneous SQL standard <default option> variants
#
CREATE TABLE t1 (a VARCHAR(30) DEFAULT CURRENT_USER);
ERROR HY000: Function or expression 'current_user()' is not allowed for 'DEFAULT' of column/constraint 'a'
CREATE TABLE t1 (a VARCHAR(30) DEFAULT CURRENT_ROLE);
ERROR HY000: Function or expression 'current_role()' is not allowed for 'DEFAULT' of column/constraint 'a'
#
# Other Item_func_sysconst derived functions
#
CREATE TABLE t1 (a VARCHAR(30) DEFAULT DATABASE());
INSERT INTO t1 VALUES ();
USE INFORMATION_SCHEMA;
INSERT INTO test.t1 VALUES ();
USE test;
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
a
test
information_schema
test
DROP TABLE t1;
#
# Check DEFAULT() function
#
CREATE TABLE `t1` (`a` int(11) DEFAULT 3+3,`b` int(11) DEFAULT '1000');
insert into t1 values (1,1),(2,2);
insert into t1 values (default,default);
select * from t1;
a b
1 1
2 2
6 1000
select default(a),b from t1;
ERROR HY000: Field 'a' doesn't have a default value
select a,default(b) from t1;
a default(b)
1 1000
2 1000
6 1000
drop table t1;
#
# Real functions
#
CREATE TABLE t1 (a DECIMAL(10,1), b DOUBLE DEFAULT CAST(a AS DOUBLE));
INSERT INTO t1 VALUES (10.1, DEFAULT);
SELECT * FROM t1;
a b
10.1 10.1
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE, b DOUBLE DEFAULT EXP(a), c DOUBLE DEFAULT LOG(b), d DOUBLE DEFAULT LOG(4, b));
INSERT INTO t1 VALUES (2, DEFAULT, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c d
2 7.38905609893065 2 1.4426950408889634
DROP TABLE t1;
CREATE TABLE t1 (a INT, b DOUBLE DEFAULT LOG2(a), c DOUBLE DEFAULT LOG10(a));
INSERT INTO t1 VALUES (4, DEFAULT, DEFAULT);
INSERT INTO t1 VALUES (100, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c
4 2 0.6020599913279624
100 6.643856189774725 2
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE, b DOUBLE DEFAULT SQRT(a), c DOUBLE DEFAULT POW(a,3));
INSERT INTO t1 VALUES (4, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c
4 2 64
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE, b DOUBLE DEFAULT ACOS(a), c DOUBLE DEFAULT ASIN(a), d DOUBLE DEFAULT ATAN(a));
INSERT INTO t1 VALUES (1, DEFAULT, DEFAULT, DEFAULT);
SELECT a, b/PI(), c/PI(), d/PI() FROM t1;
a b/PI() c/PI() d/PI()
1 0 0.5 0.25
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE, b DOUBLE DEFAULT COS(a), c DOUBLE DEFAULT SIN(a), d DOUBLE DEFAULT TAN(a), e DOUBLE DEFAULT COT(a));
INSERT INTO t1 (a) VALUES (PI()/3);
SELECT ROUND(a,3), ROUND(b,3), ROUND(c,3), ROUND(d,3), ROUND(e,3) FROM t1;
ROUND(a,3) ROUND(b,3) ROUND(c,3) ROUND(d,3) ROUND(e,3)
1.047 0.500 0.866 1.732 0.577
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE DEFAULT RAND());
INSERT INTO t1 VALUES (DEFAULT);
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE, b DOUBLE DEFAULT DEGREES(a), c DOUBLE DEFAULT RADIANS(b));
INSERT INTO t1 VALUES (PI(), DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c
3.141592653589793 180 3.141592653589793
DROP TABLE t1;
#
# INT result functions
#
CREATE TABLE t1 (a INT, b INT DEFAULT INTERVAL(a, 10, 20, 30, 40));
INSERT INTO t1 (a) VALUES (34);
SELECT * FROM t1;
a b
34 3
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a DIV b);
INSERT INTO t1 (a, b) VALUES (13, 3);
SELECT * FROM t1;
a b c
13 3 4
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT SIGN(a));
INSERT INTO t1 (a) VALUES (-10),(0), (10);
SELECT * FROM t1;
a b
-10 -1
0 0
10 1
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(30), b INT DEFAULT FIELD(a, 'Hej', 'ej', 'Heja', 'hej', 'foo'));
INSERT INTO t1 (a) VALUES ('ej');
SELECT * FROM t1;
a b
ej 2
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(30), b INT DEFAULT FIND_IN_SET(a, 'Hej,ej,Heja,hej,foo'));
INSERT INTO t1 (a) VALUES ('ej');
SELECT * FROM t1;
a b
ej 2
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(30), b INT DEFAULT ASCII(a), c INT DEFAULT ORD(a));
INSERT INTO t1 (a) VALUES ('a');
SELECT * FROM t1;
a b c
a 97 97
DROP TABLE t1;
#
# Temporal functions
#
# Item_temporal_hybrid_func
CREATE TABLE t1 (a DATE, b INT, c DATE DEFAULT DATE_ADD(a, INTERVAL b DAY));
INSERT INTO t1 VALUES ('2001-01-01', 30, DEFAULT);
SELECT * FROM t1;
a b c
2001-01-01 30 2001-01-31
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b TIME, c DATETIME DEFAULT ADDTIME(a, b));
INSERT INTO t1 VALUES ('2001-01-01', '10:20:30', DEFAULT);
SELECT * FROM t1;
a b c
2001-01-01 10:20:30 2001-01-01 10:20:30
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(32), b VARCHAR(32), c DATE DEFAULT STR_TO_DATE(a,b));
INSERT INTO t1 VALUES ('01,5,2013','%d,%m,%Y', DEFAULT);
SELECT * FROM t1;
a b c
01,5,2013 %d,%m,%Y 2013-05-01
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(30), b VARCHAR(30) DEFAULT DATE_FORMAT(a,'%W %M %Y'));
ERROR HY000: Function or expression 'date_format' is not allowed for 'DEFAULT' of column/constraint 'b'
# Item_datefunc
SET time_zone='-10:00';
SET timestamp=UNIX_TIMESTAMP('2001-01-01 23:59:59');
CREATE TABLE t1 (a DATE DEFAULT CURDATE(), b DATE DEFAULT UTC_DATE());
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
a b
2001-01-01 2001-01-02
DROP TABLE t1;
SET time_zone=DEFAULT, timestamp= DEFAULT;
CREATE TABLE t1 (a INT, b DATE DEFAULT FROM_DAYS(a));
INSERT INTO t1 VALUES (730669, DEFAULT);
SELECT * FROM t1;
a b
730669 2000-07-03
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b DATE DEFAULT LAST_DAY(a));
INSERT INTO t1 VALUES ('2003-02-05', DEFAULT);
SELECT * FROM t1;
a b
2003-02-05 2003-02-28
DROP TABLE t1;
CREATE TABLE t1 (yy INT, yd INT, d DATE DEFAULT MAKEDATE(yy, yd));
INSERT INTO t1 VALUES (2011,32,DEFAULT);
SELECT * FROM t1;
yy yd d
2011 32 2011-02-01
DROP TABLE t1;
# Item_timefunc
SET time_zone='-10:00';
SET timestamp=UNIX_TIMESTAMP('2001-01-01 23:59:59');
CREATE TABLE t1 (a TIME DEFAULT CURTIME(), b TIME DEFAULT UTC_TIME());
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
a b
23:59:59 09:59:59
DROP TABLE t1;
SET time_zone=DEFAULT, timestamp= DEFAULT;
CREATE TABLE t1 (a INT, b TIME DEFAULT SEC_TO_TIME(a));
INSERT INTO t1 VALUES (2378, DEFAULT);
SELECT * FROM t1;
a b
2378 00:39:38
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME, b DATETIME, c TIME DEFAULT TIMEDIFF(a,b));
INSERT INTO t1 VALUES ('2000:01:01 00:00:00', '2000:01:02 10:20:30', DEFAULT);
SELECT * FROM t1;
a b c
2000-01-01 00:00:00 2000-01-02 10:20:30 -34:20:30
DROP TABLE t1;
CREATE TABLE t1 (hh INT, mm INT, ss INT, t TIME DEFAULT MAKETIME(hh,mm,ss));
INSERT INTO t1 VALUES (10,20,30,DEFAULT);
SELECT * FROM t1;
hh mm ss t
10 20 30 10:20:30
DROP TABLE t1;
# Item_datetimefunc
SET time_zone='-10:00';
SET timestamp=UNIX_TIMESTAMP('2001-01-01 23:59:59');
CREATE TABLE t1 (a TIMESTAMP DEFAULT NOW(), b TIMESTAMP DEFAULT UTC_TIMESTAMP());
INSERT INTO t1 VALUES ();
SELECT * FROM t1;
a b
2001-01-01 23:59:59 2001-01-02 09:59:59
DROP TABLE t1;
SET time_zone=DEFAULT, timestamp= DEFAULT;
CREATE TABLE t1 (a TIMESTAMP(6) DEFAULT SYSDATE(6), s INT, b TIMESTAMP(6) DEFAULT SYSDATE(6));
INSERT INTO t1 VALUES (DEFAULT, SLEEP(0.1), DEFAULT);
SELECT b>a FROM t1;
b>a
1
DROP TABLE t1;
SET time_zone='+00:00';
CREATE TABLE t1 (a INT, b TIMESTAMP DEFAULT FROM_UNIXTIME(a));
INSERT INTO t1 VALUES (1447430881, DEFAULT);
SELECT * FROM t1;
a b
1447430881 2015-11-13 16:08:01
DROP TABLE t1;
SET time_zone=DEFAULT;
CREATE TABLE t1 (a TIMESTAMP, b TIMESTAMP DEFAULT CONVERT_TZ(a, '-10:00', '+10:00'));
INSERT INTO t1 VALUES ('2001-01-01 10:20:30', DEFAULT);
SELECT * FROM t1;
a b
2001-01-01 10:20:30 2001-01-02 06:20:30
DROP TABLE t1;
# Item_temporal_typecast
CREATE TABLE t1 (a INT, b DATE DEFAULT CAST(a AS DATE));
INSERT INTO t1 VALUES (20010203, DEFAULT);
SELECT * FROM t1;
a b
20010203 2001-02-03
DROP TABLE t1;
CREATE TABLE t1 (a INT, b TIME DEFAULT CAST(a AS TIME));
INSERT INTO t1 VALUES (102030, DEFAULT);
SELECT * FROM t1;
a b
102030 10:20:30
DROP TABLE t1;
CREATE TABLE t1 (a BIGINT, b DATETIME DEFAULT CAST(a AS DATETIME));
INSERT INTO t1 VALUES (20010203102030, DEFAULT);
SELECT * FROM t1;
a b
20010203102030 2001-02-03 10:20:30
DROP TABLE t1;
#
# Functions with temporal input
#
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT PERIOD_ADD(a,b));
INSERT INTO t1 (a,b) VALUES (200801, 2);
SELECT * FROM t1;
a b c
200801 2 200803
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT PERIOD_DIFF(a,b));
INSERT INTO t1 (a,b) VALUES (200802, 200703);
SELECT * FROM t1;
a b c
200802 200703 11
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT TO_DAYS(a));
INSERT INTO t1 (a) VALUES (950501);
SELECT * FROM t1;
a b
950501 728779
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b INT DEFAULT TO_DAYS(a));
INSERT INTO t1 (a) VALUES ('2007-10-07');
SELECT * FROM t1;
a b
2007-10-07 733321
DROP TABLE t1;
CREATE TABLE t1 (a INT, b BIGINT DEFAULT TO_SECONDS(a));
INSERT INTO t1 (a) VALUES (950501);
SELECT * FROM t1;
a b
950501 62966505600
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b BIGINT DEFAULT TO_SECONDS(a));
INSERT INTO t1 (a) VALUES ('2009-11-29');
SELECT * FROM t1;
a b
2009-11-29 63426672000
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME, b BIGINT DEFAULT TO_SECONDS(a));
INSERT INTO t1 (a) VALUES ('2009-11-29 13:43:32');
SELECT * FROM t1;
a b
2009-11-29 13:43:32 63426721412
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b BIGINT DEFAULT DAYOFMONTH(a));
INSERT INTO t1 (a) VALUES ('2007-02-03');
SELECT * FROM t1;
a b
2007-02-03 3
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b BIGINT DEFAULT DAYOFWEEK(a));
INSERT INTO t1 (a) VALUES ('2007-02-03');
SELECT * FROM t1;
a b
2007-02-03 7
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b BIGINT DEFAULT DAYOFYEAR(a));
INSERT INTO t1 (a) VALUES ('2007-02-03');
SELECT * FROM t1;
a b
2007-02-03 34
DROP TABLE t1;
CREATE TABLE t1 (a TIME, b INT DEFAULT HOUR(a));
INSERT INTO t1 (a) VALUES ('10:05:03');
SELECT * FROM t1;
a b
10:05:03 10
DROP TABLE t1;
CREATE TABLE t1 (a TIME, b INT DEFAULT MINUTE(a));
INSERT INTO t1 (a) VALUES ('10:05:03');
SELECT * FROM t1;
a b
10:05:03 5
DROP TABLE t1;
CREATE TABLE t1 (a TIME, b INT DEFAULT SECOND(a));
INSERT INTO t1 (a) VALUES ('10:05:03');
SELECT * FROM t1;
a b
10:05:03 3
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME(6), b INT DEFAULT MICROSECOND(a));
INSERT INTO t1 (a) VALUES ('2009-12-31 23:59:59.000010');
SELECT * FROM t1;
a b
2009-12-31 23:59:59.000010 10
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b INT DEFAULT YEAR(a));
INSERT INTO t1 (a) VALUES ('1987-01-01');
SELECT * FROM t1;
a b
1987-01-01 1987
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b INT DEFAULT MONTH(a));
INSERT INTO t1 (a) VALUES ('1987-01-01');
SELECT * FROM t1;
a b
1987-01-01 1
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b INT DEFAULT WEEK(a));
INSERT INTO t1 (a) VALUES ('1987-02-01');
SELECT * FROM t1;
a b
1987-02-01 5
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b INT DEFAULT YEARWEEK(a));
INSERT INTO t1 (a) VALUES ('2000-01-01');
SELECT * FROM t1;
a b
2000-01-01 199952
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b INT DEFAULT QUARTER(a));
INSERT INTO t1 (a) VALUES ('2008-04-01');
SELECT * FROM t1;
a b
2008-04-01 2
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b VARCHAR(30) DEFAULT DAYNAME(a));
ERROR HY000: Function or expression 'dayname' is not allowed for 'DEFAULT' of column/constraint 'b'
CREATE TABLE t1 (a DATE, b VARCHAR(30) DEFAULT MONTHNAME(a));
ERROR HY000: Function or expression 'monthname' is not allowed for 'DEFAULT' of column/constraint 'b'
CREATE TABLE t1 (a DATE, b INT DEFAULT EXTRACT(YEAR FROM a));
INSERT INTO t1 (a) VALUES ('2009-07-02');
SELECT * FROM t1;
a b
2009-07-02 2009
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME, b INT DEFAULT EXTRACT(YEAR_MONTH FROM a));
INSERT INTO t1 (a) VALUES ('2009-07-02 01:02:03');
SELECT * FROM t1;
a b
2009-07-02 01:02:03 200907
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME, b INT DEFAULT EXTRACT(DAY_MINUTE FROM a));
INSERT INTO t1 (a) VALUES ('2009-07-02 01:02:03');
SELECT * FROM t1;
a b
2009-07-02 01:02:03 20102
DROP TABLE t1;
CREATE TABLE t1 (a DATETIME(6), b INT DEFAULT EXTRACT(MICROSECOND FROM a));
INSERT INTO t1 (a) VALUES ('2009-07-02 01:02:03.000123');
SELECT * FROM t1;
a b
2009-07-02 01:02:03.000123 123
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b DATE, c INT DEFAULT TIMESTAMPDIFF(MONTH,a,b));
INSERT INTO t1 (a,b) VALUES ('2003-02-01','2003-05-01');
SELECT * FROM t1;
a b c
2003-02-01 2003-05-01 3
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b DATE, c INT DEFAULT TIMESTAMPDIFF(YEAR,a,b));
INSERT INTO t1 (a,b) VALUES ('2002-05-01','2001-01-01');
SELECT * FROM t1;
a b c
2002-05-01 2001-01-01 -1
DROP TABLE t1;
CREATE TABLE t1 (a DATE, b DATETIME, c INT DEFAULT TIMESTAMPDIFF(MINUTE,a,b));
INSERT INTO t1 (a,b) VALUES ('2003-02-01','2003-05-01 12:05:55');
SELECT * FROM t1;
a b c
2003-02-01 2003-05-01 12:05:55 128885
DROP TABLE t1;
#
# Hybrid type functions
#
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT COALESCE(a,b));
INSERT INTO t1 VALUES (NULL, 1, DEFAULT);
SELECT * FROM t1;
a b c
NULL 1 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT IFNULL(a,b));
INSERT INTO t1 VALUES (NULL, 2, DEFAULT);
INSERT INTO t1 VALUES (1, 2, DEFAULT);
SELECT * FROM t1;
a b c
NULL 2 2
1 2 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT NULLIF(a,b));
INSERT INTO t1 VALUES (1, 1, DEFAULT);
INSERT INTO t1 VALUES (1, 2, DEFAULT);
SELECT * FROM t1;
a b c
1 1 NULL
1 2 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT IF(a,b,2));
INSERT INTO t1 VALUES (0, 1, DEFAULT);
INSERT INTO t1 VALUES (1, 1, DEFAULT);
SELECT * FROM t1;
a b c
0 1 2
1 1 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT CASE WHEN a THEN b ELSE 2 END);
INSERT INTO t1 VALUES (0, 1, DEFAULT);
INSERT INTO t1 VALUES (1, 1, DEFAULT);
SELECT * FROM t1;
a b c
0 1 2
1 1 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT -a);
INSERT INTO t1 VALUES (10, DEFAULT);
SELECT * FROM t1;
a b
10 -10
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT ABS(a));
INSERT INTO t1 VALUES (-10, DEFAULT);
SELECT * FROM t1;
a b
-10 10
DROP TABLE t1;
CREATE TABLE t1 (a DOUBLE, b INT DEFAULT CEILING(a), c INT DEFAULT FLOOR(a), d INT DEFAULT ROUND(a));
INSERT INTO t1 VALUES (1.5, DEFAULT, DEFAULT, DEFAULT);
INSERT INTO t1 VALUES (-1.5, DEFAULT, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c d
1.5 2 1 2
-1.5 -1 -2 -2
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a+b, d INT DEFAULT a-b);
INSERT INTO t1 VALUES (2, 1, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c d
2 1 3 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a*b, d INT DEFAULT a/b, e INT DEFAULT a MOD b);
INSERT INTO t1 VALUES (7, 3, DEFAULT, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c d e
7 3 21 2 1
DROP TABLE t1;
SET time_zone='+00:00';
CREATE TABLE t1 (a DATETIME, b INT DEFAULT UNIX_TIMESTAMP(a));
INSERT INTO t1 VALUES ('2001-01-01 10:20:30', DEFAULT);
SELECT * FROM t1;
a b
2001-01-01 10:20:30 978344430
DROP TABLE t1;
SET time_zone=DEFAULT;
CREATE TABLE t1 (a TIME, b INT DEFAULT TIME_TO_SEC(a));
INSERT INTO t1 VALUES ('22:23:00', DEFAULT);
SELECT * FROM t1;
a b
22:23:00 80580
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT LEAST(a,b), d INT DEFAULT GREATEST(a,b));
INSERT INTO t1 VALUES (0, 1, DEFAULT, DEFAULT);
INSERT INTO t1 VALUES (1, 1, DEFAULT, DEFAULT);
SELECT * FROM t1;
a b c d
0 1 0 1
1 1 1 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT LAST_VALUE(a,b));
INSERT INTO t1 VALUES (1, 2, DEFAULT);
SELECT * FROM t1;
a b c
1 2 2
DROP TABLE t1;
#
# CAST
#
CREATE TABLE t1 (a VARCHAR(30), b DECIMAL(10,6) DEFAULT CAST(a AS DECIMAL(10,1)));
INSERT INTO t1 (a) VALUES ('123.456');
SELECT * FROM t1;
a b
123.456 123.500000
DROP TABLE t1;
CREATE TABLE t1 (a DECIMAL(10,3),
b VARCHAR(10) DEFAULT CAST(a AS CHAR(10)),
c VARCHAR(10) DEFAULT CAST(a AS CHAR(4)));
INSERT INTO t1 (a) VALUES (123.456);
Warnings:
Warning 1292 Truncated incorrect CHAR(4) value: '123.456'
SELECT * FROM t1;
a b c
123.456 123.456 123.
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT UNSIGNED DEFAULT CAST(a AS UNSIGNED));
INSERT INTO t1 (a) VALUES (-1);
Warnings:
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1264 Out of range value for column 'b' at row 1
SELECT * FROM t1;
a b
-1 4294967295
DROP TABLE t1;
CREATE TABLE t1 (a BIGINT UNSIGNED, b BIGINT SIGNED DEFAULT CAST(a AS SIGNED));
INSERT INTO t1 (a) VALUES (0xFFFFFFFFFFFFFFFF);
SELECT * FROM t1;
a b
18446744073709551615 -1
DROP TABLE t1;
CREATE TABLE t1 (
a VARCHAR(10) CHARACTER SET latin1,
b VARCHAR(10) CHARACTER SET latin1 DEFAULT a COLLATE latin1_bin,
c VARCHAR(10) CHARACTER SET utf8 DEFAULT CONVERT(a USING utf8),
d VARBINARY(10) DEFAULT BINARY(a)
);
INSERT INTO t1 (a) VALUES ('a');
SELECT * FROM t1;
a b c d
a a a a
DROP TABLE t1;
#
# Bit functions
#
CREATE TABLE t1 (a INT, b INT DEFAULT BIT_COUNT(a));
INSERT INTO t1 (a) VALUES (7);
SELECT * FROM t1;
a b
7 3
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a|b);
INSERT INTO t1 (a,b) VALUES (1,2);
SELECT * FROM t1;
a b c
1 2 3
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a&b);
INSERT INTO t1 (a,b) VALUES (5,4);
SELECT * FROM t1;
a b c
5 4 4
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a^b);
INSERT INTO t1 (a,b) VALUES (11,3);
SELECT * FROM t1;
a b c
11 3 8
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a&~b);
INSERT INTO t1 (a,b) VALUES (5,1);
SELECT * FROM t1;
a b c
5 1 4
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, c INT DEFAULT a<<b, d INT DEFAULT a>>b);
INSERT INTO t1 (a,b) VALUES (5,1);
SELECT * FROM t1;
a b c d
5 1 10 2
DROP TABLE t1;
#
# String functions
#
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(20) DEFAULT REVERSE(a));
INSERT INTO t1 (a) VALUES ('abcd');
SELECT * FROM t1;
a b
abcd dcba
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT UPPER(a), c VARCHAR(10) DEFAULT LOWER(a));
INSERT INTO t1 (a) VALUES ('ABcd');
SELECT * FROM t1;
a b c
ABcd ABCD abcd
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT LEFT(a,1), c VARCHAR(10) DEFAULT RIGHT(a,1), d VARCHAR(10) DEFAULT SUBSTR(a,2,2));
INSERT INTO t1 (a) VALUES ('abcd');
SELECT * FROM t1;
a b c d
abcd a d bc
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(20), b VARCHAR(20) DEFAULT SUBSTRING_INDEX(a,'.',2));
INSERT INTO t1 (a) VALUES ('www.mariadb.org');
SELECT * FROM t1;
a b
www.mariadb.org www.mariadb
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10), c VARCHAR(20) DEFAULT CONCAT(a,b));
INSERT INTO t1 (a,b) VALUES ('a','b');
SELECT * FROM t1;
a b c
a b ab
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10), c VARCHAR(20) DEFAULT CONCAT_WS(',',a,b));
INSERT INTO t1 (a,b) VALUES ('a','b');
SELECT * FROM t1;
a b c
a b a,b
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT REPLACE(a,'a','A'));
INSERT INTO t1 (a) VALUES ('abc');
SELECT * FROM t1;
a b
abc Abc
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT REGEXP_REPLACE(a,'[0-9]','.'));
INSERT INTO t1 (a) VALUES ('a1b2c');
SELECT * FROM t1;
a b
a1b2c a.b.c
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT REGEXP_SUBSTR(a,'[0-9]+'));
INSERT INTO t1 (a) VALUES ('ab12cd');
SELECT * FROM t1;
a b
ab12cd 12
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(20), b VARCHAR(20) DEFAULT SOUNDEX(a));
INSERT INTO t1 (a) VALUES ('tester');
SELECT * FROM t1;
a b
tester T236
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(20), b VARCHAR(20) DEFAULT QUOTE(a));
INSERT INTO t1 (a) VALUES ('a\'b');
SELECT * FROM t1;
a b
a'b 'a\'b'
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT LPAD(a,10,'.'), c VARCHAR(10) DEFAULT RPAD(a,10,'.'));
INSERT INTO t1 (a) VALUES ('ab');
SELECT * FROM t1;
a b c
ab ........ab ab........
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT LTRIM(a), c VARCHAR(10) DEFAULT RTRIM(a));
INSERT INTO t1 (a) VALUES (' ab ');
SELECT a, HEX(b), HEX(c) FROM t1;
a HEX(b) HEX(c)
ab 616220 206162
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b VARCHAR(10) DEFAULT TRIM(BOTH 'a' FROM a));
INSERT INTO t1 (a) VALUES ('abba');
SELECT a, b FROM t1;
a b
abba bb
DROP TABLE t1;
CREATE TABLE t1 (a INT, b VARCHAR(10) DEFAULT SPACE(a));
INSERT INTO t1 (a) VALUES (3);
SELECT a, HEX(b) FROM t1;
a HEX(b)
3 202020
DROP TABLE t1;
CREATE TABLE t1 (a INT, b VARCHAR(10), c VARCHAR(10) DEFAULT REPEAT(b,a));
INSERT INTO t1 (a,b) VALUES (3,'x');
SELECT a, b, c FROM t1;
a b c
3 x xxx
DROP TABLE t1;
CREATE TABLE t1 (str VARCHAR(10), pos INT, len INT, newstr VARCHAR(10), result VARCHAR(10) DEFAULT INSERT(str,pos,len,newstr));
INSERT INTO t1 (str,pos,len,newstr) VALUES ('Quadratic', 3, 4, 'What');
SELECT * FROM t1;
str pos len newstr result
Quadratic 3 4 What QuWhattic
DROP TABLE t1;
CREATE TABLE t1 (n INT, res VARCHAR(10) DEFAULT ELT(n,'ej', 'Heja', 'hej', 'foo'));
INSERT INTO t1 (n) VALUES (1);
SELECT * FROM t1;
n res
1 ej
DROP TABLE t1;
CREATE TABLE t1 (bits INT, res VARCHAR(10) DEFAULT MAKE_SET(bits,'a','b','c','d'));
INSERT INTO t1 (bits) VALUES (1|4);
SELECT * FROM t1;
bits res
5 a,c
DROP TABLE t1;
CREATE TABLE t1 (a INT, b VARCHAR(10) DEFAULT CHAR(a));
INSERT INTO t1 (a) VALUES (77);
SELECT * FROM t1;
a b
77 M
DROP TABLE t1;
CREATE TABLE t1 (a INT, b VARCHAR(10) DEFAULT CONV(a,10,16));
INSERT INTO t1 (a) VALUES (64);
SELECT * FROM t1;
a b
64 40
DROP TABLE t1;
#
# Predicates
#
CREATE TABLE t1 (a INT, b INT DEFAULT NOT a);
INSERT INTO t1 (a) VALUES (NULL),(0),(1);
SELECT * FROM t1;
a b
NULL NULL
0 1
1 0
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT, x INT DEFAULT a XOR b);
INSERT INTO t1 (a,b) VALUES (0,0),(0,1),(1,0),(1,1);
SELECT * FROM t1;
a b x
0 0 0
0 1 1
1 0 1
1 1 0
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT a IS TRUE, c INT DEFAULT a IS NOT TRUE);
INSERT INTO t1 (a) VALUES (NULL),(0),(1);
SELECT * FROM t1;
a b c
NULL 0 1
0 0 1
1 1 0
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT a IS FALSE, c INT DEFAULT a IS NOT FALSE);
INSERT INTO t1 (a) VALUES (NULL),(0),(1);
SELECT * FROM t1;
a b c
NULL 0 1
0 1 0
1 0 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT a IS NULL, c INT DEFAULT a IS NOT NULL);
INSERT INTO t1 (a) VALUES (NULL),(0),(1);
SELECT * FROM t1;
a b c
NULL 1 0
0 0 1
1 0 1
DROP TABLE t1;
CREATE TABLE t1 (a INT, b INT DEFAULT a IS UNKNOWN, c INT DEFAULT a IS NOT UNKNOWN);
INSERT INTO t1 (a) VALUES (NULL),(0),(1);
SELECT * FROM t1;
a b c
NULL 1 0
0 0 1
1 0 1
DROP TABLE t1;
CREATE TABLE t1 (a INT,
eq INT DEFAULT a=0, equal INT DEFAULT a<=>0,
ne INT DEFAULT a<>0,
lt INT DEFAULT a<0, le INT DEFAULT a<=0,
gt INT DEFAULT a>0, ge INT DEFAULT a>=0);
INSERT INTO t1 (a) VALUES (NULL),(-1),(0),(1);
SELECT * FROM t1;
a eq equal ne lt le gt ge
NULL NULL 0 NULL NULL NULL NULL NULL
-1 0 0 1 1 1 0 0
0 1 1 0 0 1 0 1
1 0 0 1 0 0 1 1
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT a LIKE 'a%');
INSERT INTO t1 (a) VALUES ('AAA'),('aaa'),('bbb');
SELECT * FROM t1;
a b
AAA 1
aaa 1
bbb 0
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT a RLIKE 'a$');
INSERT INTO t1 (a) VALUES ('AAA'),('aaa'),('bbb');
SELECT * FROM t1;
a b
AAA 1
aaa 1
bbb 0
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT a IN ('aaa','bbb'));
INSERT INTO t1 (a) VALUES ('AAA'),('aaa'),('bbb'),('ccc');
SELECT * FROM t1;
a b
AAA 1
aaa 1
bbb 1
ccc 0
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT a NOT IN ('aaa','bbb'));
INSERT INTO t1 (a) VALUES ('AAA'),('aaa'),('bbb'),('ccc');
SELECT * FROM t1;
a b
AAA 0
aaa 0
bbb 0
ccc 1
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT a BETWEEN 'aaa' AND 'bbb');
INSERT INTO t1 (a) VALUES ('AAA'),('aaa'),('bbb'),('ccc');
SELECT * FROM t1;
a b
AAA 1
aaa 1
bbb 1
ccc 0
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT a NOT BETWEEN 'aaa' AND 'bbb');
INSERT INTO t1 (a) VALUES ('AAA'),('aaa'),('bbb'),('ccc');
SELECT * FROM t1;
a b
AAA 0
aaa 0
bbb 0
ccc 1
DROP TABLE t1;
#
# Numeric result functions with string input
#
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT STRCMP(a,'b'));
INSERT INTO t1 (a) VALUES ('A'),('a'),('B'),('b'),('C'),('c');
SELECT * FROM t1;
a b
A -1
a -1
B 0
b 0
C 1
c 1
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT LENGTH(a), c INT DEFAULT CHAR_LENGTH(a), d INT DEFAULT BIT_LENGTH(a));
INSERT INTO t1 (a) VALUES ('a'),('aa'),('aaa');
SELECT * FROM t1;
a b c d
a 1 1 8
aa 2 2 16
aaa 3 3 24
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT LOCATE('a',a));
INSERT INTO t1 (a) VALUES ('xa'),('xxa'),('xxxa');
SELECT * FROM t1;
a b
xa 2
xxa 3
xxxa 4
DROP TABLE t1;
CREATE TABLE t1 (a VARCHAR(10), b INT DEFAULT REGEXP_INSTR(a, 'a'));
INSERT INTO t1 (a) VALUES ('xa'),('xxa'),('xxxa');
SELECT * FROM t1;
a b
xa 2
xxa 3
xxxa 4
DROP TABLE t1;