i have an error while import the sql file - mysql

CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(240) NOT NULL,
`email` varchar(240) NOT NULL,
`password` varchar(240) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

Maybe the table name has something to do.I see that you chose the name "USERS".Perhaps is a reserved word!What database are you using?

Fixed the first part:
CREATE TABLE users(
id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
name VARCHAR(240) NOT NULL,
email VARCHAR(240) NOT NULL,
password VARCHAR(240) NOT NULL)
..
.
.

Related

There can be only one auto column error when giving AUTO_INCREMENT value

I am using phpMyAdmin while trying to create a table.
The definition is as follows
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to have id as an auto increment variable but it seems that I can't make it thus even though it is a primary key(I needed the id attribute because a user can write many comments on the same movie). So I don't understand why I still get this error. Thanks in advance.
I'd rather advise you to have clear PRIMARY KEY which is AUTO_INCREMENT and UNIQUE KEY like in your example:
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

#1452 - Cannot add or update a child row: a foreign key constraint fails ... Jutts

I have created tables in MySQL Workbench as shown below :
Categories Table
CREATE TABLE IF NOT EXISTS `categories` (
`categoryId` int(11) primary key auto_incremnt NOT NULL,
`categoryName` varchar(200) DEFAULT NULL,
`categoryDescription` varchar(200) DEFAULT NULL,
`categoryPicture` varchar(100) DEFAULT NULL,
`PostDate` varchar(255) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT '1',
`LastInsertID` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=45 ;
Products Table
CREATE TABLE IF NOT EXISTS `products` (
`proId` int(11) NOT NULL,
CONSTRAINT `categoryId` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ,
`proName` varchar(100) DEFAULT NULL,
`proPath` varchar(90) DEFAULT NULL,
`proPrice` float DEFAULT NULL,
`proQuantity` decimal(38,0) DEFAULT NULL,
`proImage` varchar(100) DEFAULT NULL,
`PostDate` varchar(50) NOT NULL,
`status` int(11) NOT NULL DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
I'm having a bit of a strange problem, I'm trying to add a foreign key
to one table that references another, but it is failing for some
reason. With my limited knowledge of MySQL, the only thing that could
possibly be suspect is that there is a foreign key on a different
table referencing the one I am trying to reference.
Please help me out as soon as possible .
Thanks in advance
You should create the categoryId column before assigning it in a foreign key,
categoryId int(11) NOT NULL,
CREATE TABLE IF NOT EXISTS `products` (
`proId` INT(11) NOT NULL,
`categoryId` INT ,
`proName` VARCHAR(100) DEFAULT NULL,
`proPath` VARCHAR(90) DEFAULT NULL,
`proPrice` FLOAT DEFAULT NULL,
`proQuantity` DECIMAL(38,0) DEFAULT NULL,
`proImage` VARCHAR(100) DEFAULT NULL,
`PostDate` VARCHAR(50) NOT NULL,
`status` INT(11) NOT NULL DEFAULT '1',
FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`)
) ENGINE=INNODB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

Field not inserting or updating , int type in sql

I am working on magento platform.I face a problem regarding values insertion to specific field: My query run perfect but one specific column not working for any query.I try my best but didn't find why .When i change the column type from int to varchar type it works.This is my table structure.
CREATE TABLE `followupemails_emaillogs` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
the "followupemails_id" column not working in insert and update query.This is one update query where record exist that id(29). UPDATE followupemails_emaillogs SET followupemails_id=5 WHERE id =29.
This is insertion query INSERT INTO followupemails_emaillogs SET followupemails_id=4, schedule_time='2013-10-23 08:10:00', email_status='pending', client_name='ayaz ali'.this works fine on fiddle but not on my sqlyog ? what could be the issue.At last i find query that work perfect
.INSERT INTO followupemails_emaillogs (followupemails_id,schedule_time,email_status,client_name,client_email) VALUES (26,'2013-10-23 08:10:00','pending','ayaz ali','mamhmood#yahoo.com');
Can anyone tell me why set query not working but second query works perfect.so that i can accept his answer.Thanks for all your help
Try like this
To Create,
CREATE TABLE followupemails_emaillogs (
id int(8) NOT NULL AUTO_INCREMENT PRIMARY KEY,
schedule_time datetime DEFAULT NULL,
sent_time datetime DEFAULT NULL,
email_status varchar(100) DEFAULT NULL,
client_name varchar(250) DEFAULT NULL,
client_email varchar(250) DEFAULT NULL,
followupemails_i int(11) DEFAULT NULL,
UNIQUE (id)
)
To Insert,
INSERT INTO followupemails_emaillogs (schedule_time,sent_time,email_status,client_name,client_email,followupemails_i)
VALUES
('2012-05-05','2012-05-06',"sent","sagar","sagar#xxxx.com",2)
the whole query is ok
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.
but at the last there is dot which is actually error so remove the dot and create the table
latin1.
so remove the dot sign and not null in id filed use this line by default fields are null so don't use default null
id int (8) AUTO_INCREMENT
CREATE TABLE `followupemails_emaillogs` (
`id` int (8) AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100),
`client_name` varchar(250),
`client_email` varchar(250),
`followupemails_id` int,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1
don't need (11) in the sql query for int operator , This get for length of the nvarchar,varchar datatype column only not a int datatype,So change and write int instead of int(11) and int(8)
Try this query instead of your query
CREATE TABLE `followupemails_emaillogs` (
`id` int NOT NULL AUTO_INCREMENT,
`schedule_time` datetime DEFAULT NULL,
`sent_time` datetime DEFAULT NULL,
`email_status` varchar(100) DEFAULT NULL,
`client_name` varchar(250) DEFAULT NULL,
`client_email` varchar(250) DEFAULT NULL,
`followupemails_id` int DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=latin1.

Unable to create table from query

I have the following query
CREATE TABLE grades_gra (
id_gra INT(11) NOT NULL AUTO_INCREMENT,
identifier_gra VARCHAR(2) DEFAULT NULL,
name_gra VARCHAR(250) DEFAULT NULL
PRIMARY KEY (id_gra)
)
ENGINE = INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=UTF8;
When I execute it, it gives me an error invalid default vale for 'naes_gra'
You forgot a comma before primary key:
CREATE TABLE grades_gra (
id_gra INT(11) NOT NULL AUTO_INCREMENT,
identifier_gra VARCHAR(2) DEFAULT NULL,
name_gra VARCHAR(250) DEFAULT NULL,
PRIMARY KEY (id_gra)
)
ENGINE = INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=UTF8;
SQLFiddle demo
Put the comma after DEFAULT NULL, before PRIMARY keyword

3 tables or 1 table?

CREATE TABLE IF NOT EXISTS `domains` (
`domains_id` bigint(15) NOT NULL AUTO_INCREMENT,
`domains_url` varchar(255) NOT NULL,
PRIMARY KEY (`domains_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `domains_actions` (
`domains_actions_id` int(15) NOT NULL AUTO_INCREMENT,
`domains_actions_selmgec` int(15) NOT NULL,
`domains_id` int(15) NOT NULL,
`domains_actions_member` int(15) NOT NULL,
`domains_actions_date` date NOT NULL,
`actions_id` int(2) NOT NULL
`domains_actions_value` int(15) NOT NULL,
PRIMARY KEY (`domains_actions_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `actions` (
`actions_id` int(15) NOT NULL AUTO_INCREMENT,
`actions_action` varchar(15) NOT NULL,
PRIMARY KEY (`actions_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
So if I have read properly, I need an actions tables for the likes/new/social then this is linked into domains_actions.
Not sure what you're trying to do with these, but they obviously have repeating data. You should consolidate into one table with a type column; perhaps using ENUM.
CREATE TABLE IF NOT EXISTS `domains` (
`domains_id` int(15) NOT NULL AUTO_INCREMENT,
`domains_selmgec` int(15) NOT NULL,
`domains_domain` int(15) NOT NULL,
`domains_member` int(15) NOT NULL,
`domains_date` date NOT NULL,
`domains_type` int(2) NOT NULL,
`type` ENUM('likes', 'new', 'social'),
PRIMARY KEY (`domains_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
A more relational and normalized schema would look something like:
CREATE TABLE IF NOT EXISTS `domain` (
`id` int(15) NOT NULL AUTO_INCREMENT,
`selmgec` int(15) NOT NULL,
`domain` int(15) NOT NULL,
`member` int(15) NOT NULL,
`date` date NOT NULL,
`type` int(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `type` (
`id` tinyint(1) NOT NULL AUTO_INCREMENT,
`type` varchar(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `domainType` (
`domain_id` int(15) NOT NULL,
`type_id` tinyint(1) NOT NULL,
PRIMARY KEY (`domain_id`, `type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
This design will allow you hold a single domain and assign it multiple types. You would need to change the engine to InnoDb and create foreign key constrains to enforce these types.
See the demo
Without knowing all of the details, I would create one table and then a TypeId column that references another table:
CREATE TABLE IF NOT EXISTS `domains` (
`domains_id` int(15) NOT NULL AUTO_INCREMENT,
`domains_selmgec` int(15) NOT NULL,
`domains_domain` int(15) NOT NULL,
`domains_member` int(15) NOT NULL,
`domains_date` date NOT NULL,
`domains_type` int(2) NOT NULL,
`type_id` int(2) NOT NULL,
PRIMARY KEY (`domains_likes_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `types` (
`type_id` int(15) NOT NULL AUTO_INCREMENT,
`type_name` varchar(15) NOT NULL,
PRIMARY KEY (`type_id`)
) ;