Insert values from form boxes into last record of table - sql-server-2008

I have a very large sub routine that opens forms and insert values into text boxes and saves changes. it works, but it inserts these values into random rows and not the newest record on the table. I have also tried a INSERT INTO VALUES query which does the same thing (through VBA and regular SQL query). Is it possible to add these values to the final/first row of the corresponding table? Primary key's are autonumber.
DoCmd.OpenForm "dentalheader", acNormal , , , , acHidden
Forms!dentalheader!yeartype.Value = "Benefit"
Forms!dentalheader!term.Value = "80"
Forms!dentalheader!personmax.Value = "99999"
INSERT INTO dentalheader (each, overallmax, personmax,)
VALUES (true, '10000', '99999', )

Add a datetime field and insert Now:
INSERT INTO dentalheader (timestamp, each, overallmax, personmax)
VALUES (Now(), true, '10000', '99999' )
Then, for display, sort on timestamp.

Related

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

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.

Default values not working in phpmyadmin/mysql database

I can't get a table to accept "" or '' and use the default value. It is inserting NULL instead.
I am trying these commands in the direct input sql window.
INSERT INTO test01 VALUES ("", now(), "");
INSERT INTO test01 VALUES ('', now(), '');
But both just give NULL in the 3rd column. The structure is set to non-null with a default value of "yes". (Without quotation marks).
Here is a screenshot of the structure. You can see NULL is not checked.
http://garryjones.se/extras/so3.png
Default values only work if no value is inserted/updated. If you explicitly set it to an empty string (which is NOT the same as a NULL value) then it will end up with an empty string in the column. Instead of the code above you should eliminate the column from the INSERT statement at all:
INSERT INTO test01 (t1, t2) VALUES ('', now())
Other is already explain the reason here I am adding one more point you are also using current time stamp on update so do not need to use this column as well.
INSERT INTO test01 (t1) VALUES ('')
You could use the DEFAULT keyword: INSERT INTO test01 VALUES ("", now(), DEFAULT);

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');

pass column in INSERT query

I want to update and insert to (in 1 query) my table. I've 1 new row and 2 old records.
I use this query (topic):
INSERT INTO table (id, title, url) VALUES (1, "test", "http://"), (2, [it is an old record. I don't want to change its title], "https://"), (3, "helloworld", "mms://") ON DUPLICATE KEY UPDATE title = VALUES(title), url = VALUES(url)
I don't want to change the second record (id = 2) title column, but if I put NULL, DEFAULT or title after comma, title will change to NULL (also if i put nothing after comma, an error will occur)
What can I do? should I use 2 or more queries?
Thank you.
You can use something like:
INSERT INTO table (id, title, url) VALUES (1, "test", "http://"), (2, "", "https://"), (3, "helloworld", "mms://") ON DUPLICATE KEY UPDATE title = IF(VALUES(title)="",title,VALUES(title)), url = VALUES(url)
Not 100% clear on your goal but could you not nest a SELECT that would pull back the old title for in place of the text in square brackets?