python開發mysql:表關係&單表簡單查詢

來源:互聯網
上載者:User

標籤:delete   轉化   單表查詢   change   span   測試   uid   1.2   mys   

 

 一 一對多,多對一

 1 1.1 建立多對一 ,一對多的關係需要注意 2     先建立被關聯的表,被關聯的欄位必須保證時唯一的 3     在建立關聯的表,關聯的欄位一定是可以重複的 4  5 1.2 樣本; 6     出版社 多對一,多個老師可能在一家出版社 7     一夫多妻  一對多 8     create table dep(.  被關聯的欄位必須保證唯一 9     id int primary key auto_increment,10     name varchar(20),11     comment varchar(50)12     );13 14     create table emp(15     id int primary key auto_increment,16     name varchar(20),17     dep_id int, 關聯的欄位一定保證可以重複的18     constraint fk_depid_id foreign key(dep_id) references dep(id)19     foreign key(dep_id) 本表關聯欄位20     references 後面接指定關聯的表,一定是唯一的21     on update cascade22     on delete cascade23     );

 

二 一對一

 1 2.1 樣本. 使用者表,管理員表 2     create table user( 3     uid int primary key auto_increment, 4     name varchar(20) 5     ); 6     insert into user(name) values(‘egon‘); 7  8     create table admin( 9     id int primary key auto_increment,10     user_id int unique,  唯一11     password varchar(20),12     constraint foreign key(user_id) refreences user(uid) 被關聯的欄位一定是唯一的13     on update cascade14     on delete cascade15     );16     insert into admin(user_id,password) values(3,‘alex3714‘);17 18 2.2 注意關聯欄位與被關聯的欄位一定都是唯一的19 20 2.3 樣本  學生和客戶,客戶轉化為學生21     一個學生肯定是一個客戶,但是客戶不一定學生

 

三 多對多,雙向的多對一,就變成多對多

 1 3.1 樣本. 作者,書 2     create table book( 3     id int primary key auto_increment, 4     name varchar(20) 5     price varchar(20) 6     ); 7  8     create table book2author( 9     id int primary key auto_increment,10     book_id int,11     author_id int,12     constraint foreign key(book_id) references book(id),13     constraint foreign key(author_id) references author(id)14     on update cascade15     on delete cascade,16     unique(book_id,author_id)  聯合唯一17     );18 19     create table author(20     id int primary key auto_increment,21     name varchar(20)22     );

 

四 簡單單表查詢
 1 1 簡單查詢 2   select * from t1; 先找到表,在找到記錄,測試時候用 3   select name,id from t1; 4  5 2 where條件 and > < = != between or in is not 6   select name,id from t1 where id > 3; 先找表,在走條件,然後欄位 7   select name,id from t1 where id > 3 and id <1 0; 多條件 8   select name,id from t1 where id between 3 and 10; 在..之間 not between 9   select id from t1 where id=3 or id=4 or id=5;10   select id from t1 where id in (3,4,5);11   select id from t1 where id is Null;可以判斷是不是為空白‘‘並非空這麼簡單,只有Null才能用is判斷,‘‘用==判斷12   select name from t1 where name like ‘%n%‘;13   select name from t1 where name like ‘e__n‘; _代表是匹配一個14 15 3 group by分組16   select stu_id,group_concat(name) from t1 group by stu_id; 按照id分組17   group_concat(name) 看組裡面有哪些人,就需要這個彙總函式18   select stu_id,count(id) from t1 group by stu_id;  查看每個組裡面多個人19   select stu_id,max(id) from t1 group by stu_id; 查看每個組裡的最大id20   max min sum avg平均21 22 刪除欄位23   alter table t1 drop age;24   alter table t1 change id id(int3);        可以用modify替代25   alter table t1 add primary key(id,age);   將誰設定成主鍵26   alter table t1 drop primary key;          刪除主鍵

 

python開發mysql:表關係&單表簡單查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.