Column name or number of supplied values does not match table definition. - mysql

I'm writing a program in visual to use a database to make a PC builder type program. When I try to insert data into the PROCESSORS table, I get the following error:
ERROR:
Column name or number of supplied values does not match table definition.
Since my other inserts work fine, I don't know what's wrong with this one.
DROP TABLE PROCESSORS
DROP TABLE MOTHERBOARDS
DROP TABLE SOCKET_TYPE
DROP TABLE STORE;
CREATE TABLE PROCESSORS (
PRODCUT_ID VARCHAR(20) PRIMARY KEY,
BRAND VARCHAR(6) NOT NULL,
CORES INTEGER NOT NULL,
SPEED DECIMAL NOT NULL,
INTEGRATED_GPU VARCHAR(40) NOT NULL);
CREATE TABLE STORE(
STORE_ID VARCHAR(20) PRIMARY KEY,
PRODUCT_ID VARCHAR(20) NOT NULL,
PRODUCT_NAME VARCHAR(50) NOT NULL,
STORE VARCHAR(30) NOT NULL,
PRICE INTEGER NOT NULL);
/*Newegg Product I7-4470*/
INSERT INTO PROCESSORS VALUES('BX80646I74770','Intel',4,3.4,'None');
INSERT INTO STORE VALUES('N82E16819116900','BX80646I74770','Intel Core I7-4470 Haswell','Newegg',309.99);

Looks like you're entering a decimal in to an integer in the STORE Price column :)
Edit: Just noticed the DECIMAL type of the column is missing the impelementation syntax, e.g. DECIMAL(2,2). As it stands, according to here the default when not provided is DECIMAL(10, 0) which is a 10 digit integer basically. Provide precise parameters to the DECIMAL data type to allow the correct numbers with decimal places to be added, this will fix the INSERT problem.

Related

Can I have a string of TIMESTAMPS in MySQL?

I am just wondering to know can I have a string of TIMESTAMPs in a MySQL table schema like following:
CREATE TABLE IF NOT EXISTS auth (
id BINARY(16) PRIMARY KEY,
email VARCHAR(64) UNIQUE NOT NULL,
password VARCHAR(64) NOT NULL,
admin INT NOT NULL DEFAULT 0,
created_at [TIMESTAMP]
);
Unfortunately the above code gives me a syntax error!
My idea is to have a list of TIMESTAMs to store every future updates inside of it, something like ['2023-01-01' , '2023-02-02' , ....]
If this is not possible in my suggested way, how can I store any changes of one column of a table like created_at column? Should I do it in the web-server?

primary key INT for emoji

