Here's a snippet of my PHP that is creating the table:
$sql = 'CREATE TABLE '.$table.' (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 55 ) NOT NULL ,
`venue` VARCHAR( 55 ) NOT NULL ,
`time` TIMESTAMP NOT NULL ,
`desc` TEXT NOT NULL
)';
This is making the time column become the current timestamp when I add or change a row. How can I prevent this?
That's the default behavior of the TIMESTAMP column type. I'd recommend changing it to a DATETIME column.
You can also alter the behavior by explicitly specifying how you want it to behave.
added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
The above column specification will not have the ON UPDATE behavior. You can also specify a NULL value or 0 as the default.
Related
I faced a problem when inserting a NULL value into a column defined as NOT NULL WITH DEFAULT values.
In this example, I removed most of the columns for illustration purposes.
CREATE TABLE
FKTIM04
(
OBJECTID CHARACTER(32) NOT NULL,
UP_CHANGE_CL CHARACTER(1) DEFAULT '1' NOT NULL,
UP_CTRL_CL CHARACTER(1) DEFAULT '0' NOT NULL,
CONSTRAINT PK_FKTIM04 PRIMARY KEY (OBJECTID)
);
When I execute this SQL statement, there is an error:
INSERT INTO KTI.FKTIM04 (
UP_Change_CL
,UP_ctrl_CL
,ObjectID
)
VALUES (
NULL
,NULL
,'UMSTM0LW8A8Z50DT4WA7U93EEQDRXRTH'
)
Error:
[Code: -407, SQL State: 23502] Assignment of a NULL value to a NOT
NULL column "TBSPACEID=2, TABLEID=1298, COLNO=46" is not allowed..
SQLCODE=-407, SQLSTATE=23502, DRIVER=4.22.29
I know that the column is defined as NOT NULL. If it tries to insert a NULL into the column, shouldn't it take the DEFAULT value instead?
Please teach me how to get the DEFAULT values to be inserted instead.
What should I look out for?
Thank you.
The default value will be used for a column if a value is not supplied in the INSERT statement for this column.
So don't include the columns that you want to get their default values in the list like this:
INSERT INTO KTI.FKTIM04 (
ObjectID
)
VALUES (
'UMSTM0LW8A8Z50DT4WA7U93EEQDRXRTH'
)
this way the row will be inserted and the 2 columns, since they were not specified in the list, will get their default values.
See the demo.
Another way to achieve the same is by using DEFAULT keyword:
INSERT INTO FKTIM04 (
UP_Change_CL
,UP_ctrl_CL
,ObjectID
)
VALUES (
DEFAULT
,DEFAULT
,'UMSTM0LW8A8Z50DT4WA7U93EEQDRXRTH'
)
See the demo.
I have a task to build a table in SQL Server, the task is to have 2 football teams match.
but before starts and ends and then putting the values of the results (HostTeam) (AwayTeam) and (WinnerTeam), they have to be 0-0 at (HostTeam)(AwayTeam) columns and NULL on the (WinnerTeam) column.
"When adding a game record, the starting score should be 0-0 and the winner team name should be empty"
I don't want to to explicitly give you the answer because this seems like you're asking for the answer to a homework/quiz question. That being said, here is some help in the right direction.
You have 2 options, either write out the CREATE TABLE script yourself, or use the built in tools provided within SSMS. Either way, you will name your table, create your desired columns/fields, give those columns/fields a type, determine whether they are allowed to be empty or not (Nullable), and you can then set DEFAULT values (NULL, 0, etc.)
If you have any code showing your current attempt, please post that and we can go from there.
As you can see bellow you have some options about your default value.
AUTO_INCREMENT means x++
DEFAULT NULL means The default value of this column is null
NOT NULL DEFAULT '0' means The deafault Value is NOT NULL but 0 (you
can define it yourself)
CREATE TABLE test_user (
id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Id Auto_Increment Default Null',
name varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'No_name' COMMENT 'Varchar Name
Default Value No_Name',
surname varchar(255) DEFAULT NULL COMMENT 'Varchar Name Default NULL',
num int(11) NOT NULL DEFAULT '0' COMMENT 'Default integer value 0',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I have sample ddl script:
CREATE TABLE PERSON
(
ID BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL
, FIRST_NAME VARCHAR(100) NOT NULL
, LAST_NAME VARCHAR(100) NOT NULL
, DATE_OF_BIRTH DATE
, GENDER VARCHAR(1) NOT NULL
, SSN VARCHAR(100)
, LAST_LOGIN TIMESTAMP
, VERSION INT DEFAULT 0 NOT NULL
);
When I open this database in db visualiser I see timestamp has size 26 . I really dont know why because I read default is 6. So I want to change it for example:
, LAST_LOGIN TIMESTAMP(1)
now is size 21. This is really weird. Probably last integer mean size but what is the meaning of first number "2" ?
What you call "size" is the subsecond precision of the timestamp, which is 6 by default. Your example of TIMESTAMP(1) defines a timestamp with subsecond precision of 1.
The number you see in DB Visualiser is the display size of the TIMESTAMP column.
TIMESTAMP(1), such as '2013-06-19 01:01:01.1', is always respresented using 21 characters. Timestamps defined as TIMESTAMP or TIMESTAMP(6) are displayed with five more digits at the end.
I have the following table:
CREATE TABLE `events` (
`evt_id` int(11) NOT NULL AUTO_INCREMENT,
`evt_name` varchar(50) NOT NULL,
`evt_description` varchar(100) DEFAULT NULL,
`evt_startdate` date NOT NULL,
`evt_enddate` date DEFAULT NULL,
`evt_starttime` time DEFAULT NULL,
`evt_endtime` time DEFAULT NULL,
`evt_amtpersons` int(11) DEFAULT NULL,
`sts_id` int(11) NOT NULL,
`adr_id` int(11) DEFAULT NULL,
`evt_amtPersonsSubs` int(11) DEFAULT NULL,
`evt_photo` varchar(50) DEFAULT NULL,
`sys-mut-dt` timestamp NULL DEFAULT NULL,
`sys-mut-user` varchar(20) DEFAULT NULL,
`sys-mut-id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`evt_id`),
KEY `sts_id` (`sts_id`),
KEY `adr_id` (`adr_id`),
CONSTRAINT `sts_id` FOREIGN KEY (`sts_id`) REFERENCES `statusses` (`sts_id`) O
N DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1
Now I have got two problems:
Here is my query:
INSERT INTO `events`(`evt_name` , `evt_description` , `evt_startdate` , `evt_enddate` , `evt_starttime` , `evt_endtime` , `evt_amtpersons` , `sts_id` , `adr_id` , `evt_amtPersonsSubs` , `evt_photo` , `sys-mut-user` , `sys-mut-id`) VALUES ('asf' , 'asf' , '2009-04-02' , '2009-04-22' , '00:00:00' , '00:00:00' , '3' , '1' , '' , '' , '' , 'test' , '1')
When I execute this query through my php programs I get no error. But when I execute the query in a shell directly on the mysql database I get two warnings. How can I get PHP to alert me when there are warnings because if there are warnings mysql doesn't do the insert.
About the warnings:
| Warning | 1366 | Incorrect integer value: '' for column 'adr_id' at row 1
| Warning | 1366 | Incorrect integer value: '' for column 'evt_amtPersonsSubs' a t row 1
How can I get rid of these warnings. Tried to make some changes but it didn't work out so far.
You are inserting an empty string. You should remove the '' and put a number in that field
As you said, the column does not have to have a value specified when you insert. The fact is indicated by the "DEFAULT NULL" for that column at table creation. This fact, however, means that if you do not specify the column name in your list of columns while doing INSERT (and therefore you will not specify the corresponding value either), then the tuple can be inserted anyway, and for that column value you will get a NULL automagically by default.
However, in your query you specify that you are going to insert that column value, and the column value you say is '' (an empty string). This is of course not valid, because that column accepts integers (or NULL, because you havent' declared the column NOT NULL), and an empty string is an empty string, not an integer.
The SQL server is generous and accepts the empty string anyway (probably it casts it to zero) but reports you a warning. If you set a strict mode for the server (something I strongly suggest you to do), you will get an error and the insert will fail.
Please note that if you follow my suggestion of setting strict mode, this is server wide, involving all your databases and all your tables (at least with the mysql released one year ago). If you have awfully written software that need a forgiving server, then you cannot use it.
The error message tells you that that the empty string ('') is not a valid value for an integer field - in this case the fields adr_id and evt_amtPersonsSubs. Did you mean to put NULL instead?
In PHP, you can retrieve the error or warning message, for the most recent query only, using the mysql_error() function.
'' is not an integer.... how about using NULL in the query if you actually want a null value?
The warnings tell you that you're trying to insert a string value into an integer column.
In all the places where you have an int column you must not put the value between ' but just put the value as is
[...]'00:00:00' , '00:00:00' , 3 , 1 , [...]
If you don't want to provide a value for a certain column you should define the column with NULL. Then you can even leave your '' for the insert.
BUT
In general it's bad practice to do inserts like that. What if you one day need to add a column to your table? Then you have to go and rewrite your code as well.
Therefore you should do inserts like that:
INSERT INTO tbl_name (col1, col2) VALUES(value1, value2);
This way your code will still work, even if you decide to add columns. Plus the code is easier to read!!
Implicit defaults are defined as follows:
For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT
attribute, the default is the next value in the sequence.
Reference:
MySQL 5.7 Reference Manual / Data Types / Data Type Default Values
I inherited MYSQL database that has lots of tables with data like
CREATE TABLE IF NOT EXISTS `ejl_registration` (
`id` int(11) NOT NULL auto_increment,
`team_id` int(11) default NULL,
`start_date` date default NULL,
`end_date` date default NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=88668 ;
start_date and end_date should have values like:
2007-1-5, 2007-12-31
2008-1-1, 2008-12-31
2009-1-15,2009-12-31
But some of those en_date fields are either NULL or 0000-00-00.
Is there a ways to have single query to update all those invalid en_date fields and set their value to the end of the year equal to the year of start_date
Try this (please double check, I have not tested the command):
UPDATE `ejl_registration` SET `end_date`= CONCAT(YEAR(`start_date`),'-12-31')
WHERE `end_date` IS NULL OR `end_date` = '0000-00-00';
I don't know if DATEADD and DATEDIFF exist in MySQL, but I would strongly advise using some kind of date function rather than converting to strings and manipulating them that way.
In MS SQL SERVER this would work...
UPDATE
ejl_registration
SET
end_date = DATEADD(YEAR, 1 + DATEDIFF(YEAR, 0, start_date), 0)
WHERE
(end_date) IS NULL OR (end_date = '0000-00-00')