I have a table Members with column DayJoin of type varchar. I want to set default value of the column DayJoin should be the current day if user does not enter a value for that field.
In MSSQL Server I wrote this statement:
CREATE TABLE Members
(
ID int identity (1,1) primary key,
Email varchar(50),
Password Varchar(50),
Role Varchar(50) default 'Member',
DayJoin varchar(50) DEFAULT Convert(varchar, GETDATE(),103),
LastLogin varchar(50)DEFAULT Convert(varchar, GETDATE(),103),
FName varchar(50),
LName varchar(50),
Phone varchar(50),
DOB varchar(50),
Gender varchar(50),
IDcard varchar(50),
Address varchar(50),
City varchar(50),
Job varchar(50),
Avatar varchar(50) default 'images/noavatar.gif'
)
The statement gets the value of the current day and formats it as (dd/mm/yyyy). But when I run a similar one in mySQL, it's not working
CREATE TABLE `NewTable` (
`ID` int UNSIGNED NULL AUTO_INCREMENT ,
`Email` varchar(50) NULL ,
`Password` varchar(50) NULL DEFAULT '' ,
`Role` varchar(10) NULL ,
`DayJoin` varchar(10) NULL DEFAULT 'CURRENT_TIMESTAMP' ,
`LastLogin` varchar(10) NULL DEFAULT 'date_format(now(),%Y/%m/%d)' ,
`Firstname` varchar(50) NULL ,
`Lastname` varchar(50) NULL ,
`Phone` varchar(20) NULL ,
`DOB` varchar(10) NULL ,
`Gender` varchar(20) NULL ,
`IDcard` varchar(20) NULL ,
`Address` varchar(50) NULL ,
`City` varchar(50) NULL ,
`Job` varchar(50) NULL ,
`Avatar` varchar(50) NULL DEFAULT 'images/noavatar.gif' ,
PRIMARY KEY (`ID`)
)
ENGINE=ARCHIVE
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_unicode_ci
;
I get a response saying something like; "default value in error"
Does anyone know what the problem is?
Use DATETIME type instead:
`DayJoin` DATETIME DEFAULT CURRENT_TIMESTAMP
remove the quotes around 'CURRENT_TIMESTAMP' in your code and use TIMESTAMP data type. It should read like:
`DayJoin` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
It will give error as "Invalid default value for 'DayJoin' "
Use datetime or timestamp datatype for 'DayJoin'
`DayJoin` TimeStamp NULL DEFAULT 'CURRENT_TIMESTAMP' ,
Try this change your DayJoin datatype
`DayJoin` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
or
`DayJoin` TimeStamp NULL DEFAULT CURRENT_TIMESTAMP
Related
I have it set up to echo when my tables create but the users table just won't, I've looked at multiple solutions but none of them has helped here my code
<?
phpinclude_once("index.php");
$tbl_users = "CREATE TABLE IF NOT EXISTS users
(
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL,
email VARCHAR(64) NOT NULL,
password VARCHAR(64) NOT NULL,
gender ENUM('m','f') NOT NULL,
website VARCHAR(64) NULL,
country VARCHAR(64) NULL,
userlevel ENUM('a','b','c','d') NOT NULL DEFAULT a,
avatar VARCHAR(64) NULL,
ip VARCHAR(64) NOT NULL,
signup DATETIME NOT NULL,
lastlogin DATETIME NOT NULL,
notescheck DATETIME NOT NULL,
activated ENUM('0','1') NOT NULL DEFAULT 0,
PRIMARY KEY (id)),
UNIQUE KEY username (username,email))";
$query = mysqli_query($db_spooky, $tbl_users);
if ($query === TRUE)
{
echo "<h3>user table created OK :) </h3>";
}
else
{
echo "<h3>user table NOT created :( </h3>";
}
?>
CREATE TABLE IF NOT EXISTS users (id INT(11) NOT NULL AUTO_INCREMENT
,username VARCHAR(16) NOT NULL
,email VARCHAR(64) NOT NULL,password VARCHAR(64) NOT NULL
,gender ENUM('m','f') NOT NULL
,website VARCHAR(64) NULL
,country VARCHAR(64) NULL
,userlevel ENUM('a','b','c','d') NOT NULL DEFAULT 'a'
,avatar VARCHAR(64) NULL
,ip VARCHAR(64) NOT NULL
,signup DATETIME NOT NULL
,lastlogin DATETIME NOT NULL
,notescheck DATETIME NOT NULL,activated ENUM('0','1') NOT NULL DEFAULT 0
,PRIMARY KEY (id)
,UNIQUE KEY username (username,email))
Learn to organize your code instead of writing it in one long line for one, and two, ENUM default for a needs to be in single quotes and you had an extra closing bracket. The code above is the corrected version.
This question already has answers here:
What is wrong with my SQL here? #1089 - Incorrect prefix key
(15 answers)
Closed 7 years ago.
I have a problem creating a table with phpmyadmin, which gives me the following error:
#1089 - Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys
This is the query that I do:
CREATE TABLE `b2b`.`users` ( `id` BIGINT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(30) NOT NULL ,
`surnames` VARCHAR(80) NOT NULL ,
`birthdate` DATE NOT NULL ,
`drivingdoc` VARCHAR(20) NOT NULL ,
`acdate` DATE NOT NULL ,
`countrydoc` VARCHAR(20) NOT NULL ,
`province` VARCHAR(20) NOT NULL ,
`locality` VARCHAR(35) NOT NULL ,
`address` VARCHAR(150) NOT NULL ,
`number` VARCHAR(20) NOT NULL ,
`flat` VARCHAR(20) NOT NULL ,
`door` VARCHAR(20) NOT NULL ,
`description` VARCHAR(2000) NOT NULL ,
PRIMARY KEY (`id`(7))) ENGINE = InnoDB;
Using MariaDB in ubuntu minimal.
The problem is:
PRIMARY KEY (`id`(7))
You cannot use part of a number as a key, you have to use the whole thing. Also, specifying lengths for numeric types is useless at best, and damaging at worst.
Change to:
PRIMARY KEY (`id`)
That Primary Key syntax is nothing I've ever seen before. Try this:
CREATE TABLE `b2b`.`users` (
`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
....
) ENGINE = InnoDB;
Or if you want
CREATE TABLE `b2b`.`users` (
`id` BIGINT NOT NULL AUTO_INCREMENT,
....
PRIMARY KEY (id)
) ENGINE = InnoDB;
You have several errors in your sql, so try to find the difference with this sql
CREATE TABLE `b2b`.`users` ( `id` INT(7) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(30) NOT NULL ,
`surnames` VARCHAR(80) NOT NULL ,
`birthdate` DATE NOT NULL ,
`drivingdoc` VARCHAR(20) NOT NULL ,
`acdate` DATE NOT NULL ,
`countrydoc` VARCHAR(20) NOT NULL ,
`province` VARCHAR(20) NOT NULL ,
`locality` VARCHAR(35) NOT NULL ,
`address` VARCHAR(150) NOT NULL ,
`number` VARCHAR(20) NOT NULL ,
`flat` VARCHAR(20) NOT NULL ,
`door` VARCHAR(20) NOT NULL ,
`description` VARCHAR(255) NOT NULL ,
PRIMARY KEY (`id`)) ENGINE = InnoDB;
Right now, i've created database with having 10 columns in my database but i can't create the database table and i got "#1067 - Invalid default value for 'primary_key" error.
Here is my sql code:
CREATE TABLE `quiz`.`user_details` ( `primary_key` INT(6) UNSIGNED NOT NULL DEFAULT '1' AUTO_INCREMENT , `foreign_key` INT(6) UNSIGNED NOT NULL DEFAULT '1' AUTO_INCREMENT , `firstname` VARCHAR(20) NOT NULL , `lastname` VARCHAR(20) NOT NULL , `username` VARCHAR(20) NOT NULL , `password` VARCHAR(20) NOT NULL , `email` VARCHAR(20) NOT NULL , `phone_number` INT(10) NOT NULL , `address` CHAR(50) NOT NULL , `exam_taking` INT(3) NOT NULL AUTO_INCREMENT , PRIMARY KEY (`primary_key`(6)), UNIQUE `foreign_key` (`foreign_key`)) ENGINE = InnoDB;
Don't add DEFAULT value because you already defined primary key as AUTO INCREMENT.
Possible duplicate.
#1067 - Invalid default value for 'bonusid' how can i fix this error?
You cannot have a default value for auto incrementing fields. Simply change the definition to:
CREATE TABLE `quiz`.`user_details`
(`primary_key` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
`foreign_key` INT(6) UNSIGNED NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(20) NOT NULL ,
`lastname` VARCHAR(20) NOT NULL ,
`username` VARCHAR(20) NOT NULL ,
`password` VARCHAR(20) NOT NULL ,
`email` VARCHAR(20) NOT NULL ,
`phone_number` INT(10) NOT NULL ,
`address` CHAR(50) NOT NULL ,
`exam_taking` INT(3) NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`primary_key`),
UNIQUE `foreign_key` (`foreign_key`)) ENGINE = InnoDB;
Note also that a name foreign_key for an attribute which is not a foreign key is really misleading.
My server is on hostgator running on a linux centOS.
I'm simply trying to create a table within my database and I figured out how to get the table to get created. Although when I add the AUTO_INCREMENT setting the code doesn't execute and the table isn't created.
Why would this be and how can I correct it?
Here is my code:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
To use AUTO_INCREMENT you may have to assign the column as a primary key:
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL,
PRIMARY KEY (id))";
Your query will give error
there can be only one auto column and it must be defines as a key, so add primary key to id field
$members2_table = "CREATE TABLE ninja08_codin.members2(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(64) NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cred VARCHAR(10) NOT NULL)";
I'm working on a codeigniter project and I'm trying to troubleshoot a sql issue. I have a query that updates a date field in my table and it's not updating it at all.
I have this table
CREATE TABLE `Customer` (
`customer_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(55) NOT NULL DEFAULT '',
`last_name` varchar(55) NOT NULL DEFAULT '',
`city` varchar(255) DEFAULT '',
`state` char(2) DEFAULT '',
`zip` int(5) DEFAULT NULL,
`title` varchar(255) NOT NULL DEFAULT '',
`image` varchar(255) DEFAULT '',
`blurb` blob,
`end_date` date DEFAULT NULL,
`goal` int(11) DEFAULT NULL,
`paypal_acct_num` int(11) DEFAULT NULL,
`progress_bar` enum('full','half','none') DEFAULT NULL,
`page_views` int(11) NOT NULL DEFAULT '0',
`total_pages` int(11) NOT NULL DEFAULT '0',
`total_conversions` int(11) NOT NULL DEFAULT '0',
`total_given` int(11) NOT NULL DEFAULT '0',
`conversion_percentage` int(11) NOT NULL DEFAULT '0',
`avg_contribution` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`customer_id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
and when I run this query to insert data, it runs fine and sets the date to 2012-11-01
INSERT INTO `Customer` (`first_name`, `last_name`, `end_date`) VALUES ('John', 'Smith2', '2012-11-01');
Then I get the customer_id and try to run this query
UPDATE `Customer` SET `end_date` = '2012-14-01' WHERE `customer_id` = '18';
and it sets the date end_date field to 0000-00-00.
Why is it changing the end date to 0000-00-00 rather than 2012-14-01?
2012-14-01 is first day of fourteenth month :)
(so its invalid date, thus casted to 0000-00-00 and Data truncated for column 'end_date' at row 1 warning was returned by mysql, which you can see by querying SHOW WARNINGS to mysql immediately after badly behaving query)
2012-01-14 is 14th of January.
use this:
UPDATE `Customer` SET `end_date` = date('Y-m-d') WHERE `customer_id` = '18';
Use date function to update this field.