SQL table with a choice list (in pure SQL or with Java) - sql

I am willing to create myself a table in a database I make.
I don't know how to create a choice list to fill fields.
I explain my problem more specifically.
I want to obtain something like this
......................................................
table Employees (just example)
.......................................................
index / Name / Profession
1 / John / Manager ..........<= here is the choice list between all possible jobs we have
How can I do this?
I want that my Employee table is editable (like in a GUI interface in Java), and that the values of profession can be edited (meaning, we can select a different value from the choice list).
Many thanks in advance for your help!
Tom

I recommend to have 2 tables in DB. one for employee and one for jobs..
In UI (java) to have the business to populate the drop down from jobs table.. when you save the UI info you have to write in both tables if is necessary.
e.g. Employee table: Id, name, job_id and Job table with Id, name. we need 2 methods one to read from employee and one to read from jobs. Dropdown have 3 properties: Id (id from job table, display - name from job table and binding value - job_id from employee table)
the drop down should have the options to add new values..
when you save in the db if you have new values in the job you have to add those values in the db and link to the employees

Related

How can i update only some values of a table, which are not one after another or in sequence, with new ones in plsql?

Lets say a have a table of employees:
ID
PERSON_NAME
PERSON_telephonenumber
1
JOHN
525323
2
NICK
624534
3
GEORGE
134355
4
BILL
676346
How can i update only the person_telephonenumber of nick and bill with new ones that i have been given from an excel file?And are much more than two values.
Please help!
Thank you in advance
First step is to make the Excel spreadsheet with two columns into an Oracle table with two columns; let's call them OLD_NUMBER and NEW_NUMBER. Let's call this table EXCEL_EXPORT. Also, you didn't tell us the data type of phone numbers; let's say it's varchar2(15).
How to import data from Excel to Oracle is a different question; I assume you do that already, so you know how. (The "how" depends on what you use to interact with the database; for example, I use SQL Developer, and it has very simple tools for this kind of process.)
Then, as an important step, make OLD_NUMBER primary key in the table:
alter table excel_export modify (old_number primary key);
This allows you to update the existing table through a join:
update
( select e.person_telephonenumber, ee.new_number
from employees e join excel_export ee
on e.person_telephonenumber = ee.old_number )
set person_telephonenumber = new_number
;

MS Access Text Search Box with referenced values

Lets assume I have 2 tables: Projects and Employees. Each project got one responsible employee that I chose as a Lookup Value from the Employee table.
Now I understand that Access saves the ID of the employee in the project table and that's good. However, now I want to create a search query to get all projects with e.g. Mike as responsible person.
This is my code:
SELECT projects.name, projects.responsible
FROM projects
WHERE projects.responsible = Forms!form_search!employee_name;
Now it works fine if I type in the Form the employee ID but I cannot remember all IDs. I want to be able to type in 'Mike' in the Form and the query delivers me still all of Mike's projects.
How can I achieve that?
I thought about something like:
WHERE (projects.responsible = (employees.ID WHERE employees.name = Forms...));
But this doesn't work...
You can use a dropdown list with 2 columns as a filter criteria.
To fill this list, you make a request on your employee table, hide the first column (0cm wide) and the second column would display the matching employee name but your SQL request still receive the ID as criteria.
You should NOT have to modify your SQL request.

How to use SQL Server views with distinct clause to Link to a detail table?

