#1072 - Key column 'Name' doesn't exist in table - mysql

CREATE TABLE IF NOT EXISTS ban (
UID int(4) NOT NULL,
AdminUID int(4) NOT NULL,
Grund varchar(50) NOT NULL,
Datum varchar(50) NOT NULL,
IP varchar(50) NOT NULL DEFAULT '0',
Serial varchar(50) NOT NULL,
Eintragsdatum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
STime int(11) NOT NULL DEFAULT '0'
,
PRIMARY KEY (UID),
KEY Name (Name),
KEY IP (IP),
KEY Serial (Serial),
KEY STime (STime)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Error:
1072 - Key column 'Name' doesn't exist in table
Where is my fault?

Remove KEY Name (Name) in the code as there is not Name field in the table.
CREATE TABLE IF NOT EXISTS ban (
UID int(4) NOT NULL,
AdminUID int(4) NOT NULL,
Grund varchar(50) NOT NULL,
Datum varchar(50) NOT NULL,
IP varchar(50) NOT NULL DEFAULT '0',
Serial varchar(50) NOT NULL,
Eintragsdatum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
STime int(11) NOT NULL DEFAULT '0' , PRIMARY KEY (UID), KEY IP (IP), KEY Serial (Serial), KEY STime (STime) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Related

Create two tables customer_data and order_data in SQL in a single execution via PHP

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.

Add a foregin key to a different table

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.

Cant create a relation in mysql - Error creating foreign key on id_produk (check data types)

here is my table :
CREATE TABLE `pesanan` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ref_number` varchar(50) NOT NULL DEFAULT '',
`id_produk` int(11) NOT NULL,
`id_user` int(11) NOT NULL,
`qty` int(11) NOT NULL,
`total` varchar(50) NOT NULL DEFAULT '',
`tanggal` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;
i want to create a relation of id_produk to table produk.id and id_user to user.id, here is the other table :
CREATE TABLE `produk` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id_kategori` int(11) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT '',
`slug` varchar(100) NOT NULL DEFAULT '',
`judul` varchar(100) NOT NULL DEFAULT '',
`harga` varchar(10) DEFAULT '',
`target` int(11) DEFAULT NULL,
`desc` text,
`cover` varchar(100) DEFAULT NULL,
`tanggal` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=latin1;
and user table
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`suspended` int(11) DEFAULT NULL,
`level` varchar(11) DEFAULT NULL,
`nama` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`alamat` text,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=latin1;
when i try to relation it , it give me this error :
Error creating foreign key on id_produk (check data types)
The columns should have the exact same data type in both tables. In your case in the "produk" and "user" tables they are unsigned, which means they go from 0 to 4294967295 but in the "pesanan" table they are signed which means they go from -2147483648 to 2147483647. See more here.
If the server allowed you to create such foreign keys, it would possibly create situations where, for example, a user is added with and ID of 2147483648 that cannot ever be referenced in the "pesanan" table.
You should change the "id_produk" and "id_user" columns in the "pesanan" table to be unsigned.

Duplicate key name 'unique_id'

Here is the sql, however, there is an error says "*#1061 - Duplicate key name 'unique_id'* ", what is the problem.
create table `users`(
uid int(11) auto_increment,
unique_id varchar(23) not null unique,
name varchar(50) not null,
email varchar(100) not null unique,
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null,
PRIMARY KEY (`unique_id`),
UNIQUE KEY `uid` (`uid`),
UNIQUE KEY `unique_id` (`unique_id`),
UNIQUE KEY `email` (`email`)
)ENGINE=InnoDB AUTO_INCREMENT=877888 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
remove this line
UNIQUE KEY `unique_id` (`unique_id`),
since unique_id is already Primary Key. and Primary Keys are unique.
full CREATE TABLE statement
create table `users`
(
uid int(11) auto_increment,
unique_id varchar(23) not null,
name varchar(50) not null,
email varchar(100) not null unique, -- specified here
encrypted_password varchar(80) not null,
salt varchar(10) not null,
created_at datetime,
updated_at datetime null,
PRIMARY KEY (`unique_id`),
UNIQUE KEY `uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=877888
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
SQLFiddle Demo

DB Structure : Ok or not?

I am developing an application management business (sales, suppliers, customers, products, ...) for a new company. To begin, I need to create a database. Could you please tell me if the BD scheme bellow is good and optimized ?
CREATE TABLE IF NOT EXISTS `company` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
`nom` varchar(50) NOT NULL,
`description` varchar(500) NOT NULL,
`enable` ENUM('YES', 'NO') DEFAULT 'YES',
`level` int(1) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY SIRET (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
// contactType can be one of the 3 values : email, phone, fax
CREATE TABLE IF NOT EXISTS `contactType` (
id UNSIGNED INT NOT NULL auto_increment,
`contactType` ENUM('email', 'phonenumber', 'faxnumber')
`mobile` ENUM('YES', 'NO') default 'NO',
PRIMARY KEY (id),
UNIQUE KEY type (contactType)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `contacts` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
`contactType` varchar(50) NOT NULL, // A reference to contactType just above
`contactref` varchar(50) NOT NULL, // Phone number, fax number or email adress
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `customers` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `supplier` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `industry` (
id UNSIGNED INT NOT NULL auto_increment,
`industry` varchar(250) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (activite)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `entreprisePerIndustry` (
id UNSIGNED INT NOT NULL auto_increment,
`industry_id` varchar(250) NOT NULL, // Chemical, Computer, Consulting, ...
FOREIGN KEY (industry_id) REFERENCES industry(id) ON DELETE CASCADE,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (industry_id)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Below are few TIPS which can helps you to create better DB
Change table engine from MyISAM to InnoDB if you want make foreign key constrains to work.
AUTO INCREMENT DataType should be UNSIGNED INT. this will double the range.
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT`
if a column value chosen from a list of permitted values then change dataType to ENUM .In your case enable, level can be turned into ENUM dataType
`enable` ENUM( 'y', 'n' ) NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no' `
add foreign key relation to
contacts.contactType with contactType.id
entreprisePerIndustry .industry with industry.id
update
I have created basic and optimized table structure ( AFAIK ).
--
-- Table structure for table `tbl_company`
--
CREATE TABLE IF NOT EXISTS `tbl_company` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`siret` varchar(50) NOT NULL,
`nom` int(11) NOT NULL,
`description` varchar(250) NOT NULL,
`enable` enum('y','n') NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no',
`level` enum('1','2') NOT NULL DEFAULT '1',
`last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Table structure for table `tbl_contact`
--
CREATE TABLE IF NOT EXISTS `tbl_contact` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`siret` varchar(50) NOT NULL,
`contact_type` enum('email','phone','fax') NOT NULL DEFAULT 'email',
`contact_ref` varchar(100) NOT NULL COMMENT 'Phone number, fax number or email adress',
`last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `siret` (`siret`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
...
...
complate structure is here