Oracle exsist Exercise 2

Source: Internet
Author: User

Table Structure

Create Table u
(
Name char (64) not null,
Id integer not null primary key
)
Tablespace Cici;

 
Comment on table u
Is 'user table ';

Create Table grou
(
Name char (64) not null,
Id integer not null primary key
)
Tablespace Cici;

 
Comment on table grou
Is 'permission group table ';

Create Table u_grou
(
G_id integer not null,
U_id integer not null
)
Tablespace Cici;

 
Comment on Table u_grou
Is 'relational table ';

Alter table u_grou
Add foreign key (g_id)
References grou (ID );
Alter table u_grou
Add foreign key (u_id)
References U (ID );

 

Data

Insert into u values ('u1', 1 );
Insert into u values ('u1', 2 );

Insert into grou values ('g1', 1 );
Insert into grou values ('g1', 2 );

Insert into u_grou values (1, 1 );
Insert into u_grou values (1, 2 );
Insert into u_grou values (2, 2 );

 

Query the permission groups of users whose user ID is 1

Select * From grou where exists (
Select * From u_grou where grou. ID = u_grou.g_id and exists (
Select * from u where u. ID = u_grou.u_id and U. ID = 1
)
);

 

 

Query the permission groups of users whose name is U1

Select * From grou where exists (
Select * From u_grou where exists (
Select * from u where u. Name = 'u1' and U. ID = u_grou.u_id
) And grou. ID = u_grou.g_id
);

 

Query Process

 --->Select * from u where u. Name = 'u1' and U. ID = 1If there is a value, the returned result is true, so there is data

--->Select * from u where u. Name = 'u1' and U. ID = 2If there is a value, the returned result is true, so there is data

==> All data in the u_grou table is found

Select * From grou where grou. ID = 1If there is a value, the returned result is true, so there is data

Select * From grou where grou. ID = 2If there is a value, the returned result is true, so there is data

==>GrouAll table data is detected

 

 Select * From grou where exists (
Select * From u_grou where exists (
Select * from u where u. Name = 'u1' and U. ID = u_grou.u_id andRownum = 1
) And grou. ID = u_grou.g_id
);

Query Process ??

Exist Essence

Http://zlk.iteye.com/blog/903322

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.