I may be total standard here, but I have a table with duplicate values across the records i.e. People and HairColour. What I need to do is create another table which contains all the distinct HairColour values in the Group of Person records.
i.e.
Name HairColour
--------------------
Sam Ginger
Julie Brown
Peter Brown
Caroline Blond
Andrew Blond
My Person feature view needs to list out the distinct HairColours:
HairColour Ginger
HairColour Brown
HairColour Blond
Against each of these Person feature rows I record the Recommended Products.
It is a bit weird from a Relational perspective, but there are reasons. I could build up the Person Feature"View as I add Person records using say an INSTEAD OF INSERT trigger on the View. But it gets messy. An alternative is just to have Person Feature as a View based on a SELECT DISTINCT of the Person table and then link Recommended Products to this. But I have no Primary Key on the Person Feature View since it is a SELECT DISTINCT View. I will not be updating this View. Also one would need to think about how to deal with the Person Recommendation records when a Person Feature record disappeared since since it is not based on a physical table.
Any thoughts on this please?
Edit
I have a table of People with duplicate values for HairColour across a number of records, e.g., more than one person has blond hair. I need to create a table or view that represents a distinct list of "HairColour" records as above. Against each of these "HairColour" records I need link another table called Product Recommendation. The main issue to start with is creating this distinct list of records. Should it be a table or could it be a View based on a SELECT DISTINCT query?
So Person >- HairColour (distinct Table or Distinct View) -< Product Recommendation.
If HairColour needs to be a table then I need to make sure it has the correct records in it every time a Person record is added. Obviously using a View would do this automatically, but I am unsure whether you can can hang another table off a View.
If I understand correctly, you need a table with a primary key that lists the distinct hair colors that are found in a different table.
CREATE TABLE Haircolour(
ID INT IDENTITY(1,1) NOT NULL,
Colour VARCHAR(50) NULL
CONSTRAINT [PK_Haircolour] PRIMARY KEY CLUSTERED (ID ASC))
Then insert your records. If this is querying a table called "Person" it will look like this:
INSERT INTO Haircolour (Colour) SELECT DISTINCT HairColour FROM Person
Does this do what you are looking for?
UPDATE:
Your most recent Edit shows that you are looking for a many-to-many relationship between the Person and ProductRecommendation tables, with the HairColour table functioning as a cross reference table.
As ErikE points out, this is a good opportunity to normalize your data.
Create the HairColour table as described above.
Populate it from whatever source you like, for example the insert statement above.
Modify both the Person and the ProductRecommendation tables to include a HairColourID field, which is an integer foreign key that points to the PK field of the HairColour table.
Update Person.HairColourID to point to the color mentioned in the Person.HairColour column.
Drop the Person.HairColour column.
This involves giving up the ability to put free form new color names into the Person table. Any new colors must now be added to the HairColour table; those are the only colors that are available.
The foreign key constraint enforces the list of available colors. This is a good thing. Referential integrity keeps your data clean and prevents a lot of unexpected errors.
You can now confidently build your ProductRecommendation table on a data structure that will carry some weight.
Are you simply looking for a View of distinct hair colors?
CREATE VIEW YourViewName AS
SELECT DISTINCT HairColour
FROM YourTableName
You can query this view like a table:
SELECT 'HairColour: ' + HairColour
FROM YourViewName
If you are trying to create a new (temp) table, the syntax would look like:
SELECT Name, HairColour
INTO #Temp
FROM YourTableName
GROUP BY Name, HairColour
Here the GROUP BY is doing the same work that a DISTINCT keyword would do in the select list. This will create a temp table with unique combinations of "Name" and "HairColour".
You need to clear up a few things in your post (or in your mind) first:
1) What are the objectives? Forget about tables and views and whatever. Phrase your objectives as an ordinary person would. For example, from what I could gather from your post:
"My objective is to have a list of recommended products based on each person's hair colour."
2) Once you have that, check what data you have. I assume you have a "Persons" table, with the columns "Name" and "HairColour". You check your data and ask yourself: "Do I need any more data to reach my objective?" Based on your post I say yes: you also need a "matching" between hair colours and product ids. This must be provided, or programmed by you. There is no automatic method of saying for example "brown means products X,Y,Z.
3) After you have all the needed data, you can ask: Can I perform a query that will return a close approximation of my objective?
See for example this fiddle:
http://sqlfiddle.com/#!2/fda0d6/1
I have also defined your "Select distinct" view, but I fail to see where it will be used. Your objectives (as defined in your post) do not make this clear. If you provide a thorough list in Recommended_Products_HairColour you do not need a distinct view. The JOIN operation takes care of your "missing colors" (namely "Green" in my example)
4) When you have the query, you can follow up with: Do I need it in a different format? Is this a job for the query or the application? etc. But that's a different question I think.

reading from textfile and then inserting record to oracle table

