Im just testing things out but my first code didnt work.
CREATE TABLE `Accounts`.`Registry` ( `Username` VARCHAR(16) NOT NULL ,
`Password` VARCHAR(16) NOT NULL ,
`First Name` INT(20) NOT NULL ,
`Last Name` INT(20) NOT NULL ,
`Gender` INT(1) NOT NULL ,
`ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`Password`, `First Name`, `Last Name`),
UNIQUE (`ID`(1000)),
UNIQUE (`Username`)) ENGINE = MyISAM;
Error
#089 - Incorrect prefix key, the used length is longer than the key part, or the storage engine doesnt support unique prefix keys
This must be the reason for the error UNIQUE (ID(1000))
Try this.
CREATE TABLE Accounts.Registry (
Username VARCHAR(16) NOT NULL ,
Password VARCHAR(16) NOT NULL ,
First Name INT(20) NOT NULL ,
Last Name INT(20) NOT NULL ,
Gender INT(1) NOT NULL ,
ID INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (Password, First Name, Last Name),
UNIQUE (ID),
UNIQUE (Username)) ENGINE = MyISAM;
As already mentioned in comment and by others remove UNIQUE (`ID` (1000)).
For better performance, create a table asInnodb and other useful info shared below
Int stores values as numbers with a range from -2147483648 to 2147483647.
First Name & Last Name change as VARCHAR(20).
Gender you can change as TINYINT.
CREATE TABLE `Accounts`.`Registry` (
`ID` INT NOT NULL AUTO_INCREMENT,
`Username` VARCHAR(16) NOT NULL,
`Password` VARCHAR(16) NOT NULL,
`First Name` INT(20) NOT NULL,
`Last Name` INT(20) NOT NULL,
`Gender` TINYINT(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE (`Username`)
) ENGINE=Innodb;
Related
CREATE TABLE IF NOT EXISTS customers_data (
id int(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
INVOICE_ID varchar(50) NOT NULL,
_NAME varchar(255) NOT NULL,
MOBILE bigint(12) NOT NULL,
GSTIN varchar(20) NOT NULL,
_ADDRESS varchar(255) NOT NULL,
EMAIL varchar(255) NOT NULL,
_STATE varchar(50) NOT NULL,
MODE varchar(10) NOT NULL,
_DATE date NOT NULL DEFAULT current_timestamp(),
total_qty int(11) NOT NULL,
total_amount decimal(40,2) NOT NULL,
total_sc_gst decimal(30,2) NOT NULL,
Round_Off float(2,2) NOT NULL,
grand_total decimal(50,0) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
customer_data created sucessfully.
But there is an error in order_data regarding foreign key. Remember I want to create these two table in a same time.
CREATE TABLE IF NOT EXISTS order_data (
id int(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
cus_id int(20) NOT NULL,
ITEM varchar(255) NOT NULL,
HSN varchar(50) NOT NULL,
QTY int(15) NOT NULL,
RATE decimal(40,2) NOT NULL,
S_C_GST decimal(30,2) NOT NULL,
TOTAL decimal(50,2) NOT NULL,
_DATE timestamp NOT NULL DEFAULT current_timestamp(),
FOREIGN KEY(cus_id) REFERENCES customers_data(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
I got this error.
#1005 - Can't create table test. (errno: 150 "Foreign
key constraint is incorrectly formed")
I think you are using MySQL. You have not used Constraint with Foreign Key. Try the below query.
CREATE TABLE IF NOT EXISTS order_data (
id int(20) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
cus_id int(20) NOT NULL,
ITEM varchar(255) NOT NULL,
HSN varchar(50) NOT NULL,
QTY int(15) NOT NULL,
RATE decimal(40,2) NOT NULL,
S_C_GST decimal(30,2) NOT NULL,
TOTAL decimal(50,2) NOT NULL,
_DATE timestamp NOT NULL DEFAULT current_timestamp(),
**CONSTRAINT <FK_NAME>** FOREIGN KEY(cus_id) REFERENCES customers_data(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Making the table "at the same time" is not the problem you need to solve, (it's not possible anyway).
You can't make a foreign key if the data type is different from the data type of the referenced column.
order_data.cus_id is an INT column.
customers_data.id is an INT UNSIGNED column.
Change either one or the other data type, so they are the same.
I'm trying to add username from my st_accounts column into my table results using mySQL.
st_accounts table
`id` int(11) NOT NULL, //Primary Key
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
`img` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
results table
`score_ID` int(11) NOT NULL, //Primary key
`score` int(20) NOT NULL,
`date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I think you want:
create table accounts (
account_id int auto_increment primary key,
. . .
);
create table results (
result_id int auto_increment primary key,
account_id int not null
score int NOT NULL,
score_date date NOT NULL,
constraint fk_results_accounts foreig key (accounts_id) references accounts(account_id)
) ;
Notes:
Declare the primary key explicitly, not in a comment.
My convention is to name the primary key after the name of the table (in singular) with _id after it.
The foreign key has the same name as the primary key -- self documenting.
I made the integer primary keys auto_increment, so the database can assign unique values.
The following recordset in Dreamweaver throws a 1052 ambiguous error every time I attempt to test it. I know it has something to do with the dateADDED, but don't know how to fix it.
SELECT commentID, commentTitle, commentContent, topicTable.topicTitle, DAYNAME(dateADDED) as day, MONTHNAME(dateADDED) as month,
DAY(dateADDED) as date, YEAR(dateADDED) as year
FROM commentTable, topicTable
WHERE commentID = colname AND topicTable.topidID = commentTable.topicID
Here is the layout of the tables,
CREATE TABLE userTable
(
userID VARCHAR(15) NOT NULL,
screenName VARCHAR(15) NOT NULL UNIQUE,
userPasswd CHAR(40) NOT NULL,
firstName VARCHAR(15) NOT NULL,
lastName VARCHAR(25) NOT NULL,
dateJoined TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
lastlogin DATETIME,
PRIMARY KEY(userID)
)
;
CREATE TABLE categoryTable
(
categoryID MEDIUMINT AUTO_INCREMENT NOT NULL,
categoryName VARCHAR(30) NOT NULL,
categoryDescription VARCHAR(200) NOT NULL,
PRIMARY KEY (categoryID)
)
;
CREATE TABLE topicTable
(
topicID MEDIUMINT AUTO_INCREMENT NOT NULL,
topicTitle VARCHAR(30) NOT NULL,
userID VARCHAR(15) NOT NULL,
dateAdded TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
categoryID MEDIUMINT NOT NULL,
PRIMARY KEY (topicID)
)
;
CREATE TABLE commentTable
(
commentID MEDIUMINT AUTO_INCREMENT NOT NULL,
commentTitle VARCHAR(30) NOT NULL,
commentContent TEXT NOT NULL,
userID VARCHAR(15) NOT NULL,
dateAdded TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
topicID INT NOT NULL,
PRIMARY KEY (commentID)
)
;
dateADDED is represented in both tables, so you should choose from which one you want it to be in the result set :
SELECT ct.commentID
, ct.commentTitle
, ct.commentContent
, tt.topicTitle
, DAYNAME(ct.dateADDED) as `day`
, MONTHNAME(ct.dateADDED) as `month`
, DAY(ct.dateADDED) as `date`
, YEAR(ct.dateADDED) as `year`
FROM commentTable ct
JOIN topicTable tt ON ct.commentID = tt.colname AND tt.topidID = ct.topicID
By the way, still wonder, what colname stands for, maybe it should be ct.userID = tt.userID instead?
I am using MySQL to create a small database. and facing some issues in foreign keys and primary key. I really don't understand as it seems a simple problem.
CREATE TABLE IF NOT EXISTS `db_trimms`.`urban_area` (
`area_id` INT(10) NOT NULL ,
`city` VARCHAR(60) NOT NULL ,
`state` VARCHAR(60) NOT NULL ,
`urban_area` VARCHAR(60) NOT NULL ,
`census_region` VARCHAR(60) NOT NULL ,
`area_no` VARCHAR(60) NOT NULL ,
`freeway_speed` VARCHAR(60) NOT NULL ,
`arterial_speed` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
its running successfully. But while create another table and creating foreign key referring to the area_id of the above table...its creating issues...
#1005 - Can't create table 'db_trimms.emiss_others_offpeak' (errno: 150) (Details...)
and here is the query
CREATE TABLE IF NOT EXISTS `db_trimms`.`emiss_others_offpeak` (
`area_id` INT(10) UNSIGNED NOT NULL ,
`ammonia` VARCHAR(60) NOT NULL ,
`atm_carbon_dio` VARCHAR(60) NOT NULL ,
`carbon_dio_equiv` VARCHAR(60) NOT NULL ,
`carbon_mono` VARCHAR(60) NOT NULL ,
`methane` VARCHAR(60) NOT NULL ,
`nitrogen_dio` VARCHAR(60) NOT NULL ,
`nitrogen_oxide` VARCHAR(60) NOT NULL ,
`nitrous_oxide` VARCHAR(60) NOT NULL ,
`non_meth_hydrocarbs` VARCHAR(60) NOT NULL ,
`oxides_of_nitrogen` VARCHAR(60) NOT NULL ,
`particulate_matter_pm10` VARCHAR(60) NOT NULL ,
`particulate_matter_pm2_5` VARCHAR(60) NOT NULL ,
`sulfate` VARCHAR(60) NOT NULL ,
` sulfur_dioxide` VARCHAR(60) NOT NULL ,
`total_hydrocarbon` VARCHAR(60) NOT NULL ,
`vol_org_comp` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`area_id`) ,
CONSTRAINT `fk_others_offpeak_urban`
FOREIGN KEY (`area_id` )
REFERENCES `db_trimms`.`urban_area` (`area_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
The data types are incompatible with each other. Make column area_id from table urban_area also UNSIGNED.
`area_id` INT(10) UNSIGNED NOT NULL ,
SQLFiddle Demo
It's a simple CREATE TABLE statement that I'm writing in PHPMyAdmin. Ok, I know I could just do it the easy way in PHPMyAdmin but I like to have full control. Here's the statement:
CREATE TABLE profile
(
id int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
type int(1) NOT NULL,
view int(1) NOT NULL default '1',
ver int(1) NOT NULL default '2',
email NOT NULL varchar(32),
password NOT NULL varchar(16),
first varchar(32),
last varchar(32),
site varchar(64),
address varchar(32),
city varchar(32),
zip int,
state char(2),
country varchar(50),
about text,
datereg int(20)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
i replaced
view int(1) NOT NULL default '1',
ver int(1) NOT NULL default '2',
email NOT NULL varchar(32),
password NOT NULL varchar(16),
with
view int(1) NOT NULL default 1,
ver int(1) NOT NULL default 2,
email varchar(32) NOT NULL ,
password varchar(16) NOT NULL ,
and it worked
There are more than one mistake which makes your SQL statement not working.
Try this instead:
CREATE TABLE `profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` int(1) NOT NULL,
`view` int(1) NOT NULL DEFAULT '1',
`ver` int(1) NOT NULL DEFAULT '2',
`email` varchar(32) NOT NULL,
`password` varchar(16) NOT NULL,
`first` varchar(32),
`last` varchar(32),
`site` varchar(64),
`address` varchar(32),
`city` varchar(32) ,
`zip` int(11),
`state` char(2),
`country` varchar(50),
`about` text,
`datereg` int(20),
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
A few additions:
Try avoiding reserved words (like view, last, first, and so on) in your tables. It is possible (if
escaped properly), but it helps using none at all.
Escape your field
names and your table name properly.
Read the manual:
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
try this =)
CREATE TABLE `db`.`profile` (
`id` INT NOT NULL AUTO_INCREMENT ,
`type` INT(1) NOT NULL ,
`view` INT(1) NOT NULL DEFAULT 1 ,
`ver` INT(1) NOT NULL DEFAULT 2 ,
`email` VARCHAR(32) NULL ,
`password` VARCHAR(16) NULL ,
`first` VARCHAR(32) NULL ,
`last` VARCHAR(32) NULL ,
`site` VARCHAR(64) NULL ,
`address` VARCHAR(32) NULL ,
`city` VARCHAR(32) NULL ,
`zip` INT NULL ,
`state` CHAR(2) NULL ,
`country` VARCHAR(50) NULL ,
`about` TEXT NULL ,
`datereg` INT(20) NULL ,
PRIMARY KEY (`id`) )
ENGINE = MyISAM DEFAULT CHARSET=utf8;
view is a MySQL Keyword. If you want to use a column with the same name you must surround it with backticks like
`view` int(1) NOT NULL default 1,
Without seeing what the error is, I would say it's failing because you are trying to name a column with a MySQL reserved keyword - my guess is view
If you place backticks ` around your column names, it will allow you to use reserved words.
Just remember any other queries on that table will also need the backticks around the column names which use reserved words.
eg.
CREATE TABLE profile
(
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
`type` int(1) NOT NULL,
`view` int(1) NOT NULL default '1',
`ver` int(1) NOT NULL default '2',
`email` NOT NULL varchar(32),
`password` NOT NULL varchar(16),
`first` varchar(32),
`last` varchar(32),
`site` varchar(64),
`address` varchar(32),
`city` varchar(32),
`zip` int,
`state` char(2),
`country` varchar(50),
`about` text,
`datereg` int(20)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;