1. 程式人生 > >python 基礎復習之數據庫02

python 基礎復習之數據庫02

utf8 color utf 多對多 查看表 就是 其中 dsd 列名

  1 """
  2 什麽時候用主鍵?主鍵的作用?
  3     保證數據的唯一性
  4     一張表只能有一個主鍵
  5     一個主鍵只能是一列嗎?錯的 可以有多列
  6     
  7 例子:
  8 create table t1(
  9     nid int(11) not null auto_increment primary key,
 10     pid int(11) default null,
 11     num int(11) default null
 12 )engine=innodb default charset=utf8;
 13 
 14
create table t1( 15 nid int(11) not null auto_increment, 16 pid int(11) not null, 17 num int(11), 18 primary key (nid,pid) # 主鍵也可以這樣設置 19 )engine=innodb default charset=utf8; 20 21 create table t2( 22 id int auto_increment primary key, 23 name char(10), 24 id1 int,
25 id2 int, 26 constraint fk_t1_t2 foreign key (id1,id2) references t1(nid,pid) 27 )engine=innodb default charset=utf8; 28 29 # 註意:創建多個外鍵進行關聯的時候,可以使用以上方法,主鍵有兩列, 30 31 數據行: 32 insert into tb1(name,age) values ("alex",18); 33 delete from tb1; 34 truncate table tb1; 35 36
delete from tb1 where id > 8 37 38 update tb1 set name="root" where id > 8 39 40 select * from tb; # 註:一般不用*號 ,* 的效率低 41 select id,name from tb; 42 43 對於自增: 44 desc 表名;# 查看表裏的字段類型 以及是否為空 45 show create table 表名;# 查看這個表是如何創建的。 46 show create table 表名 \G;展現出來的格式比較規範。 47 alter table 表名 AUTO_INCREMENT=2; # 修改自增起始值 兩個兩個增加 48 # 註 如果AUTO_INCREMENT=8 網小改就不行了,只能往大了改 49 50 MySQL:自增步長 51 基於會話級別: 52 一次登錄就是一次會話 53 54 show session variables like ‘auto_inc%‘; # 查看會話的步長 55 set session auto_increment_increment=2;設置會話的步長為2 56 57 58 0,唯一索引 59 create table t1( 60 id int auto_increment primary key, 61 num int, 62 unique uq1 (num) # 唯一索引 63 )engine=innodb default charset=utf8; 64 65 create table t1( 66 id int auto_increment primary key, 67 num int, 68 xx int, 69 unique uq1 (num,xx) # 聯合唯一索引 70 )engine=innodb default charset=utf8; 71 72 1,外鍵的變種 73 a, 用戶表和部門表 74 用戶: 75 1 alex 1 76 2 root 1 77 3 egon 2 78 4 laoyao 3 79 80 部門: 81 1 服務 82 2 保安 83 3 公關 84 ====》一對多 85 b, 用戶表和博客表 86 用戶: 87 1 alex 88 2 root 89 3 egon 90 4 laoyao 91 博客表: FK() + 唯一索引 92 1 /qazwsx/ 4 (laoyao的博客) 93 2 /qwewerf/ 1 (alex的博客) 94 3 /sadsads/ 3 (egon的博客) 95 4 /dsafdsd/ 2 (root的博客) 96 ====》一對一 97 用戶表和用戶登陸表 一對一 98 c, 用戶表(百合網) 相親記錄表 99 多對多 100 101 一對一: 102 create table userinfo( 103 id int auto_increment primary key, 104 name varchar(32), 105 gender char(12), 106 email varchar(64) 107 )engine=innodb default charset=utf8; 108 109 110 create table admin( 111 id int not null auto_increment primary key, 112 username varchar(64) not null, 113 password varchar(64) not null, 114 user_id int not null, 115 unique uq_user (user_id), 唯一索引 116 constraint fk_admin_uinfo primary key (user_id) references userinfo(id) 117 )engine=innodb default charset=utf8; 118 119 120 多對多 121 create table userinfo( 122 id int not null auto_increment primary key, 123 name varchar(12), 124 gender varchar(12) 125 )engine=innodb default charset=utf8; 126 127 create table pri_host( 128 id int not null auto_increment primary key, 129 host_name varchar(32) 130 )engine=innodb default charset=utf8; 131 132 133 create table user_host_re( 134 id int not null auto_increment primary key, 135 userid int not null, 136 hostid int not null, 137 unique uq_user_host(userid,hostid), 138 constraint fk_user_re foreign key (userid) references userinfo(id), 139 constraint fk_user_host foreign key (hostid) references pri_host(id) 140 )engine=innodb default charset=utf8; 141 142 143 SQL語句數據行操作補充 144 145 create table tb11( 146 id int auto_increment primary key, 147 name varchar(32), 148 age int 149 )engine=innodb default charset=utf8; 150 151 create table tb12( 152 id int auto_increment primary key, 153 name varchar(32), 154 age int 155 )engine=innodb default charset=utf8; 156 157 158 增: 159 insert into tb11(name,age) values("alex",12); 160 161 insert into tb11(name,age) values("alex",13),("sam",15); 162 163 insert into tb12(name,age) select name,age from tb11; 164 # (把表tb11中的數據插入表tb12中) 165 166 刪: 167 delete from tb12; 168 delete from tb12 where id = 2 169 delete from tb12 where id != 2 170 delete from tb12 where id >= 2 171 delete from tb12 where id < 2 or name="alex" 172 173 改: 174 update tb12 set name="alex" where id > 12 and name="xx" 175 # (把id大於12 且名字叫“xx”的,改成“alex”) 176 update tb12 set name="alex",age=19 where id > 12 and name="xx" 177 178 查: 179 select * from tb12; (展示tb12表中所有的數據) 180 181 select id,name from tb12; (展示id,和 name 列中的數據) 182 183 select id,name from tb12 where id > 10 or name ="xxx"; 184 185 select name,age,11 from tb12; 186 #(會增加一個常數列,這一列都是11) 187 188 其他: 189 select * from tb12 where id != 1 190 select * from tb12 where id <> 1 != <> 都是不等於 191 select * from tb12 where id in (1,5,12); 192 # (展示tb12 中id在(1,5,12)中的數據) 193 select * from tb12 where id not in (1,5,12); 194 select * from tb12 where id between 5 and 12; 195 # (展示id在5~12之間的數據) 196 197 select * from tb12 where id in (select id from tb11) 198 # 先看括號裏的 199 200 201 % _ 都是通配符 202 select * from tb12 where name like "%a" (以a結尾) 203 select * from tb12 where name like "a%" (以a開頭) 204 select * from tb12 where name like "a_" (以a結尾) 205 206 select * from tb12 limit 10; # 查看前10條數據 207 select * from tb12 limit 1,1; 第一個1 代表起始位置,第二個 208 # 位置代表取幾個 209 210 211 select * from tb12 limit 10 offset 20; 212 # offset 後面的數字表示從哪裏開始,limit後面的表示取幾個。 213 214 排序: 215 select * from tb12 216 # 默認順序從小到大排。 217 218 select * from tb12 order by id desc; 219 # (按照id從大到小排) 220 select * from tb12 order by id asc; 221 # (按照id從小到大排) 222 223 select * from tb12 order by id desc limit 10; 224 # (取後10條數據) 225 226 select * from tb12 order by age desc,id desc; 227 # (先按照年齡從大到小排,如果年齡有相等的,就按照id的從大到小排) 228 229 create table department5( 230 id int auto_increment primary key, 231 title varchar(32) 232 )engine=innodb default charset=utf8; 233 insert into department5(title) values("公關"),("公共") 234 #,("公公"),("關關") 235 236 237 create table userinfo5( 238 id int auto_increment primary key, 239 name char(32), 240 part_id int, 241 constraint fk_user_depar foreign key (part_id) ref 242 # erences department5(id) 243 )engine=innodb default charset=utf8; 244 insert into userinfo5(name,part_id) values("楊涵",3), 245 # ("駱駝",2),("老虎",1),("蜘蛛",1); 246 247 248 分組: 249 group by 以...進行分組 250 select * from userinfo5 group by part_id; 251 # (以part_id進行分組) # 如果其中有兩個重復,不知道 252 #該留誰,所以會報錯 253 254 所以你得指定都拿誰, 255 select part_id from userinfo5 group by part_id; 256 可以把* 換成part_id ,因為part_id裏面有重復的序號,聚合 257 # 後,序號就會變成一個。 258 select max(id),part_id from userinfo5 group by part_id; 259 但是如果把id 也寫上,但是id裏沒有重復的序號,所 260 #以計算機不知道該選誰,這是就需要 261 MySQL裏的函數了 max(id),會選兩個中比較大的那一個 262 min(id) ,part_id重復的話,會選最小的那個 263 264 select count(id),max(id),part_id from userinfo5 gr 265 #oup by part_id; 266 # count(id) 計數 給part_id計數 267 268 count 269 max 270 min 271 avg 272 sum 都是聚合函數 幫我們把重復的弄成一個出來 273 274 如果對於聚合函數的結果進行二次篩選時,必須使用having 275 select count(id),max(id),part_id from userinfo5 276 # group by part_id having count(id) > 1; 277 select count(id) as count_id,max(id),part_id from 278 # userinfo5 group by part_id having count(id) > 1; 279 可以改名 通過as 280 281 282 連表操作: 283 284 select * from userinfo5,department5 where userinfo5.part_id = 285 department5.id; 286 # (讓兩個表連接起來,但也不能瞎連接,倆個的id是有關聯的,用等號連起來) 287 288 select * from userinfo5 left join department5 on userinfo5.part_id = 289 department5.id; 290 # left join 把兩個表連接起來 # on 就代表告訴兩張表的關系 291 292 select * from userinfo5 left join department5 on userinfo5.part_id = 293 department5.id; 294 # left join 就是左邊的表裏所有的內容全部的顯示,也就是userinfo5裏的全部顯示 295 296 select * from userinfo5 right join department5 on userinfo5.part_id = 297 department5.id; 298 # left join 就是右邊的表裏所有的內容全部的顯示 也就是department5 裏的全部顯示 299 300 select * from userinfo5 inner join department5 on userinfo5.part_id = 301 department5.id; 302 # inner join 就是將出現null時的那一行隱藏 303 304 select score.sid from score 305 left join student on score.student_id = student.sid 306 left join course on score.corse_id = course.cid 307 left join class on score.class_id = class.cid 308 left join teacher on score.teacher_id = teacher.tid 309 310 # class_id 與 teacher_id 在score表裏沒有,但是score表已經與其他兩張 311 都表關聯了, 312 其它兩張表裏有這兩列,所以,也就相當於score表裏也有了這兩列,所以就可 313 以使用score.teacher_id 與 score.class_id。 314 如果列名有重復的,可以在前面加上表名就可以 例如:score.sid 315 316 select count(name) from userinfo5; # 計算name這一列的個數 317 318 # 註:本文是根據老男孩課程內容整理而成的,本文僅供個人筆記使用,如果有侵犯,請聯系我,我立即撤銷。 319 320 """

python 基礎復習之數據庫02