Create table mysql - php

I'am new to mysql and i want to know what KEY (not primary key) means in the query below :
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`username` varchar(150) NOT NULL default '',
`email` varchar(100) NOT NULL default '',
`password` varchar(100) NOT NULL default '',
`activation` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `idx_name` (`name`),
KEY `username` (`username`),
KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=65 ;
also this line ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=65 ;

A KEY is an index: it's just like a library index: quicker finding for the values of that colum. You want this for joining and searching.
ENGINE=MyISAM
Means the engine you use (this is the default). If you need foreign keys for example, then you might want InnoDB.
DEFAULT CHARSET=utf8
Is the default character set.
AUTO_INCREMENT=65 ;
Means that currently the auto-increment value is at 65.

KEY is a synonym for INDEX. See database index if you're not familiar with them.

KEY is normally a synonym for INDEX.
ENGINE=MyISAM
It means the storage engine for your table. There are two types of storage engines in MySQL: transactional and non-transactional. The default engine is InnoDB as of MySQL 5.5.5 ( MyISAM before 5.5.5).
DEFAULT CHARSET=utf8
Character set support that enables you to store data using a variety of character sets.
AUTO_INCREMENT=65
It means currently the value of auto_increment is 65 for primary key id.
More information:-
https://dev.mysql.com/doc/refman/5.7/en/create-table.html

Related

Unable to create a table in database

This is the query I have used for creating the table
CREATE TABLE IF NOT EXISTS `logging_api_request_js` (
`id` int(30) unsigned NOT NULL AUTO_INCREMENT,
`log_message` longtext,
`level` varchar(10) DEFAULT NULL,
`ip_address_merchant` varchar(45) DEFAULT NULL,
`ip_address_customer` varchar(45) DEFAULT NULL,
`creationTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updateTime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11
It showed an error Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
When I googled I saw that in the mysql 5.6. version this issue / restriction has been took off.But i wont be able to upgrade mysql right now.
Is there any work around for this,whith out changing the table structure to dump this using mysql?Please help.Im having little knowledge about the db operations.Do help.Thanks
You could remove ON UPDATE CURRENT_TIMESTAMP and create trigger.
DROP TRIGGER IF EXISTS `update_logging_api_request_js`;
DELIMITER //
CREATE TRIGGER `update_logging_api_request_js` BEFORE UPDATE ON `logging_api_request_js`
FOR EACH ROW BEGIN
SET NEW.updateTime = NEW.creationTime;
END
//
DELIMITER ;

Foreign Key constraints in MySQL

I am a newbie to PHP. I have been given the code snippet below as homework:
CREATE TABLE `admin_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`statusdate` DATETIME DEFAULT NULL,
`type` INT(11) DEFAULT NULL
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin
Why will it not be possible to set up any foreign key constraints using this table?
I have done some research on Google and I cant find a reason why foreign key constraints are not possible. Please help
You can do that like this :
CREATE TABLE `admin_log` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`statusdate` DATETIME DEFAULT NULL,
`type` INT(11) DEFAULT NULL,
INDEX (type),
FOREIGN KEY (type)
REFERENCES type(id)
ON UPDATE CASCADE ON DELETE RESTRICT,
PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=latin
Notice the engine INNODB insetad of MYISAM which doesn't permit foreign key.
Or using MySQLAdmin in the "structure" tab, click on the "relationals view" link below the table description.
Even it has been mentioned on comment before -- just to make it more prominent: MyISAM is not supporting foreign keys. So you will need to change the engine of your table to e.g. INNODB if possible.

MySQL: errno(150), cannot create table (related to foreign keys)

I want to use cascading in my project to simplify certain processes. I created these two queries as well as a couple more but all child tables throw the same error on execution. They worked when I used MyISAM as the engine, but on further testing and research I figured that it does not support cascading, so I switched the engine to InnoDB which triggered these errors. I've looked at a couple of forums and threads with the same problem but I just can't seem to figure out where the actual problem lies. Can someone help?
ParentTable:
CREATE TABLE IF NOT EXISTS `branches` ( `branch_id` int(11) NOT NULL AUTO_INCREMENT, `key` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `name` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `short_name` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `city` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `timezone` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, UNIQUE(`key`), PRIMARY KEY (`branch_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
Child Table:
CREATE TABLE IF NOT EXISTS `files` ( `file_id` int(11) NOT NULL AUTO_INCREMENT, `branch_id` int(11) NOT NULL, `path` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL, `use_google_analytics` BOOLEAN NOT NULL, FOREIGN KEY(`branch_id`) REFERENCES `branches`(`branch_id`) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (`file_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
Error:
1005: Can't create table 'files' (errno: 150)
You defined your foreign key
ON DELETE CASCADE
But your branch_id is defined as NOT NULL. That won't work.
You want your branch_id never be null but then the foreign key related data gets removed you want it set to NULL with the CASCADE option.
So either change removd the ON DELETE CASCADE or allow NULL in the foreign key. This works
SQLFiddle demo

AUTO_INCREMENT in two places

I came across following sql statements and you can see that AUTO_INCREMENT is in two different places. Can you explain the different, I know the first one is auto incrementing id. But what does the second one mean?
CREATE TABLE `categories`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`image_path` varchar(200) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB;
Second statement.
CREATE TABLE `categories`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`image_path` varchar(200) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT=4 ;
I referenced http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html. But I couldn't find anything.
The AUTO_INCREMENT in the second statement sets the first number to be used in the id at 4.
`id` int(11) NOT NULL AUTO_INCREMENT
Sets the column name and tells the DB to auto increment the number when a new row is added.
) ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT=4 ;
Sets the Engine used for the table, the charset and that it should start numbering at 4, not 1.
CREATE TABLE explains this in a bit more detail.
AUTO_INCREMENT
The initial AUTO_INCREMENT value for the table. In MySQL 5.0, this
works for MyISAM and MEMORY tables. It is also supported for InnoDB as
of MySQL 5.0.3.

ON DELETE CASCADE not working in MySQL

I am using the following SQL to create a table named app_info:
CREATE TABLE IF NOT EXISTS `app_info` (
`_id` int(11) NOT NULL AUTO_INCREMENT,
`app_name` varchar(50) DEFAULT NULL,
`app_owner` varchar(50) DEFAULT NULL,
`last_update` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
I am using the following SQL to create a table named tab_info:
CREATE TABLE `myDB`.`tab_info` (
`_id` INT NOT NULL AUTO_INCREMENT ,
`app_id` INT NOT NULL ,
`tab_title` VARCHAR(15) NOT NULL ,
PRIMARY KEY (`_id`) ,
UNIQUE INDEX `app_id_UNIQUE` (`app_id` ASC) ,
INDEX `app_tab_key` (`app_id` ASC) ,
CONSTRAINT `app_tab_key`
FOREIGN KEY (`app_id` )
REFERENCES `myDB`.`app_info` (`_id` )
ON DELETE CASCADE
ON UPDATE CASCADE);
But when I delete data from primary key table, the orphaned rows in the foreign key table are not being deleted automatically. Does anyone know what the problem could be?
The MyISAM storage engine doesn't support foreign key constraints. The constraint is parsed but silently ignored.
To fix your problem use the InnoDB engine instead (for both tables).
CREATE TABLE ( ... ) ENGINE = InnoDB ... ;
Instead of dropping your tables and recreating them you can also change the storage engine:
ALTER TABLE myDB.app_info ENGINE = InnoDB;
ALTER TABLE myDB.tab_info ENGINE = InnoDB;
After changing the engine you will need to add the foreign key constraint again.

Categories