我对关系数据库比较陌生,我不确定我想做什么是可能的。如果有人可以告诉我该怎么做或者至少指出我正确的方向,
我有table_1
NameId SelectedName
1 A
3 C
6 F
NameId
是FOREIGN KEY
REFERENCES table_2 (Id)
我有table_2
Id Name
1 A
2 B
3 C
4 D
5 E
6 F
我想设置我的数据库,以便只要table_2
中的条目更新,table_1
将更改其Name
列的值,如果条目来自table_2
table_2
就在其中。例如,如果我在Id Name
3 C_EDITED
table_1
此更改也会反映在public class LanguageChoice extends AppCompatActivity {
private Spinner
spWords,
spTrans;
private Button btnApply;
String language_choice;
String translation_choice;
String dict_source;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language_choice);
btnApply = (Button) findViewById(R.id.aplBtn);
spWords = (Spinner) findViewById(R.id.vocabSpin);
spTrans = (Spinner) findViewById(R.id.transSpin);
//when apply button is clicked, move to choose mode activity
btnApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
importFile();
}
});
chooseLanguage();
}
private void chooseLanguage(){
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> WAdapter = ArrayAdapter.createFromResource(this,
R.array.languages, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> TAdapter = ArrayAdapter.createFromResource(this,
R.array.translations, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
WAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
TAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spWords.setAdapter(WAdapter);
spTrans.setAdapter(TAdapter);
//spinner for input language
spWords.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
language_choice = spWords.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
});
//spinner for output language
spTrans.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
translation_choice = spTrans.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
});
//define the name of the file with the words-translation pairs
dict_source = ("C:\\B1\\" + language_choice + "-" + translation_choice).toLowerCase() + ".txt";
}
public void importFile() {
//first import the correspondent dictionary
String filePath = dict_source;
Log.i("import from", filePath);
try {
String line;
BufferedReader reader = new BufferedReader(new FileReader(filePath));
while ((line = reader.readLine()) != null) {
String[] parts = line.split(" - ", 3);
if (parts.length == 3) {
String key = parts[0];
String value = parts[2];
this.wordsMap.put(key, value);
} else {
Log.i("Import:", "ignoring line: " + line);
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
你可能不希望这样做......
表格看起来没有标准化,因此如果您只是在table_2
中重复table_1
的值,则可能会出现数据冗余和完整性问题。因此,如果没有table_1
列,SelectedName
可能会更好,因为您可以随时在表格_2中查找Name
的值,例如。
SELECT t1.NameId, t2.Name
FROM table_1 t1 INNER JOIN table_2 t2 ON (t1.NameId = t2.Id);
但是,如果我们假设Id
和Name
是您的主要密钥(即您有(1,'A'),(1,'B')等记录,表2中的(1,'Anything')然后PK将在两个列上定义,引用该表的FK也是如此。这个SQLfiddle显示它是如何工作的,但它确实要求你更改table_2
上的PK和table_1
上的FK,并使用其他一些评论中提到的ON UPDATE CASCADE方法。
答案 1 :(得分:1)
create table table_2(
id int,
name varchar unique,
primary key (id)
);
create table table_1(
nameid int ,
selectedname varchar foreign key references table_2(name) on update cascade,
);
insert into table_2 values
(1,'A'),
(2,'B'),
(3,'C'),
(4,'D'),
(5,'E'),
(6,'F')
insert into table_1 values
(1,'A'),
(3,'C'),
(6,'F')
select * from table_1;
select * from table_2;
update table_2 set name = 'X' where id = 1;
select * from table_2;
--Note X
答案 2 :(得分:0)
我不明白为什么你在table_1中保留选定的列。但仍然
update table_1 inner join table_2
on tabel_2.id=table_1.nameid
set selectedname =table_2.name