473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Add new colums to existing view

5
hi all,

Is it possible to add new column to existing view? For example:
let us have three tables:
table1 with columns(c1,c2 ,c3 )
table2 with columns (cc1,cc2, cc3)
table3 with columns(ccc1,ccc2)

we have a view "View1" with columns say c1, cc1
Now is it possible to add new column ccc2 to the existing view "View1"

thanks
Feb 24 '09 #1
9 21985
wizardry
201 100+
yes you can just use: alter view select as column names but only if you defined the view.

here is the docs for this.

http://dev.mysql.com/doc/refman/5.0/en/alter-view.html
Feb 24 '09 #2
bidur
5
hi wizardry ,
thanks for your reply.
I went through the link. I m still confused.
It would be easy if you could provide a simple example

thanks
Feb 25 '09 #3
wizardry
201 100+
you can use the alter statement as well where create is and add your new columns; don't forget to include the old ones as well because you have to recreate the existing view, with the new columns instead of what a typical alter statement does.
Expand|Select|Wrap|Line Numbers
  1. create view t3 as select a.t1_id, a.t2_id_fk, a.t2_dt, b.t2_id, b.t2_name, b.t2_date from t1 a, t2 b;
  2.  
ok t3 is your view name, then you want to use alias for your columns you can use a.t1 or table name.coulmn name i.e. t1.t1_id i prefer and suggest using the alias to create a good programming practice. alias is a.t1_id

then in your from statement tablename a

you assign your table name the alias used.


you can create a view of up to 64 table joins in the current version of mysql.

but thats not just on views its table joins period.

what are you using the view for? are you manipulating data in this view?

let me know if this helps!
Feb 25 '09 #4
bidur
5
Hi wizardry,
I am looking if views could be useful or efficient use of joins could solve my problem.
I have got different tables with the following definition:(simplified for now)
name with(nid,fid,eid,name)
email with(eid,fid,etype,email)
family with(fid)
name_email with(nid,eid)

Here, we have email associated with each name. For now I have three different cases to handle

case 1.emails can be current emails shown in the name table i.e. eid field
query:
SELECT name.nid,name.name,name.e_id
FROM name

case 2.emails can be personal emails shown in the name_email table i.e. nid-eid pair
query:
SELECT name.nid,name.name, name_email.e_id
FROM name
LEFT JOIN name_email ON name.nid=name_email.n_id

case 3.emails can be family emails which is linked by the fid
query:
SELECT name.nid,name.name,email.email
FROM name
LEFT JOIN family ON name.fid = family.fid
LEFT JOIN email ON family.fid = email.fid

These query work fine if they we select only emails.
But for case 1 and case 2 if we SELECT more fields for e.g. etype then teh result gets mixed with some other unwanted results.
This is because of joins.
In this scenario my query are
case 1.(shows unwanted result mixed with desired result)
query:
SELECT name.nid,name.name,email.etype,name.eid
FROM name
LEFT JOIN family ON name.fid = family.fid
LEFT JOIN email ON family.fid = inf.email.fid

case 2.(shows unwanted result mixed with desired result)
query:
SELECT name.nid,name.name,email.etype, name_email.eid
FROM name
LEFT JOIN name_email ON name.id=name_email.nid
LEFT JOIN family ON name.fid = family.fid
LEFT JOIN email ON family.fid = email.fid

case3.

query:
SELECT name.nid,name.name,email.email,email.etype
FROM name
LEFT JOIN family ON name.fid = family.fid
LEFT JOIN email ON family.fid = email.fid
Feb 26 '09 #5
Hi,

if the table anv and bpo has any matching columns with anv then full outer join and insert into mybpo

or else u can insert like this

insert into mybpo ( columnname)
select bpo from anv

and
insert into mybpo ( columnname)
select usr from bpo
Feb 26 '09 #6
wizardry
201 100+
can you post your code for the tables?

reason its pulling the wrong results is because of your querys your wanting to pull data from three tables where their ids = id. a left join is going to return all results for the parent table were child table = clause.

inner join returns where id = id and all columns.

outter join is similar to inner but it is defined by the third table.

right join is the opposite of left join.

paste your code for the tables, so i can look at the structure and ensure your table constraint are fine, and that will allow me to see about your querys in more depth.

one thing you could try is instead of defing your join in your select statements is to use an alias like i mention before, the engine is smart enough to determine what join it needs to run.

