資料庫的一道面試題

2021-08-29 19:07:37 字數 3740 閱讀 3033

[color=red]建立表:[/color]

#學生表

create table if not exists s

(id varchar(10) primary key not null,

name varchar(20) not null

);#課程表

create table if not exists c

(id varchar(10) primary key not null,

name varchar(20) not null

);#sc表

create table if not exists sc

(sid varchar(10) not null,

cid varchar(10) not null

);#插入資料

insert into s(id, name) values(1, 's1');

insert into s(id, name) values(2, 's2');

insert into s(id, name) values(3, 's3');

insert into s(id, name) values(4, 's4');

insert into s(id, name) values(5, 's5');

commit;

insert into c(id, name) values(6, 'c1');

insert into c(id, name) values(7, 'c2');

insert into c(id, name) values(8, 'c3');

insert into c(id, name) values(9, 'c4');

insert into c(id, name) values(10, 'c5');

commit;

insert into sc(sid, cid) values(1, 6);

insert into sc(sid, cid) values(1, 7);

insert into sc(sid, cid) values(1, 8);

insert into sc(sid, cid) values(1, 9);

insert into sc(sid, cid) values(1, 10);

insert into sc(sid, cid) values(2, 7);

insert into sc(sid, cid) values(2, 8);

insert into sc(sid, cid) values(2, 9);

insert into sc(sid, cid) values(2, 6);

insert into sc(sid, cid) values(3, 6);

insert into sc(sid, cid) values(3, 8);

insert into sc(sid, cid) values(4, 9);

insert into sc(sid, cid) values(5, 6);

insert into sc(sid, cid) values(5, 7);

insert into sc(sid, cid) values(5, 8);

insert into sc(sid, cid) values(5, 9);

insert into sc(sid, cid) values(5, 10);

insert into sc(sid, cid) values(5, 10);

commit;

[color=red]表結構:[/color]

select * from s;

+----+------+

| id | name |

+----+------+

| 1 | s1 |

| 2 | s2 |

| 3 | s3 |

| 4 | s4 |

| 5 | s5 |

+----+------+

select * from c;

+----+------+

| id | name |

+----+------+

| 10 | c5 |

| 6 | c1 |

| 7 | c2 |

| 8 | c3 |

| 9 | c4 |

+----+------+

mysql> select * from sc;

+-----+-----+

| sid | cid |

+-----+-----+

| 1 | 6 |

| 1 | 7 |

| 1 | 8 |

| 1 | 9 |

| 1 | 10 |

| 2 | 7 |

| 2 | 8 |

| 2 | 9 |

| 2 | 6 |

| 3 | 6 |

| 3 | 8 |

| 4 | 9 |

| 5 | 6 |

| 5 | 7 |

| 5 | 8 |

| 5 | 9 |

| 5 | 10 |

| 5 | 10 |

+-----+-----+

#更新資料:

update s set id=5 limit 1;

update s set id=5 where name = c1;

#刪除資料:

#mysql允許使用delete和truncate語句刪除資料

delete from s where id = 5;

delete from s where id in (4, 5);

[color=red]問題及答案:[/color]

#求選擇了所有課程的學生

select * from s where(select count(*) from sc where sc.sid = s.id) = (select count(*) from c);

#求選擇了3門以上課程的學生

select * from s where(select count(*) from sc where sc.sid = s.id) > 3;

#求所有學生的所有課程

select s.name, c.name from sc join s on(s.id = sc.sid) join c on(c.id = sc.cid);

select s.name, c.name from s, c, sc where s.id = sc.sid and c.id = sc.cid;

一道面試題

一道面試題 射擊運動員10發打中90環有多少種可能,請編寫程式計算出來,並列印出結果,0環和10環均有效。打中90環就是沒打中10環,所以打中90環跟打中10環的可能性是一樣的。然後開始遞迴狂打槍,一到10就記錄 if params i 10 在迴圈的控制中已經排除了大於10的可能性 i 10 pa...

一道面試題

前些時候在找工作,就在準備結束此次找工作歷程的時候,去了一家公司面試,去了之後技術經理直接帶到一台電腦旁,給了一張紙條,上面是這樣的題目 用c或c 來實現 1 建立一棵樹,該樹的深度是隨機的,每個節點的位元組點數是隨機的。2 給每個節點分配一段隨機大小的記憶體空間,給每個節點賦乙個隨機數。3 遍歷這...

一道面試題

如果n為偶數,則將它除以2,如果n為奇數,則將它加1或者減1。問對於乙個給定的n,怎樣才能用最少的步驟將它變到1。例如 n 61 n 60 n 2 30 n 2 15 n 16 n 2 8 n 2 4 n 2 2 n 2 1 public class myclass public static vo...