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

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?

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

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.

Related

Add a line in between a SQL table and change following id

I would like to insert a row in the middle of my SQL table. Similar to Excel, the existing rows should then be automatically increased by ID+1. Is there such a function?
Example:
Current state:
After insert inbetween:
Please never do it in production.
Setup
create table tbl(
id int NOT NULL AUTO_INCREMENT primary key,
val varchar(20)
);
insert into tbl (val)
values
('a'),('b'),('c'),('d');
The script to add 'x' between 'b' and 'c' when ordered by id
update tbl
set id=id+1
where id > 2
order by id desc;
insert tbl(id, val)
values (3,'x');

Adding entry inside a table in database

I wanted to add an entry inside a table in SQL database.
For example I have the following Database
CREATE TABLE `distributor_geneology` (
`distributor_gen_id` bigint(20) NOT NULL,
`user_id` varchar(24) NOT NULL,
`id` bigint(20) NOT NULL,
`sponsor_id` bigint(20) NOT NULL,
`rank` tinyint(4) NOT NULL
);
And I want to add an entry in sponsor_id or say id inside a database.
First, I imported the database in my SQL Workbench then In my SQL Workbench, I ran a command select * from distributor_geneology which gave me
Error Code: 1146. Table 'dba_db.distributor_genelogy' doesn't exist
[Question] How can I create/add Entry for ID (or sponsor ID or any other filed)?
One typical way which data would enter a MySQL database is via an INSERT statement:
INSERT INTO distributor_geneology (distributor_gen_id, user_id, id, sponsor_id, rank)
VALUES
(1, 1, 1, 1, 1);
I am inserting 1 everywhere, but you may alter the tuple with the values you want.
Another way to get data into a table is bulk loading via LOAD DATA.
For your first part of your question which is "Add an entry to inside table"
this operation called insertion in the database and the keyword database used to insert data is insert into
It is possible to write the INSERT INTO statement in two ways:
1- specifies both the column names and the values to be inserted
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
you can rearrange the columns orders as you want but must the values be the same order of the columns and you can let any column null if you don't want to insert any data in this column but be careful if you have not null column you must insert in you query
in your case, all the columns you have are not null.
2- if you do not need to specify the column names in the SQL query. make sure the order of the values is in the same order as the columns in the table
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
For your second part of your question which is "Error Code: 1146. Table 'dba_db.distributor_genelogy' doesn't exist"
First, ensure you imported the DB correctly and if yes > write try to use DB name in your query.
select * from DB_Name.Table_Name
Edit:
Try this query format
INSERT INTO distributor_geneology (distributor_gen_id, user_id, id, sponsor_id, rank)
VALUES
(10, '10', 10, 10, 10);
please note I put second value between 2 quotes because you are defining the user_id as varchar which means not an integer so we should put it between qouts

Comparing nulls during join

I read that null cannot be compared with null and the result is always false.
In the below link I am able to compare 2 nulls and the rows are returned.
CREATE TABLE user (id varchar(50), banstatus varchar(100));
INSERT INTO user (id, banstatus) VALUES ('1', '1');
INSERT INTO user values ('2', 'NULL');
CREATE TABLE banstatus (id varchar(50), texti varchar(100));
INSERT INTO banstatus VALUES('1', 'Banned');
Insert into banstatus values ('NULL' , 'NULL');
select * from user as u
join banstatus as b on u.banstatus=b.id
http://sqlfiddle.com/#!9/33f25/1/0
So what is the correct statement about comparison of nulls ?
The values you have inserted are strings. 'NULL' is a string and is a definite value. To insert NULL you shouldn't use the quotes, for example:
INSERT INTO table (field1, field2) VALUES ('foo', NULL)
DEMO
And you can't compare with NULL, to understand its meaning it's something like undefined. Although, You can test if a value is or is not NULL.
You are using 'NULL' (a string) not NULL .. so you are comparing two (same ) strings
In mysql there is a null safe opearator that compare as true both null values
<=>
NULL-safe equal. This operator performs an equality comparison like the = operator, but returns 1 rather than NULL if both operands are NULL, and 0 rather than NULL if one operand is NULL.
https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
so for a correct null safe join you should use
select * from user as u
join banstatus as b on u.banstatus<=>b.id
In your answer you compare NULL as varchar, not plain NULL.

