sql

How to insert data to multiple tables with foreign key dependencies involved (MySQL)

馋奶兔 提交于 2021-02-18 00:49:36
问题 I am looking for the best-practice way to insert data to multiple MySQL tables where some columns are foreign key dependencies. Here is an example: Table: contacts -------------------------------------------------------------------- | contact_id | first_name | last_name | prof_id | zip_code | -------------------------------------------------------------------- The 'contacts' table has PRIMARY KEY (contact_id) which simply auto_increments, and FOREIGN KEY (prof_id) REFERENCES 'profession'

MERGE - conditional “WHEN MATCHED THEN UPDATE”

梦想的初衷 提交于 2021-02-17 21:14:10
问题 The highlights in the image below shows the logic I want to implement. I realize the syntax is incorrect. Is there a way to conditionally update a record in a MERGE statement only if it the value of one of its columns in the target table is NULL, and the corresponding value in the source table is not null? How would you suggest re-writing this? MERGE dbo.input_311 AS [t] USING dbo.input_311_staging AS [s] ON ([t].[unique key] = [s].[unique key]) WHEN NOT MATCHED BY TARGET THEN INSERT(t.

Is Explicit Transaction Rollback Necessary?

*爱你&永不变心* 提交于 2021-02-17 21:11:36
问题 Many examples out there advocate explicit rollback of database transactions, along the lines of: using (var transaction = ...) { try { // do some reading and/or writing here transaction.Commit(); } catch (SqlException ex) { // explicit rollback transaction.Rollback(); } } However, I tend to do this: using (var transaction = ...) { // do some reading and/or writing here transaction.Commit(); } When an exception occurs, I'm just relying on the implicit rolling back of transactions that aren't

javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp] error with java scheduler

时光总嘲笑我的痴心妄想 提交于 2021-02-17 19:21:05
问题 What I'm trying to do is to update my database after a period of time. So I'm using java scheduler and connection pooling. I don't know why but my code only working once. It will print: init success success javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp]. at org.apache.naming.NamingContext.lookup(NamingContext.java:820) at org.apache.naming.NamingContext.lookup(NamingContext.java:168) at org.apache.naming.SelectorContext.lookup

SUM OVER PARTITION BY

喜你入骨 提交于 2021-02-17 07:50:09
问题 What am I missing? This query is returning duplicate data over and over again. The count is correct for a complete total, but I am expecting one row, and yet I am getting the value repeated about 40 times. Any ideas? SELECT BrandId ,SUM(ICount) OVER (PARTITION BY BrandId ) FROM Table WHERE DateId = 20130618 I get this? BrandId ICount 2 421762 2 421762 2 421762 2 421762 2 421762 2 421762 2 421762 1 133346 1 133346 1 133346 1 133346 1 133346 1 133346 1 133346 What am I missing? I cant remove

SQL | Two Schemas Questions [closed]

一世执手 提交于 2021-02-17 07:17:10
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 days ago . Improve this question I'm new to the schema part of sql so trying to explore and have doubts on the following, there are two schemas S1 and S2 with table T1 in S1 and T2 in S2: If a user logins to S1 will he be able to the following: Access table T2 How will you grant access to T2

How to create a MySQL hierarchical recursive query?

旧巷老猫 提交于 2021-02-17 07:05:12
问题 I have a MySQL table which is as follows: id name parent_id 19 category1 0 20 category2 19 21 category3 20 22 category4 21 ... ... ... Now, I want to have a single MySQL query to which I simply supply the id [for instance say id=19 ] then I should get all its child ids [i.e. result should have ids '20,21,22'].... The hierarchy of the children is not known; it can vary.... I know how to do it using a for loop... but how to achieve the same using a single MySQL query? 回答1: For MySQL 8+: use the

trigger for not inserting members doesnt work

情到浓时终转凉″ 提交于 2021-02-17 07:04:27
问题 I have this table CREATE TABLE members ( member_id INT PRIMARY KEY NOT NULL, first_name VARCHAR(20), last_name VARCHAR(20), web_page VARCHAR(200), e_mail VARCHAR(200), cv VARCHAR(800), dep_id INT, teacher_id INT ); and I want to create a trigger that if someone wants to insert a member which has a dep_id of 1 or 2 or 3 . And the teacher_id is different than NULL (as the teacher_id column is filled with either NULL or an id of another member) I came up with this CREATE TRIGGER employee_insup1

Update PO status by SQL query

拟墨画扇 提交于 2021-02-17 07:04:10
问题 I am trying to update the PO status from 'r' to 'c' Below is the extraction query. update purchase_order set status = 'c' where STATUS = 'r' and order_date < '2019-01-01'; It was ok to update but now it appears the error message. I wonder what happened? 回答1: Assuming the example select gives all of the rows you want to change and none of the rows you don't: update purchase_order set status = 'c' where STATUS = 'r' and order_date < '2020-01-01'; You have to do this in a separate statement from

trigger for not inserting members doesnt work

廉价感情. 提交于 2021-02-17 07:04:09
问题 I have this table CREATE TABLE members ( member_id INT PRIMARY KEY NOT NULL, first_name VARCHAR(20), last_name VARCHAR(20), web_page VARCHAR(200), e_mail VARCHAR(200), cv VARCHAR(800), dep_id INT, teacher_id INT ); and I want to create a trigger that if someone wants to insert a member which has a dep_id of 1 or 2 or 3 . And the teacher_id is different than NULL (as the teacher_id column is filled with either NULL or an id of another member) I came up with this CREATE TRIGGER employee_insup1