开发者

How to insert a value in SQL anywhere?

I can't insert a value using this code:

 inse开发者_开发问答rt into t_category_values (CategoryCode,
 CategoryValueCode, CategoryValue) values(2, 1, 1);

error msg

no primary key


Your query is wrong it should be:

INSERT INTO `DBA`.`t_category_values` 
(CategoryCode, CategoryValueCode, CategoryValue)
VALUES(1, 1, "aaa");

See here for more help: Sql Insert Wiki


Edit:

Your query and error:

insert into t_category_values (CategoryCode, CategoryValueCode, CategoryValue) values(2, 1, 1);

no primary key

In insert while specifying to insert in few column instead of all in table you must have to include primary key. That means in your one of CategoryCode, CategoryValueCode, CategoryValue should be primary key or include fourth column that is primary key in table.


insert into `DBA`.`t_category_values` (`CategoryCode`, `CategoryValueCode`, `CategoryValue`) values(1, 1, "aaa");


Try this (edited because you use Sybase SQL Anywhere):

INSERT INTO dba.t_category_values
(CategoryCode, CategoryValueCode, CategoryValue)
VALUES(1, 1, 'aaa');

EDITED:
from Sybase web page: Adding rows to a table

Suppose that a new eastern sales department is created, with the same manager as the current Sales department. You can add this information to the database using the following INSERT statement:

 INSERT
 INTO department ( dept_id, dept_name, dept_head_id )
 VALUES ( 220, 'Eastern Sales', 902 )

If you make a mistake and forget to specify one of the columns, SQL Anywhere reports an error.

The NULL value is a special value used to indicate that something is either not known or not applicable. Some columns are allowed to contain the NULL value, and others are not.


The INSERT goes like this:

INSERT INTO `DBA`.`t_category_values`
(CategoryCode, CategoryValueCode, CategoryValue) 
VALUES (1, 1, "aaa");
  1. No "double quotes" around table names, backticks are allowed. DBA is the database, t_category_values is the table.
  2. Name the columns you want to fill
  3. Add 'values' followed by the values for those columns.

If you want to insert data from another table, use a SELECT:

INSERT INTO `DBA`.`t_category_values`
(CategoryCode, CategoryValueCode, CategoryValue) 
SELECT (CategoryCode, CategoryValueCode, CategoryValue) 
FROM `DBA`.`old_category_values`;


Insert query eg:-

Insert into `tableName`(field1, field2, field3) values ('value1', 'value2', 'value3');


You are getting the error "No Primary Key" because you have not specified a valid unique primary key.

Your table must be 4 columns.

INSERT INTO t_category_values (PRIMARY_KEY, CategoryCode, CategoryValueCode, CategoryValue) 
VALUES(pkey_value_here, 2, 1, 1);

The only time you dont need to specify the primary key column is when the primary key is an auto increment value. If it is an auto increment column the INSERT will automatically fill in that value for you and you dont need to worry about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