i.e.

Expand|Select|Wrap|Line Numbers
  1. select a.bid, a.name, b.email, b.type, c.fid, a.fid, b.fid
  2. from
  3. name a,
  4. email b,
  5. family c
  6. where a.fid=b.fid,
  7. and a.fid=c.fid;
  8.  
  9.  
Feb 26 '09 #7
bidur
5
Hi wizardry,
thanks for your reply.
the codes for the tables goes as follows:
--
-- Table structure for table `family`
--
Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE TABLE `family` (
  3.   `fid` int(11) NOT NULL auto_increment,
  4.   PRIMARY KEY  (`fid`)
  5. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;# MySQL returned an empty result set (i.e. zero rows).
  6.  
  7.  
  8. -- 
  9. -- Table structure for table `email`
  10. -- 
  11.  
  12. CREATE TABLE `email` (
  13.   `eid` int(11) NOT NULL auto_increment,
  14.   `fid` int(11) NOT NULL default '0' COMMENT 'Links email addresses for related people',
  15.   `email` varchar(40) NOT NULL default '',
  16.   `etype` enum('Contact','Family','Office','Personal') NOT NULL default 'Personal',
  17.   PRIMARY KEY  (`eid`),
  18.   KEY `email_family` (`fid`)
  19. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=86 ;# MySQL returned an empty result set (i.e. zero rows).
  20.  
  21. -- 
  22. -- Constraints for table `email`
  23. -- 
  24. ALTER TABLE `email`
  25.   ADD CONSTRAINT `email_family` FOREIGN KEY (`fid`) REFERENCES `family` (`fid`);# MySQL returned an empty result set (i.e. zero rows).
  26.  
  27.  
  28. -- 
  29. -- Table structure for table `name`
  30. -- 
  31.  
  32. CREATE TABLE `name` (
  33.   `nid` int(11) NOT NULL auto_increment,
  34.   `fid` int(11) NOT NULL default '0' COMMENT 'Links related people',
  35.   `eid` int(11) default NULL ,
  36.   `name` varchar(50) NOT NULL default '',
  37.   `gender` enum('Male','Female') NOT NULL default 'Male',
  38.    PRIMARY KEY  (`nid`),
  39.   KEY `name_email` (`eid`), 
  40.   KEY `name_family` (`fid`) 
  41. ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=53 ;# MySQL returned an empty result set (i.e. zero rows).
  42.  
  43. -- 
  44. -- Constraints for table `name`
  45. -- 
  46. ALTER TABLE `name`
  47.  
  48.   ADD CONSTRAINT `name_ibfk_11` FOREIGN KEY (`eid`) REFERENCES `email` (`eid`),
  49.   ADD CONSTRAINT `name_ibfk_8` FOREIGN KEY (`fid`) REFERENCES `family` (`fid`);# MySQL returned an empty result set (i.e. zero rows).
  50.  
  51. -- 
  52. -- Table structure for table `name_email`
  53. -- 
  54.  
  55. CREATE TABLE `name_email` (
  56.   `nid` int(11) NOT NULL default '0' COMMENT 'Links email addresses for a person',
  57.   `eid` int(11) NOT NULL default '0',
  58.   KEY `name_email_name` (`nid`),
  59.   KEY `name_email_email` (`eid`)
  60. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;# MySQL returned an empty result set (i.e. zero rows).
  61.  
  62. -- 
  63. -- Constraints for table `name_email`
  64. -- 
  65. ALTER TABLE `name_email`
  66.   ADD CONSTRAINT `name_email_ibfk_1` FOREIGN KEY (`nid`) REFERENCES `name` (`nid`),
  67.   ADD CONSTRAINT `name_email_ibfk_2` FOREIGN KEY (`eid`) REFERENCES `email` (`eid`);# MySQL returned an empty result set (i.e. zero rows).
  68.  
  69.  
Feb 27 '09 #8
wizardry
201 100+
ok i see what your problem is its the constraints what your trying to do is a one to one relationship between these tables with family as parent 1 => 1 email, name, name_email i will post the code in a moment. You might need to restructure your database some.

Expand|Select|Wrap|Line Numbers
  1.       7 drop table if exists `family`;$
  2.       8  $
  3.       9 CREATE TABLE `family` ( $
  4.      10   `fid` int(11) NOT NULL auto_increment, $
  5.      11   PRIMARY KEY  (`fid`) $
  6.      12 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=31 ;# MySQL returned an empty result set (i.e. zero rows). $
  7.      13  $
  8.      14 insert into `family` (`fid`) values ('1');$
  9.      15 insert into `family` (`fid`) values ('2');$
  10.      16 insert into `family` (`fid`) values ('3');$
  11.      17 insert into `family` (`fid`) values ('4');$
  12.      18 insert into `family` (`fid`) values ('5');$
  13.      19 $
  14.      20  $
  15.      21  drop table if exists `email`;$
  16.      22 $
  17.      23 CREATE TABLE `email` ( $
  18.      24   `eid` int(11) NOT NULL auto_increment, $
  19.      25   `fid` int(11) NOT NULL default '0' COMMENT 'Links email addresses for related people', $
  20.      26   `email` varchar(40) NOT NULL default '', $
  21.      27   `etype` enum('Contact','Family','Office','Personal') NOT NULL default 'Personal', $
  22.      28   PRIMARY KEY  (`eid`), $
  23.      29   KEY `email_family` (`fid`),$
  24.      30 foreign key(`fid`)$
  25.      31 references `family`(`fid`)$
  26.      32 on delete cascade$
  27.      33 on update cascade $
  28.      34 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=86 ;# MySQL returned an empty result set (i.e. zero rows). $
  29.      35  $
  30.      36 insert into `email` (`eid`, `fid`, `email`, `etype`) values ('1', '1', 'email1@test.com', 'Personal');$
  31.      37 insert into `email` (`eid`, `fid`, `email`, `etype`) values ('2', '2', 'email2@test.com', 'Personal');$
  32.      38 insert into `email` (`eid`, `fid`, `email`, `etype`) values ('3', '3', 'email3@test.com', 'Personal');$
  33.      39 insert into `email` (`eid`, `fid`, `email`, `etype`) values ('4', '4', 'email4@test.com', 'Personal');$
  34.      40 insert into `email` (`eid`, `fid`, `email`, `etype`) values ('5', '5', 'email5@test.com', 'Personal');$
  35.      41 $
  36.      42 $
  37.      43  $
  38.      44 CREATE TABLE `name` ( $
  39.      45   `nid` int(11) NOT NULL auto_increment,
  40.      46   `fid` int(11) NOT NULL default '0' COMMENT 'Links related people', $
  41.      47   `eid` int(11) default NULL , $
  42.      48   `name` varchar(50) NOT NULL default '', $
  43.      49   `gender` enum('Male','Female') NOT NULL default 'Male', $
  44.      50    PRIMARY KEY  (`nid`), $
  45.      51   KEY `name_email` (`eid`),  $
  46.      52   KEY `name_family` (`fid`),$
  47.      53 foreign key(`fid`)$
  48.      54 references `family`(`fid`)$
  49.      55 on delete cascade$
  50.      56 on update cascade  $
  51.      57 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=53 ;# MySQL returned an empty result set (i.e. zero rows). $
  52.      58  $
  53.      59 insert into `name` (`nid`, `fid`, `eid`, `name`, `gender`) values ('1', '1', '1', 'test1', 'male');$
  54.      60 insert into `name` (`nid`, `fid`, `eid`, `name`, `gender`) values ('2', '2', '2', 'test1', 'male');$
  55.      61 insert into `name` (`nid`, `fid`, `eid`, `name`, `gender`) values ('3', '3', '3', 'test1', 'male');$
  56.      62 insert into `name` (`nid`, `fid`, `eid`, `name`, `gender`) values ('4', '4', '4', 'test1', 'male');$
  57.      63 insert into `name` (`nid`, `fid`, `eid`, `name`, `gender`) values ('5', '5', '5', 'test1', 'male');$
  58.      64 $
  59.      65  $
  60.      66   $
  61.      67 CREATE TABLE `name_email` ( $
  62.      68   `ne_id` int(11) NOT NULL auto_increment COMMENT 'primary key', $
  63.      69   `nid_fk` int(11) NOT NULL default '0' COMMENT 'Links email addresses for a person', $
  64.      70   `eid_fk` int(11) NOT NULL default '0', $
  65.      71 primary key (`ne_id`), $
  66.      72  KEY `name_email_name` (`nid_fk`), $
  67.      73   KEY `name_email_email` (`eid_fk`),$
  68.      74 foreign key(`nid_fk`) $
  69.      75 references `name`(`nid`)$
  70.      76 on delete cascade$
  71.      77 on update cascade,$
  72.      78 foreign key(`eid_fk`)$
  73.      79 references `email`(`eid`)$
  74.      80 on delete cascade$
  75.      81 on update cascade $
  76.      82 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;# MySQL returned an empty result set (i.e. zero rows). $
  77.      83  $
  78.      84 $
  79.      85 insert into `name_email`(`ne_id`, `nid_fk`, `eid_fk`) values ('1', '1', '1'); $
  80.      86 insert into `name_email`(`ne_id`, `nid_fk`, `eid_fk`) values ('2', '2', '2'); $
  81.      87 insert into `name_email`(`ne_id`, `nid_fk`, `eid_fk`) values ('3', '3', '3'); $
  82.      88 insert into `name_email`(`ne_id`, `nid_fk`, `eid_fk`) values ('4', '4', '4'); $
  83.      89 insert into `name_email`(`ne_id`, `nid_fk`, `eid_fk`) values ('5', '5', '5');
  84.      147 create VIEW `ev` AS select `a`.`email` AS `email`,`a`.`etype` AS `etype`,`b`.`nid` AS `nid`,`b`.`name` AS `name`,`b`.`gender` AS `gender` from (`email` `a` join `name` `b`) where (`a`.`fid` = `b`.`fid`) */;$
  85.     148 $
  86.     149 -- Dump completed on 2009-02-27 22:29:32$
  87.  
  88.  

i hope this helps.

from what i was reading in your code you wanted a one on one but with the the name_email table you wanted a one two many.

I don't know how you wanted to use that one two many on name_email unless you have other tables that the join table connects to. but anyhow if you want a one to many on the other tables just rename the constraint foreign key to be a _fk on the end of the name. and modify the constraint id's.

can you explain what exactly you want to do with these tables relationship wise and if they connect to another table for the join table?
Feb 27 '09 #9
bidur
5
Hi wizardry,
thanks a lot for your answers.
As I have explained earlier in the post. Here, we have email associated with each name and have to handle three different cases .

case 1.emails can be current emails for a person shown in the name table i.e. name.eid field

case 2.emails can be personal emails shown in the name_email table i.e.
name_email.nid-name_email.eid pair
For example Tom is a name with nid value 3 , now name_email will have all the nid-eid pairs for nid=3 i.e all the pairs with nid=3 are Tom's personal emails

case 3.emails can be family emails which is linked by the fid
for example.There is a family of Tom with wife Rita. Both will be having same fid but different nid And this third case should select all the emails from email table as per the fid. For this teh name table joins to name_email table and then to email table by tracing the fid in each table.

thank you
Mar 1 '09 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Benito | last post by:
Hi Everyone, Is there an easy way that I could pass a filter (variable) unto a view via stored procedures? I would appreciate your suggestions. Benito
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
2
by: shsandeep | last post by:
Can we add constraints to an existing view? DB2 V8.1 AIX 5.3 Cheers, San.
0
by: zafar | last post by:
I don't know what property should be used for hiding colums in Data Grid, whereas in Data Grid View we have DataGridView1.Colums(index).Visible = False , But how can I hide Colums in Data Grid.....
2
by: nkumarin001 | last post by:
Hi, Can any one please help me in this matter. I have created a table :- create table Employee ( id number, name varchar2(10), city varchar2(10)
1
sujathaeeshan
by: sujathaeeshan | last post by:
Hi all, Is there any functions or built-in property values exist that shoudn't allow the coumbo box or grid colums to edit.It has to show only the existing values which is stored in coumbo...
2
sujathaeeshan
by: sujathaeeshan | last post by:
Hi all, Is there any functions or built-in property values exist that shoudn't allow the coumbo box or grid colums to edit.It has to show only the existing values which is stored in coumbo...
60
by: Bill Cunningham | last post by:
I have a row of values like such, placed in a text file by fprintf. 10.50 10.25 10.00 10.75 11.00 What I want to do to the above colum is add a new column right beside it which is a total...
1
by: rani g | last post by:
whenever we insert a new row into the grid view control then existing view will be move down and new row will be added onto the the existing .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.