current_timeStamp is not working - mysql

I tried this code
CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP() NOT NUll,
updatedDate DATETIME
);
but the following error will come
1067 - Invalid default value for 'createdDate'
thanks

Use simply CURRENT_TIMESTAMP instead CURRENT_TIMESTAMP()
CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL, password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP NOT NUll,
updatedDate DATETIME
);
In addition, you can initialize or update any TIMESTAMP column to the
current date and time by assigning it a NULL value, unless it has been
defined with the NULL attribute to permit NULL values.
For more knowledge click the link http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html

Change the datatype datetime to timestamp it will work.
CREATE TABLE users ( userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate **timestamp** DEFAULT CURRENT_TIMESTAMP() NOT NUll,
updatedDate DATETIME )

Try this code, it should work.
CREATE TABLE users (
userId INT PRIMARY KEY AUTO_INCREMENT NOT NUll,
account VARCHAR(200) NOT NULL,
password varchar(200) NOT Null,
isActive varchar(10) NOT NUll,
createdDate DATETIME DEFAULT CURRENT_TIMESTAMP NOT NUll,
updatedDate DATETIME
);

Related

MySQL database with as low repetition as possible if that's possible

I have the following table structure in a MySQL database:
CREATE TABLE IF NOT EXISTS Links (
id SERIAL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
link TEXT NOT NULL,
lastUpdatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
createdOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS Chronometer (
id SERIAL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
dateOf DATETIME NOT NULL,
lastUpdatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
createdOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS Countdown (
id SERIAL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
dateOf DATETIME NOT NULL,
lastUpdatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
createdOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS MonthlyReminder (
id SERIAL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
day BIGINT UNSIGNED NOT NULL,
daysBefore BIGINT UNSIGNED NOT NULL,
daysAfter BIGINT UNSIGNED NOT NULL,
lastUpdatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
createdOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS AnnualReminder (
id SERIAL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL DEFAULT '',
day BIGINT UNSIGNED NOT NULL,
month BIGINT UNSIGNED NOT NULL,
daysBefore BIGINT UNSIGNED NOT NULL,
daysAfter BIGINT UNSIGNED NOT NULL,
lastUpdatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
createdOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
All the tables have a name, description, createdOn and lastUpdatedOn columns.
Is there any I can avoid all this repetition?
Maybe using more Enums and foreign Keys?

fetch data from table

I have the following table in my database
CREATE TABLE `sms_pool` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ag_id` VARCHAR(20) NOT NULL,
`sms_to` VARCHAR(15) NOT NULL,
`template_name` VARCHAR(100) NOT NULL,
`contents` VARCHAR(500) NOT NULL,
`bulk_flag` VARCHAR(1) NOT NULL,
`file_name` VARCHAR(100) NULL DEFAULT NULL,
`send_flag` VARCHAR(1) NOT NULL,
`creation_date` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` VARCHAR(20) NOT NULL,
`modification_date` DATETIME NULL DEFAULT NULL,
`modified_by` VARCHAR(20) NULL DEFAULT NULL,
`processing_msg` VARCHAR(2000) NULL DEFAULT NULL,
PRIMARY KEY (`id`),
);
I wish to write a procedure/function which takes 'id' as input.
If 'id' is present in the table then it should return the corresponding row,
if 'id' = NULL then it should return all of the rows from the database.
NOTE : if 'id' is not present in table then it should return all of the rows.
How should I do this? Any help is appreciated.
Thank you in advance. :D
You probably mean
select * from sms_pool
where :id = id OR :id is null;

data too long for this column mysql error

Here's my query:
update slcm.m_user_master set Is_Active = '1' where M_USER_ID = '1'
while updating table it showing message data too long for this column .
I have taken database IsActive database as bit.
this is table description
e Table
CREATE TABLE `m_user_master` (
`M_USER_ID` int(11) NOT NULL AUTO_INCREMENT,
`User_Type_ID` int(11) DEFAULT NULL,
`M_User_Name` varchar(50) DEFAULT NULL,
`M_User_Name_Hindi` varchar(50) DEFAULT NULL,
`User_Login_ID` varchar(30) DEFAULT NULL,
`User_Password` varchar(30) DEFAULT NULL,
`User_Mobile_No` int(12) DEFAULT NULL,
`User_Email_ID` varchar(50) DEFAULT NULL,
`Created_Date` datetime DEFAULT NULL,
`Updated_By` varchar(50) DEFAULT NULL,
`Updated_Date` datetime DEFAULT NULL,
`Is_Active` char(1) DEFAULT NULL,
`Active_From` datetime DEFAULT NULL,
`Active_To` datetime DEFAULT NULL,
`Created_By` varchar(50) DEFAULT NULL,
PRIMARY KEY (`M_USER_ID`),
KEY `FK_M_User_Master_M_User_Type_Master` (`User_Type_ID`),
CONSTRAINT `FK_M_User_Master_M_User_Type_Master` FOREIGN KEY (`User_Type_ID`) REFERENCES `m_user_type_master` (`User_Type_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8
Try the value for Is_Active without quotes like so:
update slcm.m_user_master set Is_Active = 1 where M_USER_ID = '1'
It's expecting a true/false (1/0) value which takes a single bit to store for Is_Active and it's getting a string instead which takes a byte or 8 bits to store. Hence the "too long" error.
update slcm.m_user_master set Is_Active = 1 where M_USER_ID = 1
and you can see this :
Return Bit Value as 1/0 and NOT True/False in SQL Server

Error Code: 1067. Invalid default value for 'last_visit'

A keep getting this error in my mysql statement even though I'm not specifying a default value. Previous responses said adding a NOT NULL would fix it, but I still get the error. What is odd is if I switch the lines of date_joined and last_visit, I get the same error, except on date_joined. Any ideas why its giving this error? Thanks
CREATE TABLE user_info(
user_id serial,
oauth_provider VARCHAR(255) NOT NULL,
oauth_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth date NOT NULL,
email VARCHAR(255) NOT NULL,
gender char NOT NULL,
date_joined timestamp NOT NULL,
last_visit timestamp NOT NULL,
account_enabled boolean NOT NULL,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)) ENGINE=InnoDB;
I use your code and me all work:
If you are using MySQL < 5.6 you cant have 2 timestamp column with same default values.
For MySQL 5.5 check this http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
One TIMESTAMP column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
So if you want to have 2 column with timestamp try this.
CREATE TABLE user_info(
user_id serial,
oauth_provider VARCHAR(255) NOT NULL,
oauth_id VARCHAR(255) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
date_of_birth date NOT NULL,
email VARCHAR(255) NOT NULL,
gender char NOT NULL,
date_joined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMPL,
last_visit TIMESTAMP NULL ON UPDATE CURRENT_TIMESTAMP,
account_enabled boolean NOT NULL,
CONSTRAINT user_info_pkey PRIMARY KEY (user_id)
) ENGINE=InnoDB;

mysql error 1215 cannot add foreign key contraint

DROP TABLE IF EXISTS desk;
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS authority;
CREATE TABLE authority
(
authorityId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskId INT DEFAULT 0,
authorityName VARCHAR(50) NOT NULL,
descriptions VARCHAR(100) NOT NULL,
userLevel TINYINT NOT NULL,
actions VARCHAR(300) DEFAULT NULL,
setDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE desk
(
deskId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskName CHAR(50) NOT NULL,
deskLink VARCHAR(100) NOT NULL,
details VARCHAR(350) DEFAULT NULL,
setDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
closeDate TIMESTAMP DEFAULT NULL,
userId INT DEFAULT NULL,
status TINYINT(1) DEFAULT 0
);
CREATE TABLE user
(
userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userName CHAR(150) NOT NULL,
password VARCHAR(500) NOT NULL,
department VARCHAR(150) DEFAULT NULL,
deskId INT NOT NULL,
status TINYINT(1) DEFAULT NULL,
userLevel TINYINT NOT NULL,
FOREIGN KEY (deskId) REFERENCES desk (deskId) ON DELETE CASCADE,
FOREIGN KEY (userLevel) REFERENCES authority (userLevel)
);
Error while executing query.
Error Code: 1215 .Cannot add foreign key contraint
I have tried many way but still cannot solve the problem, i tried change userLevel type to INT both table user and authority but nothing went to better
I really not know how to fix this problem, please help
Follow below steps
1) You must change your second query to
DROP TABLE IF EXISTS desk;
CREATE TABLE desk
(
deskId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskName CHAR(50) NOT NULL,
deskLink VARCHAR(100) NOT NULL,
details VARCHAR(350) DEFAULT NULL,
setDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
closeDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
userId INT DEFAULT NULL,
status TINYINT(1) DEFAULT 0
);
2) As your column authority.userLevel is not a primary key and you want to use it as foreign key in other table you need to create index on authority.userLevel column
use below query
ALTER TABLE `authority` ADD INDEX `UserLevelIndex` (`userLevel`);
3) Run your third query as it is to create user table
Run Following queries
DROP TABLE IF EXISTS desk;
DROP TABLE IF EXISTS user;
DROP TABLE IF EXISTS authority;
CREATE TABLE authority
(
authorityId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskId INT DEFAULT 0,
authorityName VARCHAR(50) NOT NULL,
descriptions VARCHAR(100) NOT NULL,
userLevel TINYINT NOT NULL,
actions VARCHAR(300) DEFAULT NULL,
setDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
index `cr_index` (userLevel)
);
CREATE TABLE desk
(
deskId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
deskName CHAR(50) NOT NULL,
deskLink VARCHAR(100) NOT NULL,
details VARCHAR(350) DEFAULT NULL,
setDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
closeDate TIMESTAMP DEFAULT '0000-00-00 00:00:00',
userId INT DEFAULT NULL,
status TINYINT(1) DEFAULT 0
);
CREATE TABLE `user`
(
userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userName CHAR(150) NOT NULL,
password VARCHAR(500) NOT NULL,
department VARCHAR(150) DEFAULT NULL,
deskId INT NOT NULL,
status TINYINT(1) DEFAULT NULL,
userLevel TINYINT NOT NULL,
FOREIGN KEY (deskId) REFERENCES desk (deskId) ON DELETE CASCADE,
FOREIGN KEY (userLevel) REFERENCES authority (userLevel)
);
In your query you write timestamp default value NULL and you can create a non index key userLevel as a foreign key so from your query remove default value or change the default value 0000-00-00 00:00:00(in desk table) and add index in userLevel column (in authority table)
I hope this will work for you