SQL INSERT - Can I insert null values if column count doesn't match?

I get an error message that says "#1136 - Column count doesn't match value count at row 1" when I try to insert the values below. Is there any way to have the lines of data that are shorter automatically insert as null at the end without manually going into each line of code?
VALUES ('','BUF','','QB1','','QB BILLS','Face=0x52','#0','25','69','13','13','56','81','81','81','3','12','3');
VALUES ('','BUF','','QB2','frank REICH','Face=0x22','#14','25','69','13','13','31','44','25','50','3','3','2');
VALUES ('','BUF','','RB1','thurman THOMAS','Face=0x83','#34','38','69','63','25','75','50','10','7','8','8');
VALUES ('','BUF','','RB2','jamie MUELLER','Face=0x51','#41','44','69','25','88','50','25','5','3','6','2');
VALUES ('','BUF','','RB3','kenneth DAVIS','Face=0xa5','#23','38','69','25','19','50','31','5','2','7','2');
VALUES ('','BUF','','RB4','don SMITH','Face=0x8b','#30','38','69','25','19','50','31','3','3','8','2');
VALUES ('','BUF','','WR1','james LOFTON','Face=0x81','#80','25','69','38','13','50','56','1','8','13','6');
VALUES ('','BUF','','WR2','andre REED','Face=0xb6','#83','25','69','56','13','56','69','1','9','10','10');
VALUES ('','BUF','','WR3','don BEEBE','Face=0x40','#82','25','69','44','13','50','44','1','4','13','2');
VALUES ('','BUF','','WR4','al EDWARDS','Face=0x9a','#85','25','69','19','13','50','44','1','4','7','2');
VALUES ('','BUF','','TE1','keith MCKELLER','Face=0xb7','#84','25','69','38','50','50','50','1','6','7','6');
VALUES ('','BUF','','TE2','pete METZELAARS','Face=0x50','#88','25','69','19','44','50','31','1','3','5','2');
VALUES ('','BUF','','C','kent HULL','Face=0x1e','#67','25','69','38','69');
VALUES ('','BUF','','LG','jim RITCHER','Face=0x7','#51','25','69','38','56');
VALUES ('','BUF','','RG','john DAVIS','Face=0x24','#65','25','69','25','63');
VALUES ('','BUF','','LT','will WOLFORD','Face=0x48','#69','25','69','25','50');
VALUES ('','BUF','','RT','howard BALLARD','Face=0x88','#75','25','69','19','63');
VALUES ('','BUF','','RE','bruce SMITH','Face=0x88','#78','44','56','69','75','25','81','116','7');
VALUES ('','BUF','','NT','jeff WRIGHT','Face=0xb','#91','25','31','31','50','19','19','30','7');
VALUES ('','BUF','','LE','leon SEALS','Face=0xac','#96','25','31','38','44','31','50','25','18');
VALUES ('','BUF','','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25');
VALUES ('','BUF','','RILB','ray BENTLEY','Face=0x30','#50','25','31','38','38','31','56','13','10');
VALUES ('','BUF','','LILB','shane CONLAN','Face=0x2f','#58','31','44','50','56','19','69','13','13');
VALUES ('','BUF','','LOLB','c. BENNETT','Face=0x82','#97','38','50','63','63','19','69','29','7');
VALUES ('','BUF','','RCB','nate ODOMES','Face=0xc3','#37','38','44','56','38','38','56','0','25');
VALUES ('','BUF','','LCB','kirby JACKSON','Face=0x89','#47','25','31','44','38','50','50','0','64');
VALUES ('','BUF','','FS','mark KELSO','Face=0x26','#38','31','38','50','38','44','44','2','39');
VALUES ('','BUF','','SS','leonard SMITH','Face=0x84','#46','31','38','50','44','44','50','2','40');
VALUES ('','BUF','','K','scott NORWOOD','Face=0x29','#11','56','81','81','31','44','44','6');
VALUES ('','BUF','','P','rick TUTEN','Face=0x20','#10','25','56','44','31','19','63','3');
Remove the unwanted column from your mysql statement
for example
mysqli_query($con,"INSERT INTO files (1, 2, unwanted, 4)
VALUES ('$01', '$02', '' ,'$04')");
make it
mysqli_query($con,"INSERT INTO files (1, 2, 4)
VALUES ('$01', '$02' ,'$04')");
or Add a variable which is null,
<?php
$null = '';
VALUES ('$null','BUF','$null','ROLB','darryl TALLEY','Face=0xad','#56','31','44','50','38','44','63','25','25');
?>
I have made the following assumptions base on your current post. I assume you want to add the score of a football player to the same table as the fixed data from each player. I would not recommend this because you will continue to add a new column every time you like to add a score.
I have made the following example for you how you can make a score table and player table that will separates the fixed data like name of the player, picture, etc from the scores.
Try this:
SQL query:
SELECT players.name, group_concat(score.score)
FROM players
JOIN score
ON players.id = score.id_players
GROUP BY players.id;
CREATE TABLE players (
id int auto_increment primary key,
name varchar(30)
);
CREATE TABLE score (
id int auto_increment primary key,
score int,
id_players int
);
INSERT INTO players (name)
VALUES ('QB BILLS'), ('frank REICH');
INSERT INTO score (id_players, score)
VALUES (1,25),(1,69), (1,13),(1,13),(1,56),(1,81),(1,81),
(1,81),(1,3),(1,12),(1,3), (2,25),(2,69),(2,13),(2,13),(2,31),(2,44),(2,25),(2,50),(2,3),(2,3),(2,2);
SQLFiddle demo

INSERT values to MySQL

If i have my SQL create statement as follows;
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200),
PRIMARY KEY( TAB_ID )
);
and if i want to Insert values to it, should i enter TAB_ID too ? Since it's auto increment.
When i INSERT INTO SWM_SALES_FEEDBACK VALUES (1,'Jerry','ty'); It gets inserted if i don't specify the primary key INSERT INTO SWM_SALES_FEEDBACK VALUES ('Jerry','ty'); i get the following error :
ERROR 1136 (21S01): Column count doesn't match value count at row 1
1.) What is the point of having AUTO_INCREMENT if it doesn't get auto incremented.
2.) If i have a field called:
BIRTH_TIME DATE,
How should i INSERT value to this field since it's DATE type
1) You have to respect an order of columns in your table. You can do either:
INSERT INTO SWM_SALES_FEEDBACK VALUES (null, 'Jerry','ty');
or
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST, TYPE) VALUES ('Jerry','ty');
2) You can use '2012-07-24' format for date column.
With an auto increment, you need to specify your columns
INSERT INTO tablename12 ('name_first','type') values ('Jerry','ty')
If you aren't inserting into every column, you need to tell the query which columns you want to insert into. try INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Not sure if mysql has a to_date() function in the new releases:
But, you can use this : Just enclose the date time field in single quotes. You can specify the date time formats
INSERT INTO table_name (birth_date) VALUES ('2008-07-04')
For other date format questions refer this.
For your auto increment, specify the columns
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Point 1
If you don't want to store all fields in DB then you have to specify those column names before VALUES clause as shown below.
INSERT INTO myTable (field2,field3) VALUES ('field2','field3')
In your case, statement should be
INSERT INTO SWM_SALES_FEEDBACK (name_first, type) VALUES ('Jerry','ty')
^^^^^^^^^^^^^^^^^^
Point 2
If you have field as DATETIME, TIMESTAMP as datatype, you can enter date as
INSERT INTO myTable (myDate) VALUES ('2012-12-28 12:12:12')
Format is yyyy-mm-dd hh:mm:ss
If you want to store the current time of the system in DB, you could use NOW() as shown below.
INSERT INTO myTable (myDate) VALUES (NOW())
Try the following code
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');