Search code examples
sqldatabasephpmyadminmysql-error-1241

Why do I receive error 1241 when trying to Create and Insert data into a new table (on phpMyAdmin)?


I am trying to create a new table and insert values into it however I keep receiving "#1241 - Operand should contain 1 column(s)". Please could someone help to identify what is wrong with my code as I am unsure what this error is referencing?

The code I am inserting into the phpMyAdmin database under the SQL tab. I have tried to remove the auto increment attributes and have tried looking at other examples to check my syntax, but I can't spot the issue. Some guidance on this would be greatly appreciated.

The code I entered begins like this:

# AppSoft Project - Greg Roberts

DROP table if exists Department;
DROP table if exists Role;
DROP table if exists User;
DROP table if exists Appraisal;
DROP table if exists Question;
DROP table if exists Answer;

CREATE table Department(
    DeptID int NOT NULL AUTO_INCREMENT,
    DeptName varchar(30) NOT NULL,
    primary key (DeptID));

INSERT into Department values(
    (00, 'SuperAdmin'),
    (01, 'Support Staff'),
    (02, 'Teaching Staff'),
    (03, 'SLT'));

CREATE table Role(
    RoleID int NOT NULL AUTO_INCREMENT,
    RoleTitle varchar(30) NOT NULL,
    primary key (RoleID));

INSERT into Role values(
    (00, 'SuperAdmin'),
    (01, 'Office Administrator'),
    (02, 'Teaching Partner'),
    (03, 'Mid Day Supervisor'),
    (04, 'Cooks'),
    (05, 'Cleaners'),
    (06, 'Maintenance'),
    (07, 'Teacher'),
    (08, 'Department Head'),
    (09, 'SENCO'),
    (10, 'Head Teacher'),
    (11, 'Executive Head'));

Error Code that Occurs


Solution

  • You dont need to insert primary keys, if they are set to auto_increment.

    DeptID int NOT NULL AUTO_INCREMENT
    

    Just insert the department name, there are no additional braces required for Values.

    INSERT into Department( DeptName) values 
    ('SuperAdmin'),
    ('Support Staff'),
    ('Teaching Staff'),
    ('SLT');
    

    You would need to do the same for Role table as well.

    Also if you try to insert 0 into the primary key, it will actually insert 1 you can read about it in the Standard Docs

    you seem to be getting the error because your first record inserts 1 into the table and then your second record tries to insert 1 again in the primary key column