I am writing a MySQL table to store data about every single emoji:
CREATE TABLE `emoji` (
`emoji_id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(255) NOT NULL,
`html` VARCHAR(255) NOT NULL,
PRIMARY KEY (`emoji_id`)
);
An example insertion might be:
INSERT INTO `emoji` VALUES (12345, 'grinning face', 'πŸ˜€');
Or:
INSERT INTO `emoji` VALUES (67890, 'woman walking', 'πŸšΆβ€β™€οΈ');
However, instead of my own proprietary ID number 12345 or 567890, I would like to use a universal decimal number that is already assigned to each emoji.
Does such an ID number exist?
I thought about simply converting the hexadecimal representation to decimal to use as the emoji_id primary key, however this does not work because some emoji (such as my second example) are represented by a sequence of hexadecimal numbers.
The closest thing I have found is Unicode.org's complete list of emoji. The first column is a unique decimal number for each emoji. For example, the decimal ID number of "house" is 735, and the decimal ID number of "woman walking" is 343.

Trigger INSERT SQL multiple data

I'm trying to create a trigger with sql so that When I insert a row in Point I insert before it a row in PointAbs.
CREATE TABLE PointAbs (
ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
X INTEGER NOT NULL,
Y INTEGER NOT NULL
);
CREATE TABLE Point(
ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(50) ,
IDPointAbs INTEGER NOT NULL,
FOREIGN KEY (IDPointAbs) REFERENCES PointAbs(ID) ON DELETE CASCADE
);
the problem is that I need to provide "X" and "Y" for PointAbs and "Name" for Point at the same time. Ho can I achieve that?
I could use a JDBC functionality to get the last insertedID but I don't like it that way.
It seems like the relation is 1 to 1, as you have to create a new PointAbs for each Point. Unless you have another table that relates to PintAbs, there will be one PointAbs for each Point. If you don't need two separated objects, you can state X and Y as an index of Point:
CREATE TABLE Point(
ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
Name VARCHAR(50) ,
IDPointAbs INTEGER NOT NULL,
X INTEGER NOT NULL,
Y INTEGER NOT NULL,
INDEX INDEX_X_Y ON Point(X,Y)
);
Of course, this might not be possible nor desirable in your design.
As you can't send parameters to the trigger like the X and Y values, your best option is to use a single transaction for both inserts.
BEGIN;
INSERT INTO PointAbs(X,Y) VALUES (10,15);
INSERT INTO Point(Name, IDPointAbs) VALUES ('Fancy Name', LAST_INSERT_ID(PointAbs));
COMMIT;
You can control the insertions using the programming language of the system back-end, but as there is no specific language mentioned other than mysql, I won't enter into details.

Number in PHP form being saved as a random number in mySQL DB

I have a form to add/edit a record in a mysql database.
However when entering a phone number, such as '07854301812', '2147483647' is saved in the database.
I have echoed out the $number variable, and this is the correct value, so something is going wrong when storing in the database.
$sql = mysql_query("UPDATE `customer` SET `f_name` = '$fname',`l_name` = '$lname',`email` = '$email',`number` = '$number' WHERE `c_id` = '$id'");
and this is my sql DDL
CREATE TABLE Customer(
c_id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
f_name VARCHAR(50) NOT NULL,
l_name VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
number INTEGER,
date_joined DATE NOT NULL CHECK (date_added <= now())
);
Your number is larger than the largest integer you can store. Given your numbers start with a 0 that'll get truncated if you switch to using a BIGINT, you should probably just store them as a VARCHAR.
I agree, looks like the '2147483647' is the max positive value for an INT.
Some info on integer types (and sizes): http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
You can quickly change the schema to use a larger number like the following:
mysql> ALTER TABLE Customer MODIFY number BIGINT;
Some more info on the Alter syntax: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
So long as you go from a smaller number to larger number you won't lose any information in your table.
You should be able to test pretty quickly with that and see if it works.

MySQL - how to use VARCHAR as AUTO INCREMENT Primary Key

I am using a VARCHAR as my primary key. I want to auto increment it (base 62, lower/upper case, numbers), However, the below code fails (for obvious reasons):
CREATE TABLE IF NOT EXISTS `campaign` (
`account_id` BIGINT(20) NOT NULL,
`type` SMALLINT(5) NOT NULL,
`id` VARCHAR(16) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
however, this works:
CREATE TABLE IF NOT EXISTS `campaign` (
`account_id` BIGINT(20) NOT NULL,
`type` SMALLINT(5) NOT NULL,
`id` VARCHAR(16) NOT NULL PRIMARY KEY
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
What is the best way to keep track of incrementation of 'id' myself? (Since auto_increment doesn't work). Do i need to make another table that contains the current iteration of ID? Or is there a better way to do this?
EDIT: I want to clarify that I know that using INT is a auto_increment primary key is the logical way to go. This question is in response to some previous dialogue I saw. Thanks
you have to use an INT field
and translate it to whatever format you want at select time
example of a solution to your problem:
create a file with a unique number and then increment with a function.
the filename can be the prefix and the file binary content represent a number.
when you need a new id to the reg invoque the function
Example
String generateID(string A_PREFIX){
int id_value = parsetoInt(readFile(A_PREFIX).getLine())
int return_id_value = id_value++
return return_id_value
}
where "A_PREFIX-" is the file name wich you use to generate the id for the field.
Or just create a sequence and maintain the pk field using the sequence to generate the primary key value with nextval function. And if perf is an issue, use cache on sequence.
But as others have stated, this is sub-optimal, if your primary key contains a numbered sequence then it's better to use int and auto-increment.
I don't see a use case where pk has to auto-increment but be a varchar data type, it doesn't make sense.
Assuming that for reasons external to the database, you do need that varchar column, and it needs to autoIncrement, then how about creating a trigger that grabs the existing autoIncrement value and uses Convert() to convert that value into a VarChar, dropping the VarChar into the field of interest. As mentioned in a previous answer, you could concatenate the table-name with the new varChar value, if there is some advantage to that.