Hi I have a table student which have few null fields in it. Student# is primary key in this table. I want to insert data into a table Officerecord by selecting data from student table, filling the null fields from a excel or doc file and inserting into Officerecord table.
Student
=======
Student # name Course age
1 Jess ENG 19
2 Jane 20
3 Kevin MAT 19
4 Rob 21
In above table course data is missing for student 2 and student 4. We have a course file which contains student # and course data. I want to read data from file and inserting this record to "Officerecord" Table which looks like as below:
Officerecord
===========
OFFICE# STUDENT# COURSE
While inserting data in this table i want no row should contain null value related to student.
Please help.
SQL Developer can import Excel files (.xls / .xlsx) directly into a table, but you will need to define the table structure ahead of time. It's as simple as right clicking the table in the list, selecting import data, selecting the file, then matching up the excel columns with the table columns.
Ideally you would use an external table for importing, but I don't believe you can easily maneuver spreadsheets using one.
In any event, once you have loaded both sets of data, you can do a simple update on your Student table to fill in the nulls.
UPDATE Student
SET (COURSE) =
( SELECT COURSE
FROM Officerecord
WHERE Student.Student# = Officerecord.Student# )
WHERE COURSE IS NULL;
Note that this will fail if you have two entries in Officerecord for a single student, but your example isn't clear with how you'd want to handle such a situation.
Copy the text from the source datafile and create a script from it which is a series of update statements.
A programmers file editor should help you here - recortd a macro that turns raw data in to statments of the form
update student set course = 'myCourse' where StudentNo = 99 and course is null;
you can create an external table for office_records file and join this table with students table and make necessary changes to input to final office_records table.
The advantage of external table is you can just replace the file when data changes, you need not go for loading the file manually.
This helps even if the user does not have the access/knowledge of inserting records from file using sql developer or Toad.

How to append two columns into one column in SQL?

I have two columns in my Table called as Workers - Technicians. In Workers I have name of workers, and in Technicians I have name of technicians who is working in company. Here is how it looks:
Workers Technicians
Andy Kevin
Conan Jason
Jay Ray
Donald Daryl
Martin .
Mark .(rows goes on)
.
.(rows goes on)
What I want to do is to append these rows and having a column called Employees. Here what i want:
Employees
Andy
Conan
Jay
Donald
Martin
Mark
Kevin
Jason
Ray
Daryl
.
.
I don't want to create another table, I just want to add a new column. How would i do this?
An union statement should do the job here. Something like this:
select Name from Workers-Technitians where Worker not null
UNION
select Name from Workers-Technitians where Technitian not null
Keep in mind that both queries must have an equal column count when using an union
Assuming your table is named "staff", you could do something like this.
select distinct employees from (
select workers as employees from staff s1
union select technicians as employees from staff s2
) as emps
Perhaps you need to take another look at your design, though. Adding a third column won't work out for you because the first two columns represent two rows. In fact, there already appears to be a forced relationship between the workers and technicians-- they seem to be bundled together in the same record for no particular reason.
Consider starting fresh with an Employee table, with one employee per row, and abandon the existing table. You can maintain employee attributes in that table if the requirements are simple, or join a separate table of attributes for more complex cases. In either case, normalizing your data will make your life easier and use of a union to join columns will be unnecessary.
I also don't understand fully what you want to do (mainly because of this: if, say, a row has "Andy" in Workers and "Kevin" in Technicians, what will you put in Employees? Andy or Kevin?), but to add a new column to the table and then fill it with data you need to:
Add it (assuming your table is named Table and 100 chars are enough for the new column):
alter table [Table] add [Employees] nvarchar(100) null
Fill it:
update [Table] set [Employees] = [Workers]
-- or:
update [Table] set [Employees] = [Technicians]
I believe what you really want is a new table, Employees, and to change the Workers and Technicians into relationships drawn from it. For that, see the link Simon gave you about database normalization.
I don't entirely understand your question but you don't want to add an additional column, this introduces (more) data redundancy and will make any future updates or querying more difficult. Use one of the SELECT query solutions given and seriously consider re-factoring your two existing separate columns into a single column. I suggest you take a look at database normalisation if you're not already familiar with the concept.