PHP: code to create mysql table with default value - php

I am trying to create a table and set default values at the same time. Is it possible? When I create a table without default values, it is working fine. But when I try to add default values, it does not work. Please point out the error.
mysql_query("CREATE TABLE test
(
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
test_id INT,
pwd VARCHAR(50),
cost FLOAT(5,2) NOT NULL DEFAULT '0.00',
status VARCHAR(15) NOT NULL DEFAULT 'on',
code INT NOT NULL DEFAULT '0',
scene VARCHAR(50) NOT NULL DEFAULT '0,0,0,0,0'
) TYPE=innoDB");

Related

create primary key based on date+month+some text like (kcs/nov-18/0001)

I have a database table for track some kind of data, you can check my table code below.
CREATE TABLE CaseTracker(
CTID CHAR(15) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
Status CHAR(10) NOT NULL,
CreatedBy CHAR(15) NOT NULL,
CreatedOn CHAR(15) NOT NULL,
UpdatedBy CHAR(15),
UpdatedOn CHAR(15),
IPAddress CHAR(20) NOT NULL,
)
And In This Website when i insert any data its done successfully, but right now my PRIMARY KEY default value is like (1, 2, 3, 4...), it's Set to AUTO_INCREMENT but i want to change my primary key value with trackcode/moth-date/AUTO_INCREMENT value Like kcs/nov-12/001 in next moth its change with kcs/dec-01/090
is there any idea or help for me ?

How to speed up slow MySQL UPDATE queries with InnoDB tables

I have a very simple MySQL update query on my InnoDB table.
UPDATE `players_teams` SET t_last_active=NOW() WHERE t_player_id=11225 AND t_team_id=6912 AND t_season_id=2002 LIMIT 1
My table is structured as so:
CREATE TABLE `players_teams` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`t_player_id` int(11) DEFAULT NULL,
`t_team_id` int(11) DEFAULT NULL,
`t_league_id` int(11) DEFAULT NULL,
`t_season_id` int(11) DEFAULT NULL,
`t_div` varchar(64) DEFAULT NULL,
`t_player_number` varchar(3) DEFAULT NULL,
`t_player_jersey_size` enum('UNKNOWN','XS','S','M','L','XL','XXL','XXXL') DEFAULT 'UNKNOWN',
`t_player_registration_number` varchar(64) DEFAULT NULL,
`t_player_class` enum('ROSTER','SPARE','COACH','INJURED','HOLIDAY','SUSPENDED','SCOREKEEPER') DEFAULT 'ROSTER',
`t_access_level` enum('PLAYER','MANAGER','ASSISTANT') DEFAULT 'PLAYER',
`t_player_position` enum('ANY','FORWARD','DEFENCE','GOALIE','PITCHER','CATCHER','FIRST BASE','SECOND BASE','THIRD BASE','SHORTSTOP','LEFT FIELD','CENTER FIELD','RIGHT FIELD') DEFAULT 'ANY',
`t_spare_status` enum('INVITED','IN','OUT') DEFAULT NULL,
`t_drink_next` int(1) DEFAULT '0',
`t_no_fees` tinyint(1) DEFAULT '0',
`t_no_drink` tinyint(1) DEFAULT '0',
`t_auto_check_in` tinyint(1) DEFAULT '0',
`t_print_reminder` tinyint(1) DEFAULT '0',
`t_notes` text,
`t_last_chatter_id` int(11) DEFAULT NULL,
`t_last_history_id` int(11) DEFAULT NULL,
`t_last_active` timestamp NULL DEFAULT NULL,
`t_status` enum('ACTIVE','INACTIVE','ARCHIVED') DEFAULT 'ACTIVE',
`t_added` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `t_player_id` (`t_player_id`),
KEY `t_team_id` (`t_team_id`),
KEY `t_season_id` (`t_season_id`),
KEY `t_player_id_2` (`t_player_id`,`t_team_id`,`t_season_id`),
KEY `Team/Player ID` (`t_team_id`,`t_player_id`),
KEY `UpdatePlayersDiv` (`t_team_id`,`t_season_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23454 DEFAULT CHARSET=latin1;
This simple update query takes on average, 3.5 seconds? I'm running this on a MediaTemple MySQL GRID container.
Here is the result of an EXPLAIN when switching the UPDATE to a SELECT.
Can someone provide some insight on how I'm not doing this correctly?
[EDIT: Added list of Indexes]
So is this a big mess of indexes? Do I have a bunch of redundant indexes in here?
Cheers,
Jon
It's using the wrong key instead of the index on the 3 columns. You can hint at indexes with USE KEY ... syntax.
https://dev.mysql.com/doc/refman/5.1/en/index-hints.html
You may also want to try reordering your key on the 3 columns. Generally, you want the most restricting column to be first in the index, then the next most restricting and so on.

Cant "give" null value in a field of my sql table phpmyadmin

Im using phpmyadmin and I have a table categories with 4 fields:
-> id
-> id_father_category (I put this with predefined: NULL) and I also put a checkbox Null
-> name
-> content
I want to give null values to my id_father_category field.
But Im having this error:
Warning: #1366 Incorrect integer value: '' for column 'id_father_category field' at row 1
And my field id_father_category stays automatically with value 0, and I dont want that.
Somebody there knows how I can solve this problem?
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Hope this will solve your problem...
This is an addition to #Nirjhor's answer.
ALTER TABLE categories MODIFY id_father_category INT(11) DEFAULT NULL;
or you can recreate the whole thing:
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`id_father_category` int(11) DEFAULT NULL,
`name` varchar(64) NOT NULL,
`content` varchar(256) NOT NULL,
PRIMARY KEY (`id`) )
ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Setting the default value to NULL will handle your issue as well as set the field to NULL. One thing to note here is to make sure you don't have some weird index on that field cause that can also create an issue.

Mysql inserting id as 0

I am trying to make a revisions page, but when I insert a new revision, it sets the id to 0 instead of 1 more than the last entry, so I can make 1 new revision, but no more. Here is my mySQL code:
("INSERT INTO `revisions` (version, author, content, `date`) VALUES ('".$version."', '".$_SESSION['username']."', '".$content."', '".$date."')");
Edit:
Wow that was embarrassing... I made the table id default 0...
Schema:
CREATE TABLE `revisions` (
`id` int(11) NOT NULL,
`version` varchar(255) NOT NULL,
`author` varchar(255) NOT NULL,
`content` varchar(255) NOT NULL,
`status` int(255) NOT NULL,
`date` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
:) it saids clearly
id int(11) NOT NULL DEFAULT '0'
it will be inserted as default value 0 if you dont make any value , then it will be 0 as default.
if you wanna be 1 as default then use this
id int(11) NOT NULL DEFAULT '1'
or if you want it be incremented then use this
id int(11) NOT NULL AUTO_INCREMENT
//this will be automatically 1,2,3,4,...values incremented everytime you inseret new value
CREATE TABLE `revisions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
......
you need to set id with auto increment
check out the mysql documentation: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
You should be adding AUTO_INCREMENT to your id instead. That way, it'll just increment automatically (as the name suggests) each time you insert a row.
CREATE TABLE `revisions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
...

Auto_increment stops script from creating table

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)";

Categories