sql-insert

In MySQL, how can I use the autoincrement value in another column at the time of the INSERT?

ⅰ亾dé卋堺 提交于 2020-01-15 14:27:09
问题 I am looking to have the automagically set autoincrement included in another column during the insert. For example in a table where ID is the autoincrement and Name is the other column, I'd like to do something like `INSERT INTO Names (Name) VALUES (CONCAT("I am number ",ID));` Currently, I do the INSERT without Name, then I have to immediately after do an UPDATE using $mysqli->insert_id. I don't want to query the table in advance because, as small a time as it may be, another record could

SQL Server 2008: Sql Insert/Update into another table using insertion IDs output from another table

南楼画角 提交于 2020-01-15 03:32:28
问题 I have a procedure for insert in multiple dependent tables (update in case record exist). I have input parameters as comma separated string which I am reading in table. After 1st insertion I am getting InsertedIds in another table variable. I am struggling over how to do insert in 2nd table. I have following input parameters for 2nd table: Declare @IdsToBeUpdated table (primary key identity pkey, id int) -- values are 1,-1,3,-1 Declare @CommentsTobeInserted table( primary key identity pkey,

Keywords in SQL script data causing problems when executing programmatically - C#

断了今生、忘了曾经 提交于 2020-01-14 05:23:17
问题 I'm fairly new to sql and am having a problem with keywords causing havoc in my sql script. I'm trying to execute a list of premade .sql script files in C#. I'm currently reading the file to a string and executing it with command.ExecuteNonQuery(). This works great for most of the scripts, but I'm running into one that inadvertently contains a keyword: INSERT INTO [thetable] SELECT '123123', 'abcabc', 'I WANT TO GO TO BED' UNION ALL SELECT '123124', 'abcdef', 'SOOO TIRED' Essentially, when it

ORA-04091: table is mutating, trigger/function may not see it error during execution of oracle trigger

只愿长相守 提交于 2020-01-13 20:29:06
问题 I have below trigger in which for FIELD_NAME field i want to insert value into FIELD_TRACKING table as 'Deactivation time of KPI in case of Downtime(Select KPI_FREQ_TIME_UNIT FROM KPI_DEFINITION)' . The bracket part in this string value comes from KPI_FREQ_TIME_UNIT field of KPI_DEFINITION table. So below is the trigger i have wrritten for this. The trigger compile without any error. But when i try to change the DNTM_REAC_AFTER_HRS field from the KPI_DEFINITION table then i am getting error

Why CREATE TABLE AS SELECT is more faster than INSERT with SELECT

ぐ巨炮叔叔 提交于 2020-01-12 10:15:18
问题 I make a query with INNER JOIN and the result was 12 millions lines. I like to put this in a table. I did some tests and when I created the table using clause AS SELECT was more faster than, create the table first and run a INSERT with SELECT after. I don't understand why. Somebody can explain for me? Tks 回答1: If you use 'create table as select' (CTAS) CREATE TABLE new_table AS SELECT * FROM old_table you automatically do a direct-path insert of the data. If you do an INSERT INTO new_table AS

PHP Array inserting too many records in the database

≯℡__Kan透↙ 提交于 2020-01-11 13:24:34
问题 If i enter only 1 record. It saves only 1 record in the database which is fine. But if i put two records of the same fields. It saves multiple records in the database which should only be two. What did i do wrong? <td>1.<input name='Description[]' type='text' required></td> <td><input type='text' name='Unit[]' placeholder='eg. reams,pcs,box' required></td> <td><input type='number' name='Quantity[]' min='1' required></td> <td><input type='number' name='Cost[]' min='1' required></td> </tr> I

PHP Array inserting too many records in the database

此生再无相见时 提交于 2020-01-11 13:24:33
问题 If i enter only 1 record. It saves only 1 record in the database which is fine. But if i put two records of the same fields. It saves multiple records in the database which should only be two. What did i do wrong? <td>1.<input name='Description[]' type='text' required></td> <td><input type='text' name='Unit[]' placeholder='eg. reams,pcs,box' required></td> <td><input type='number' name='Quantity[]' min='1' required></td> <td><input type='number' name='Cost[]' min='1' required></td> </tr> I

How do I create a table with constraints while pulling data from another table

跟風遠走 提交于 2020-01-11 07:32:25
问题 I am trying to create a writers table that contains the author ID, last name, first name, and ISBN and title of the book each author wrote. While using the same data types as the author and books table, but not copying the data from these tables. And include the author ID as the primary key, the title as not null, and the ISBN as the foreign key referencing the ISBN in the books table. However I receive the "unique constraint (WT_PK) violated" error: CREATE TABLE writers (authorid VARCHAR2(4)

ERROR: column of relation does not exist PostgreSQL ,Unable to run insert query

不羁岁月 提交于 2020-01-10 03:44:12
问题 Hi I am trying to insert into a table tester3 it fails when i use the syntax insert into tester3 (UN0, UN1) values ( 1, 'jishnu1'); but insert into tester3 values ( 1, 'jishnu1'); is working fine. mydb=# CREATE TABLE tester3 mydb-# ( mydb(# "UN0" integer, mydb(# "UN1" VARCHAR(40) mydb(# ); CREATE TABLE mydb=# insert into tester3 (UN0, UN1) values ( 1, 'jishnu1'); ERROR: column "un0" of relation "tester3" does not exist mydb=# \d tester3 Table "public.tester3" Column | Type | Modifiers -------

INSERT INTO … FROM SELECT … RETURNING id mappings

非 Y 不嫁゛ 提交于 2020-01-10 02:50:06
问题 I'm using PostgreSQL 9.3. I want to duplicate some of the db records. Since I'm using an auto-increment pk id for the table, I want to get back the id mappings from the generated ids of duplicated records to the original ones. For example, say I have a table posts with 2 records in it: [{'id': 1, 'title': 'first'} , {'id': 2. 'title': 'second'}] With SQL: INSERT INTO posts (title) SELECT title FROM posts RETURNING id, ?? I expect to see mappings like: [{'id': 3, 'from_id': 1} , {'id': 4,