How to create a table from the attributes of another table that contain a key word? - sql

I have a SQL table called SCUBA_CLASSES with attributes ID and NAME. In the table are different kinds of scuba diving classes from beginner to advanced levels. There is a second table called REQUIRED_BOOKS that has the attributes ID and TITLE that contain all the books required by different scuba classes.
I'd like to create a new table called INTRO_REQUIREMENTS with an attribute ID. It will contain a list of all the books required by scuba classes whose name starts with the word "Introduction". How would you create this?
So far I have:
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID;
I can generate a list of classes with required books but can't figure out how to add the requirement that the name of the class has to start with "Introduction".

add one more condition in where clause using using AND and Search name using LIKE.
CREATE TABLE INTRO_REQUIREMENTS AS
SELECT SCUBA_CLASSES.ID
FROM SCUBA_CLASSES, REQUIRED_BOOKS
WHERE SCUBA_CLASSES.ID = REQUIRED_BOOKS.ID
AND name LIKE "introduction%";

Related

Add 1 or more email with append structure

We have a table that is filled by a program with a web service. This table has customer data like custno, name tele1, tele2, addr etc. They want to add emails but they do not know how many fields each customer has. They told me that we can handle this by adding an Append Structure to the table. I look in the internet but I can't find a way to do this with Append Structure.
What they want is to have 1 record for Elias with all his data and under this to put his 3 email.
Can we do it with Append Structure and how.
Thanks in advance
PS. This is an important correction. This is not a DB table but it is a structure. This Structure ws_customer is used as IMPORT in order to pass the customer data for creating or updating the sap customer. So they asked from me to add an APPEND STRUCTURE and add dynamically as much emails as the customer has. For example the customer ELIAS with CUSTNO 145 ADDRESS BROWN 6 has emails asimof#gmail.com, asimof#hol.gr and asimof#greece.gr. The Append Structure will have 1 field and for the record of this customer we will have 3 emails.
Is it possible to do this?
why don't you add a table of email adresses to this structure? in my oppinion, you could do it just the way JozsefSzikszai described it.
When you have your structure, you can append a table to this structure and then you can always add as much email adresses to this table as you want
a example could look like this:
TYPES BEGIN OF your_structure,
field1 TYPE string,
field2 TYPE string,
email TYPE STANDARD TABLE OF string,
END OF your_structure.
when you then have your variable of the type your_structure, you can add email adresses like this:
DATA your_variable TYPE your_structure.
INSERT 'email#adress.com' INTO TABLE your_variable-email.
When your_structure is a DDIC structure and not defined by code, you could append the table to the DDIC structure and use it just the same way

Get parent name and schema of a inherited table

Suppose I have a PostgreSQL table called master.products and another called account.products. The second one inherits from the first.
Is is possible to create a query to get the parent name and schema of the table account.products?
You get this information from the system catalog pg_inherits.
SELECT inhparent::regclass::text
FROM pg_catalog.pg_inherits
WHERE inhrelid = 'account.product'::regclass;
The name is automatically schema-qualified to make it unambiguous according to the current search_path .
SQL Fiddle.
Related:
Check if table inherits from other table in PostgreSQL
About regclass:
How to check if a table exists in a given schema

Creating index on subtype-specific attribute in a SQL Object table

I created a table of the object type 'Document'. This table contains multiple sub-types of Document (example: Recipe, Publication, Contract). These sub-types all contain common attributes (id, title, file size) but often contain additional attributes (i.e. stock number). I created indexes on the commonly searched common attributes, but also would like to create indexes on commonly searched sub-type specific attributes.
For example, I created an index for Title of the Documents table:
CREATE INDEX i_title
ON Documents (Title);
I would like to do something similar to the following:
CREATE INDEX i_stock_number
ON DOCUMENTS d (Stock_Number) WHERE VALUE(d) IS OF TYPE(Publication);
or possibly
CREATE INDEX i_stock_number
ON DOCUMENTS (TREAT(DOCUMENTS AS Publication).Stock_Number);
Could you help me determine how to create the indexes for sub-type specific attributes?
Thank you for your time.
create index i_stock_number
on documents d
(treat(value(d) as publication).stock_number);
Assuming a data model like this:
create or replace type document is object
(
id number,
title varchar2(100),
file_size number
) not final;
create or replace type publication under document
(
stock_number number
);
create table documents of document;
insert into documents values(publication(1, 'title', 100, 200));
commit;

How can I create a table that only allows data to insert if they are allowed

How can i create a table, that allows only to put data in NAME, if the data matches with the data that i want to be allowed in NAME. So like Bla1 or Bla2.
CREATE TABLE Table1 (
NAME VARCHAR(23)
NAME has to be one of them: ('Bla1', 'Bla2')
)
The best way to do it is probably to have a second table with all the allowed names in it, and making a FOREIGN KEY from the name field in your Table1 to the name field in that other table. That'll automatically fail any insert queries for which the name is not contained in the list of allowed names.
This has an advantage over things like ENUM and such in that it does not require you to rebuild your table (which is a very expensive operation) every time you want to allow another name and it also allows you to later add additional related info to each name by adding it to the other table.
Here's a great article on why using a foreign key is much better than using enums or other such checks in the table itself: http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/
Try this:
CREATE TABLE Table1 (
name VARCHAR(23) CHECK( name IN ('Bla1','Bla2') )
);

Update TAG Relational table from select match on main table with keywords field and tag table with individual keywords

I imported the Software PAD File Database from http://paddatabase.net/download.html into Microsoft Access in a table called main:
MAIN
-----
ID
ProgramName
Keywords
.
.
I created two new tables: Tags and TagSoftwareRel.
Tags
--------
ID
Tag
TagSoftwareRel
--------------
ID
SoftwareID <- (MainTable)
TagID <- tags table
I extracted all the keywords from the field Keywords as individual words in the Tags table. They Keywords field from Main looks like this:
Keywords
Paul, animated, moving monk, paulaner
Image,Imaging,Graphics,Rotate,Resize,Effects,
Sharpen,Blur,JPEG,TIFF,BMP,WBMP,PSD,GIF,PDF,Format,ICM,ICC,CMYK,
thumbnail,Convert,Display,AJAX,AVI,red-eye removal,lossless JPEG transform
correction, rich,internet,applications,ebooks,webmaster,authoring,
What I want to do is create a SQL Query which Inserts the tagID from the tags table into tagsoftwarerel.tagid and the related softwareID from the main table into tagsoftwarerel.softwareid by using a where tags.tag like main.keywords
I'm sort of at loss of where to start with this.
As it is a public DB I can provide a copy of the database to anyone interested.
Any assistance would be most appreciated. Thank you.
I assume that the field ID from the TagSoftwareRel is an autovalue or identity. That means the value is created automaticly by inserting a new row:
I understand that you already filled the Tags-Table.
Here is a query which would fill the TagSoftarerel-Table:
Insert into TagSoftarerel (SoftwareID, TagID)
Select m.Id,
(Select T.TagId from Tag T where T.Tag = m.Keyword) as TagId
from MAIN m
Advice:
I think you could find a better solution. The Tag-information is redundant in your solution.
If you would just add the Tag.Id to the main-table you could get rid of the Keyword column and store the tag-information solely in the tag table, where it belongs to.