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?
Related
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
);
CREATE TABLE IF NOT EXISTS ban (
UID int(4) NOT NULL,
AdminUID int(4) NOT NULL,
Grund varchar(50) NOT NULL,
Datum varchar(50) NOT NULL,
IP varchar(50) NOT NULL DEFAULT '0',
Serial varchar(50) NOT NULL,
Eintragsdatum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
STime int(11) NOT NULL DEFAULT '0'
,
PRIMARY KEY (UID),
KEY Name (Name),
KEY IP (IP),
KEY Serial (Serial),
KEY STime (STime)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Error:
1072 - Key column 'Name' doesn't exist in table
Where is my fault?
Remove KEY Name (Name) in the code as there is not Name field in the table.
CREATE TABLE IF NOT EXISTS ban (
UID int(4) NOT NULL,
AdminUID int(4) NOT NULL,
Grund varchar(50) NOT NULL,
Datum varchar(50) NOT NULL,
IP varchar(50) NOT NULL DEFAULT '0',
Serial varchar(50) NOT NULL,
Eintragsdatum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
STime int(11) NOT NULL DEFAULT '0' , PRIMARY KEY (UID), KEY IP (IP), KEY Serial (Serial), KEY STime (STime) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
In below query I need to change IMEI_CODE and REGISTRATION_ID unique index as non unique index using alter query.How to achieve that
CREATE TABLE `ringee_user` (
`RINGEE_USER_ID` BIGINT(20) NOT NULL,
`USER_NAME` VARCHAR(40) NOT NULL,
`IMEI_CODE` VARCHAR(45) NOT NULL,
`REGISTRATION_ID` VARCHAR(200) NOT NULL,
`MOBILE_NUMBER` VARCHAR(14) NOT NULL,
`CREATED_DTTM` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`MODIFIED_DTTM` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
`IS_DELETE` TINYINT(1) NULL DEFAULT '0',
PRIMARY KEY (`RINGEE_USER_ID`),
UNIQUE INDEX `REG_ID_UNIQUE` (`REGISTRATION_ID`),
UNIQUE INDEX `MOBILE_NUMBER_UNIQUE` (`MOBILE_NUMBER`),
UNIQUE INDEX `IMEI_CODE_UNIQUE` (`IMEI_CODE`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
ALTER TABLE `ringee_user`
CHANGE COLUMN `IS_DELETE` `IS_DELETE` TINYINT(1) NULL DEFAULT '0' AFTER `MOBILE_NUMBER`;
I need to search records in the table which contains millions of records. I have recently updated Mysql version from 5.1 to 5.6.
I was using like in query which was taking around 15 sec to 30 sec.
Currently I have modified query to use MATCH... AGAINST feature of mysql 5.6 INNODB. My query goes like this.
SELECT JOB.IDJOB, JOB.IDEMPLOYER .....
FROM JOB
WHERE ( ( JOB.ITJOBSTARTTYPE=2 AND JOB.DTPLANNEDEND >= '2015-07-06' )
OR ( JOB.ITJOBSTARTTYPE=1
AND ( ( JOB.ITJOBENDTYPE=2 ) OR ( JOB.DTJOBEND>='2015-07-06') )
)
OR ( JOB.ITJOBSTARTTYPE=3
AND ( (JOB.DTJOBEND >='2015-07-06') OR (JOB.ITJOBENDTYPE=2) )
)
)
AND MATCH(VCJOBTITLE, LVJOBCOMPANYDESCRIPTION, VCCOMPANYNAME,
VCSALARYDESC, VCCITY, VCQUALIFICATIONREQUIRED,VCJOBREFERENCE,
LVJOBKEYWORDS )
AGAINST ('test' IN NATURAL LANGUAGE MODE)
ORDER BY JOB.ITJOBBAND ASC,
JOB.VCRANKING DESC,
JOB.IDJOB DESC,
JOB.FJOBWEIGHT ASC
LIMIT 50;
Which takes 80- 120 seconds (3X slower )
If I remove ORDER BY it will load in 6 to 10 sec.
Is their any setting Or performance tunning we can apply here?
Edit: using show create table JOB;
CREATE TABLE `JOB` (
`idJob` int(11) NOT NULL AUTO_INCREMENT,
`idEmployer` int(10) DEFAULT NULL,
`vcJobTitle` varchar(255) NOT NULL,
`lvJobCompanyDescription` text,
`idBasket` int(10) DEFAULT NULL,
`dtActualgolive` datetime DEFAULT NULL COMMENT 'whenever job status become online',
`boImmediatelygolive` int(11) NOT NULL,
`dtRequestedgolive` date DEFAULT NULL,
`dtActualend` datetime DEFAULT NULL COMMENT 'when job is set to be archieved',
`dtPlannedend` date NOT NULL COMMENT 'when posting a job this is calculated. batch will look at this date. date on which regular jobs will be archievd, or date on which internship jobs will be changed to listing',
`dtRequestedend` date DEFAULT NULL COMMENT 'exact date on which employer wants jobs to be archieved',
`dtApplicationdeadline` date DEFAULT NULL COMMENT 'job will be converted to listing on this date',
`boDurationinweeks` int(11) NOT NULL COMMENT 'whether employer gave duration in weeks or not',
`itDurationweeks` int(2) DEFAULT NULL,
`boIsoncredit` int(11) NOT NULL,
`dtCreditend` date DEFAULT NULL,
`fJobbaseprice` float NOT NULL,
`fJobpriceafteradminitemdiscount` float NOT NULL COMMENT 'price after admin item discount',
`fJobpriceafterpromodiscount` float NOT NULL COMMENT 'price after promo discount',
`vcPromotioncode` varchar(50) DEFAULT NULL,
`vcCompanyname` varchar(100) NOT NULL,
`vcSalarydesc` varchar(255) NOT NULL,
`vcCity` varchar(100) NOT NULL,
`vcCounty` varchar(120) DEFAULT NULL,
`vcQualificationrequired` text,
`boWorkfromhome` int(11) NOT NULL,
`boResidential` int(11) NOT NULL,
`boIndoor` int(11) NOT NULL,
`boOutdoor` int(11) NOT NULL,
`boIndoorandoutdoor` int(11) NOT NULL,
`itJobstarttype` int(11) DEFAULT NULL COMMENT 'date,immidiate,always recruiting',
`dtJobstart` date DEFAULT NULL,
`itJobendtype` int(11) DEFAULT NULL COMMENT 'date,ongoing',
`dtJobend` date DEFAULT NULL,
`dtFeaturedstart` date DEFAULT NULL,
`dtFeaturedend` date DEFAULT NULL,
`itFulltimeparttime` int(11) NOT NULL COMMENT 'fulltime,parttime,both',
`boEveningtime` int(11) NOT NULL,
`boDaytime` int(11) NOT NULL,
`boWeekend` int(11) NOT NULL,
`boNewsletter` int(11) NOT NULL,
`vcApplyemail` varchar(500) DEFAULT NULL,
`vcApplyphone` varchar(200) DEFAULT NULL,
`vcApplyaddress` varchar(100) DEFAULT NULL,
`vcApplyURL` varchar(500) DEFAULT NULL,
`boRequirephone` int(11) DEFAULT NULL,
`boRequireaddress` int(11) DEFAULT NULL,
`boRequirecv` int(11) DEFAULT NULL,
`boFeatured` int(11) NOT NULL,
`blLogo` longblob,
`itJobstatus` int(1) DEFAULT NULL COMMENT 'online,offline',
`itEmailedtofriendcount` int(5) DEFAULT NULL COMMENT 'keeps count of how many times users clicked on link email this to friend',
`itDeadlinetype` int(1) DEFAULT NULL,
`idInternshiptypemaster` int(11) DEFAULT NULL,
`vcLengthofscheme` varchar(100) DEFAULT NULL,
`dtInternshiplistingend` date DEFAULT NULL COMMENT 'date when internship job will archieve',
`itJobband` int(2) DEFAULT NULL,
`boGraduate` int(11) NOT NULL,
`boInternship` int(11) NOT NULL,
`boGaptemp` int(11) NOT NULL,
`boParttimeholiday` int(11) NOT NULL,
`boEntrylevel` int(11) NOT NULL DEFAULT '0',
`boHundredPercentDiscountApplicable` int(11) NOT NULL,
`vcContactdetails` varchar(100) DEFAULT NULL,
`vcJobreference` varchar(100) DEFAULT NULL COMMENT 'for importing totaljobs feeds',
`vcJoburlparam` varchar(100) DEFAULT NULL COMMENT 'for importing totaljobs url params',
`dtPutInCurrentBand` datetime DEFAULT NULL COMMENT 'date when job is online and put in band 3',
`itInitcountycount` int(2) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`lvJobkeywords` text,
`fJobweight` decimal(10,2) DEFAULT '1.00' COMMENT 'Holds the job weight ranging between 0 to 1 (1 being the highest weight)',
`dtReinstatedOn` datetime DEFAULT NULL,
`boIncludeiniframe` int(11) DEFAULT NULL,
`boIsAdvertFeatured` int(11) DEFAULT NULL,
`itincludexml` int(1) DEFAULT '1',
`boAddtojobalert` int(11) DEFAULT '1',
`vcRanking` int(11) DEFAULT '999999',
`itIncludeUkptj` int(11) DEFAULT '1',
PRIMARY KEY (`idJob`),
KEY `JOB_I_1` (`idEmployer`,`itJobstatus`),
KEY `JOB_I_2` (`itJobstatus`,`boGaptemp`,`dtJobstart`),
KEY `JOB_I_3` (`itJobstatus`,`boGraduate`,`dtJobstart`),
KEY `JOB_I_4` (`itJobstatus`,`boInternship`,`dtJobstart`),
KEY `JOB_I_5` (`itJobstatus`,`boParttimeholiday`,`dtJobstart`),
KEY `FI_JOB_idOrder_ORDER_idOrder` (`idBasket`),
KEY `FI_JOB_vcPromotioncode_PROMOTION_vcPromotioncode` (`vcPromotioncode`),
KEY `FI_JOB_idInternshiptype_ITTM_idInternshiptypemaster` (`idInternshiptypemaster`),
KEY `JOB_I_6` (`created_at`),
KEY `boEntrylevel` (`boEntrylevel`),
KEY `itJobband` (`itJobband`),
FULLTEXT KEY `index_ft_search` (`vcJobTitle`,`lvJobCompanyDescription`,`vcCompanyname`,`vcSalarydesc`,`vcCity`,`vcQualificationrequired`,`vcJobreference`,`lvJobkeywords`),
CONSTRAINT `fk_JOB_idEmployer_EMPLOYER_idEmployer` FOREIGN KEY (`idEmployer`) REFERENCES `EMPLOYER` (`idEmployer`),
CONSTRAINT `fk_JOB_idInternshiptype_ITTM_idInternshiptypemaster` FOREIGN KEY (`idInternshiptypemaster`) REFERENCES `INTERNSHIPTYPEMASTER` (`idInternshiptypemaster`),
CONSTRAINT `fk_JOB_idOrder_ORDER_idOrder` FOREIGN KEY (`idBasket`) REFERENCES `BASKET` (`idBasket`),
CONSTRAINT `fk_JOB_vcPromotioncode_PROMOTION_vcPromotioncode` FOREIGN KEY (`vcPromotioncode`) REFERENCES `PROMOTION` (`vcPromotioncode`)
) ENGINE=InnoDB AUTO_INCREMENT=1739324 DEFAULT CHARSET=latin1 |
Edit:
Without sort:
With Sort:
In Mysql, ID is Auto Increment but when converted to Postgres there is no Auto Increment .
Mysql database
CREATE TABLE IF NOT EXISTS `cities` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`state_id` bigint(20) NOT NULL,
`district_id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`description` varchar(1000) DEFAULT NULL,
`created_by` int(11) NOT NULL,
`modified_by` int(11) DEFAULT NULL,
`is_active` char(1) NOT NULL DEFAULT 'Y',
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `state_id` (`state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=26 ;
After Converted to postgres database
CREATE TABLE cities (
"id" bigint NOT NULL,
state_id bigint NOT NULL,
district_id bigint NOT NULL,
"name" varchar(255) NOT NULL,
description varchar(1000),
created_by int NOT NULL,
modified_by int,
is_active char(1) NOT NULL,
created timestamp,
modified timestamp,
PRIMARY KEY ("id")
);
INSERT INTO cities(state_id, district_id, city_type, name, description,
created_by, modified_by, is_active, created, modified)
VALUES
(1, 1, '', 'Ramtek', null, 1, null, 'Y',
'2015-04-16 10:44:11', '2015-04-16 10:44:11');
You could use bigserial in such cases. It provides an auto increment feature in postgres:
CREATE TABLE cities (
"id" bigserial PRIMARY KEY,
Not Null constraint is provided by default.
Docs : http://www.postgresql.org/docs/current/static/datatype.html#DATATYPE-SERIAL