mysql之4;

來源:互聯網
上載者:User

標籤:src   多對一   sql   date   代碼   _id   and   ade   png   

1表之間的關係:

2select查詢語句:

1表之間的關係

(1)多對一:(一個表裡的多條記錄對應另一個表裡的一個記錄)

 

建立多對一的關係需要注意
1 先建立被關聯的表,被關聯的欄位必須保證是唯一的
2 再建立關聯的表,關聯的欄位,一定要保證是可以重複的
ps:關聯的欄位一定是來自於被關聯的表對應欄位的值
代碼:
create table dep(#部門表id int primary key auto_increment, #被關聯的欄位必須保證是唯一的#id欄位是主鍵並且是自增的name varchar(20),#comment varchar(50));
create table emp(#員工表id int primary key auto_increment,name varchar(20),dep_id int, #關聯的欄位,一定要保證是可以重複的constraint fk_depid_id foreign key(dep_id)  references dep(id)#定義外鍵on update cascade#這個是表示這兩個是跟隨被關聯的表一起更改或者刪除的on delete cascade);

(2)一對一

create table user(#建立user表uid int primary key auto_increment,#主鍵且自增name varchar(15)#使用者的名字);insert into user(name) values#插入資料(‘frank1‘),(‘frank2‘),(‘frank3‘),
create table admin(#建立admin表id int primary key auto_increment,#主鍵且自增user_id int unique,#確保是獨一無二的password varchar(20),#密碼的多少foreign key(user_id) references user(uid)#關聯到user的uid確保foreign 是獨一無二的on update cascade#後面這個可以確保關聯的表可以跟著被關聯的一起改動或者刪除on delete cascade);
insert into admin(user_id,password) values
(1,‘frank3714‘),
(2,‘alfrank371asdf4‘)

(3)多對多關係

多對多代碼: 
create table author (#建立作者資訊id int primary key auto_increment, name char (10)); create table books (#建立書籍資訊id int primary key auto_increment,name char (10));create table books_authors(#建立一個關係表id int primary key auto_increment ,#主鍵且自增book_id int not null,#不為空白author_id int not null ,#不為空白unique(book_id,author_id),#聯合唯一foreign key (book_id) references books(id)#設定外鍵on delete cascade#這兩個是說明關聯表是跟隨被關聯表一起變動on update cascade ,foreign key (author_id ) references author(id)#設定外鍵on delete cascadeon update cascade );

 

2select查詢語句:

mysql:增、刪、改、查;  查是我們用到資料庫最多的就是查詢資料

我們以前學過基本的查詢語句:select * from  t1;

我們來看下一個where語句和(and,between and ,not ,or ,in、is 、like等)

select * from t1 where  id=5;

select name,age where    salary>1000 and salary <2000;可以寫成select name,age where salary between 1000 and 2000;

select name,age where  salary not between 1000and 2000;

select name,salary from employee where salary = 10000 or salary = 20000 or salary = 30000;
select name,salary from employee where salary in (10000,20000,30000);
select salary from employee where name like ‘%ank%‘; #like 是模糊比對 %可以代表任一字元(可以代表多個)
select salary from employee where name like ‘frank_‘;  #like 是模糊比對 _可以代表任意一個字元
select * from employee where dep_comment is Null;#is Null 判斷是否為空白;有值用=
select * from employee where dep_comment = Null;#這個是錯誤的
select * from employee where dep_comment is not Null;
group by 是分組(以什麼為分組)
select depart_id,count(id)  from employee group by depart_id;


 

mysql之4;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.