Why does the default constraint not work in mysql? [duplicate] - mysql

This question already has answers here:
Column count doesn't match value count at row 1
(6 answers)
Closed 2 years ago.
Error Code: 1136. Column count doesn't match value count at row 1
roll_no INT PRIMARY KEY NOT NULL unique,
name CHAR(15) NOT NULL,
class integer(3),
section CHAR(1),
sibling boolean default false);
insert into school
values(1,'ramesh',12,'D');

If you don't provide values for all the columns, you need to explicitly state what columns the values refer to:
insert into school(roll_no, name, class, section)
values(1, 'ramesh', 12, 'D');

You didn't specify the column names in the insert, and therefore MySQL is expecting data for all columns. Qualify your target column names, and the insert should work:
INSERT INTO school (roll_no, name, class, section)
VALUES
(1, 'ramesh', 12, 'D');
Best practice for SQL inserts is to always list out the target columns for the insert. One reason for this is that, if you don't, an insert statement could break if either a) the number of columns changes, or b) the order of columns in the table changes.

Related

Return records where favorite colors are not all excluded [duplicate]

This question already has answers here:
Query with multiple values in a column
(4 answers)
mysql count occurrences of special character in a field
(2 answers)
Closed 8 months ago.
mysql fiddle http://sqlfiddle.com/#!9/03f4e72
CREATE TABLE IF NOT EXISTS `favorites` (
`name` varchar(20) NOT NULL,
`colors` varchar(20) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `favorites` (`name`, `colors`) VALUES
('Timmy', 'blue,yellow,green'),
('John', 'green,orange,black'),
('Alysha', 'red,purple,orange'),
('James', 'yellow,green'),
('Janet', 'reen'),
('Peter', 'purple,orange,blue'),
('Ryan', 'purple'),
('Tony', 'blue,red');
I want to return records based on colors I exclude, for example, if I exclude purple, all but Ryan's record would return.
If I exclude yellow and green, all but James would return.
If I excluded orange and black, all records would return, the reason John would return with the rest of the records is because while he has orange and black as his favorite colors, he also likes green, which was not excluded.
It's a silly way of going about this and I know the favorite colors should be stored in another table, but the table structure can not be changed and I am trying to accomplish this using pure sql
Any help is appreciated

Primary key auto_increment not working on MYSQL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
CREATE TABLE people (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
salary INT NOT NULL,
PRIMARY KEY (id)
);
My insert statement
INSERT INTO people
VALUES
('Austin', 65000),
('Josh', 100000),
('Pettea', 55000)
;
The columns amount (total columns amount in table structure while their list is skipped) must match the values amount strongly.
So either specify column names:
INSERT INTO people (first_name, salary)
VALUES
('Austin', 65000),
('Josh', 100000),
('Pettea', 55000);
or specify the value for each column:
INSERT INTO people
VALUES
(NULL, 'Austin', 65000),
(NULL, 'Josh', 100000),
(NULL, 'Pettea', 55000);
In the last code you may use zero value or DEFAULT keyword instead of NULL. Any of these 3 values causes new value generation.
PS. The former code is preferred - it does not need in any edition if the structure is altered.
PPS. I'd recommend to define autoincremented column datatype as UNSIGNED - this doubles the amount of possible autogenerated values twice.
It should be:
INSERT INTO people(first_name, salary)
VALUES
('Austin', 65000),
('Josh', 100000),
('Pettea', 55000);
try that.
Here's a working fiddle: https://www.db-fiddle.com/f/rW6QyPbyEMvZhcskipBpaX/0
click "Run".
Edit:
I think I misread your comment: In this case you need to specify which column(s) you want to insert data. For now your query is trying to insert a two columns data value into a three columns table. INSERT INTO people is similar to INSERT INTO people(id,first_name,salary) so inserting two columns worth of data returns Column count doesn't match value count at row 1 error. It's acceptable if you do like this:
INSERT INTO people
VALUES
(1,'Austin', 65000),
(2,'Josh', 100000),
(3,'Pettea', 55000);
But you're making id column as AUTO_INCREMENT for a reason hence it's unnecessary to manually populate it.

unique constraints for multiple columns in mysql [duplicate]

This question already has answers here:
How do I specify unique constraint for multiple columns in MySQL?
(14 answers)
Closed 4 years ago.
Guys i have that table in DB
-------------------------------------------------------------
ID | COMPANY_ID | NAME | DESCRIPTION | BARCODE
-------------------------------------------------------------
i want to make a constraints that unique barcodes should be enetered with same companyID .... so how to do that in mysql ??
NB: i cam enter multiple company_id in the table
You need to add a Composite UNIQUE constraint on COMPANY_ID and BARCODE, to your table. This basically means that it will not allow rows having duplicate combination of values, for the said two fields. It can still allow duplicate values individually for either of them.
Eg: (1, 'abc') combination will not be able to exist in more than one row. However, (1, 'abc'), (1, 'def') can exist. So it allows duplicate values individually (Company_id in this example)
Do the following (change table and column name(s) accordingly):
ALTER TABLE your_table_name
ADD CONSTRAINT unique_barcode_company UNIQUE (COMPANY_ID, BARCODE)

MySQL - insert into statement requires me to enter primary key

I'm pretty much a complete newbie to SQL, I'm using MySQL with SQLyog. I have five fields, StudentForename, StudentSurname, StudentAge, StudentHouse and StudentID for the Primary Key. The StudentID field is set as a Primary Key and Not Null and AutoIncrement. I'm trying to use an INSERT INTO statement without having to entering the primary key - apparently I shouldn't need to, it should update itself. But it's not working, it's returning the error "Column count doesn't match value count at row 1". Here's the code I'm using. I've already set up the table, so I haven't got the code for the query that
INSERT INTO students VALUES('Harry', 'Potter', 'Slytherin', 30)
You will need to explicitly state which columns you will provide values for, otherwise it is assumed you will provide values for all columns. E.g.
INSERT INTO students (`first_name`, `last_name`, `house`, `age`) VALUES('Harry', 'Potter', 'Slytherin', 30)
(I made up column names, swap these with your columns)

SQL Remove Row That Has The Same Values Into New Table [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Remove Duplicate Rows Leaving Oldest Row Only?
MySQL remove duplicate rows
Say that I have the following table coolumns: int logicalid(pk) ,int home, int person say the I have the following records...
1,5,6
2,5,6
3,5,5
4,5,5
After my query, I want to place into the new table only one row with the same home,person column values so this will be the output result:
1,5,6
2,5,5
Any ideas??
Create your new table with an auto-increment column for the id.
Then insert into the table, using a query such as:
insert into newTable(home, person)
select distinct home, person
from oldTable
INSERT INTO newtable(home, person) SELECT DISTINCT home, person FROM sourcetable