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)";
Related
How to use if not exists in MySQL database.
create schema if not exists appkasir;
use appkasir;
create table if not exists admin (
id int(11) not null auto_increment,
nama varchar(20) not null,
no_telp varchar(20) null,
alamat varchar(50) null,
email varchar(50) null,
primary key (id)
);
You give no congret error message. I think it's not an error of if exists but maybe of the primary key.
Look at MySQL reference.
Try:
create schema if not exists appkasir;
use appkasir;
create table if not exists admin (
id int(11) not null auto_increment primary key,
nama varchar(20) not null,
no_telp varchar(20) null,
alamat varchar(50) null,
email varchar(50) null
);
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 ?
I have it set up to echo when my tables create but the users table just won't, I've looked at multiple solutions but none of them has helped here my code
<?
phpinclude_once("index.php");
$tbl_users = "CREATE TABLE IF NOT EXISTS users
(
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL,
email VARCHAR(64) NOT NULL,
password VARCHAR(64) NOT NULL,
gender ENUM('m','f') NOT NULL,
website VARCHAR(64) NULL,
country VARCHAR(64) NULL,
userlevel ENUM('a','b','c','d') NOT NULL DEFAULT a,
avatar VARCHAR(64) NULL,
ip VARCHAR(64) NOT NULL,
signup DATETIME NOT NULL,
lastlogin DATETIME NOT NULL,
notescheck DATETIME NOT NULL,
activated ENUM('0','1') NOT NULL DEFAULT 0,
PRIMARY KEY (id)),
UNIQUE KEY username (username,email))";
$query = mysqli_query($db_spooky, $tbl_users);
if ($query === TRUE)
{
echo "<h3>user table created OK :) </h3>";
}
else
{
echo "<h3>user table NOT created :( </h3>";
}
?>
CREATE TABLE IF NOT EXISTS users (id INT(11) NOT NULL AUTO_INCREMENT
,username VARCHAR(16) NOT NULL
,email VARCHAR(64) NOT NULL,password VARCHAR(64) NOT NULL
,gender ENUM('m','f') NOT NULL
,website VARCHAR(64) NULL
,country VARCHAR(64) NULL
,userlevel ENUM('a','b','c','d') NOT NULL DEFAULT 'a'
,avatar VARCHAR(64) NULL
,ip VARCHAR(64) NOT NULL
,signup DATETIME NOT NULL
,lastlogin DATETIME NOT NULL
,notescheck DATETIME NOT NULL,activated ENUM('0','1') NOT NULL DEFAULT 0
,PRIMARY KEY (id)
,UNIQUE KEY username (username,email))
Learn to organize your code instead of writing it in one long line for one, and two, ENUM default for a needs to be in single quotes and you had an extra closing bracket. The code above is the corrected version.
I have a table Members with column DayJoin of type varchar. I want to set default value of the column DayJoin should be the current day if user does not enter a value for that field.
In MSSQL Server I wrote this statement:
CREATE TABLE Members
(
ID int identity (1,1) primary key,
Email varchar(50),
Password Varchar(50),
Role Varchar(50) default 'Member',
DayJoin varchar(50) DEFAULT Convert(varchar, GETDATE(),103),
LastLogin varchar(50)DEFAULT Convert(varchar, GETDATE(),103),
FName varchar(50),
LName varchar(50),
Phone varchar(50),
DOB varchar(50),
Gender varchar(50),
IDcard varchar(50),
Address varchar(50),
City varchar(50),
Job varchar(50),
Avatar varchar(50) default 'images/noavatar.gif'
)
The statement gets the value of the current day and formats it as (dd/mm/yyyy). But when I run a similar one in mySQL, it's not working
CREATE TABLE `NewTable` (
`ID` int UNSIGNED NULL AUTO_INCREMENT ,
`Email` varchar(50) NULL ,
`Password` varchar(50) NULL DEFAULT '' ,
`Role` varchar(10) NULL ,
`DayJoin` varchar(10) NULL DEFAULT 'CURRENT_TIMESTAMP' ,
`LastLogin` varchar(10) NULL DEFAULT 'date_format(now(),%Y/%m/%d)' ,
`Firstname` varchar(50) NULL ,
`Lastname` varchar(50) NULL ,
`Phone` varchar(20) NULL ,
`DOB` varchar(10) NULL ,
`Gender` varchar(20) NULL ,
`IDcard` varchar(20) NULL ,
`Address` varchar(50) NULL ,
`City` varchar(50) NULL ,
`Job` varchar(50) NULL ,
`Avatar` varchar(50) NULL DEFAULT 'images/noavatar.gif' ,
PRIMARY KEY (`ID`)
)
ENGINE=ARCHIVE
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_unicode_ci
;
I get a response saying something like; "default value in error"
Does anyone know what the problem is?
Use DATETIME type instead:
`DayJoin` DATETIME DEFAULT CURRENT_TIMESTAMP
remove the quotes around 'CURRENT_TIMESTAMP' in your code and use TIMESTAMP data type. It should read like:
`DayJoin` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
It will give error as "Invalid default value for 'DayJoin' "
Use datetime or timestamp datatype for 'DayJoin'
`DayJoin` TimeStamp NULL DEFAULT 'CURRENT_TIMESTAMP' ,
Try this change your DayJoin datatype
`DayJoin` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
or
`DayJoin` TimeStamp NULL DEFAULT CURRENT_TIMESTAMP
I am creating three tables and when trying to tie the primary and foreign keys I am receiving the error message "Key column 'username' doesn't exist in table".
Could someone take a look at my code and tell me what I am doing wrong? I have tried dropping the database and revamped the tables a few times but I am still getting the same message. Here is my code, thank you in advance for any help!
create database testproject
use testproject
create table caller_info
(
caller_id int(11) unsigned auto_increment primary key not null,
first_name varchar(35) not null,
Last_name varchar(35) not null,
phone_number int(25) not null
);
create table caller_call_record
(
call_record_id int(11),
Call_Description varchar(50),
franchise_id int(10) not null,
email varchar(40) not null,
username varchar(25) primary key not null
);
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) not null,
PRIMARY KEY(call_escalation_id),
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY(username) REFERENCES caller_call_record(username)
);
As pointed out, your table caller_escalation needs a column called username in order to create the foreign key.
It sounds like once you've added that, you're now getting a Cannot add foreign key constraint error from MySQL. The first thing that comes to mind with this kind of error is that you're using the wrong engine for your tables. You are most likely using MyISAM which does not support foreign key references - in order to use these, you will need to change the engine on all of your tables to InnoDB.
try this:
create table caller_call_record
(
call_record_id int(11),
Call_Description varchar(50),
franchise_id int(10) not null,
email varchar(40) not null,
username varchar(25) not null,
PRIMARY KEY (username)
);
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) not null,
username varchar(25) not null,
PRIMARY KEY(call_escalation_id,username),
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY(username) REFERENCES caller_call_record
);
The table caller_escalation also needs a column username, which is not there
create table caller_escalation
(
call_escalation_id int(11) unsigned auto_increment not null,
Second_Level varchar(5) not null,
caller_id int(11) unsigned not null, ;; <--- added unsigned type here
PRIMARY KEY(call_escalation_id),
username varchar(25) not null, ;; <--- added field here
FOREIGN KEY(caller_id)
REFERENCES caller_info(caller_id),
FOREIGN KEY (username) REFERENCES caller_call_record (username)
);
Also ensure the column data types are the same in both tables. You have "caller_id int(11) unsigned" in caller_info and "caller_id int(11)" in caller_escalation. I added the unsigned specifier above to make it work.