how to insert data in only few column leaving other columns empty or as they are in a mysql table record?

喜你入骨 提交于 2019-12-12 13:22:33

问题


I have created a table named 'students'. It has following fields :

 roll_no <- type:Integer Not Null,
 course_name <- type:varchar(40) Not Null,     
 std_surname <- type:varchar(40) Not Null,
 std_firstname <- type:varchar(40) Not Null,      
 emailid <- type:varchar(40) ,   
 address <- type:varchar(40) Not Null,
 income <- type:Integer,        
 gender <- type:varchar(10) Not Null,         
 experience <- type:Integer, 

in the above fields mentioned some accept null values and most of them don't accept null values or null values are not allowed.

What I want to do is I want to insert some information or data into the columns that do not accept null values and then I want to insert remaining data into the remaining columns later. How can I achieve this?

More specifically how can I insert only few data into specific fields in a record at a time using insert query leaving other fields empty and then insert remaining field values like email or experience ???

Please help me with this problem?


回答1:


Insert into selected fields

for ex

    INSERT INTO 
            students
            (roll_no, course_name, dob, gender)
    VALUES
            (111, 'MBA', '1991-01-01', 'F');

for inserting NULL values, have a look at

Insert NULL value into INT column




回答2:


First insert, you can insert an empty string into the NOT NULL fields, later you can update those fields.

INSERT INTO yourTable (field1, field2, field3) VALUES ("information1", "", "");
UPDATE yourTable SET field2 = "information2", field3 = "information3" WHERE rowId = x;

Forget inserting the empty strings, MySQL will do it by default when you do it as Hytool wrote.



来源:https://stackoverflow.com/questions/37384874/how-to-insert-data-in-only-few-column-leaving-other-columns-empty-or-as-they-are

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!