自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL的開發(fā)必會的SQL語句

數(shù)據(jù)庫 MySQL
本文我們一起來談一談MySQL的開發(fā)必須要學會的sql語句都有哪些,讓我們一起來了解下吧!

 本文談談MySQL的開發(fā)必會的sql語句

[[322273]]

 

創(chuàng)建數(shù)據(jù)庫

 

  1. create database db1; 

刪除數(shù)據(jù)庫

 

  1. drop database db1; 

創(chuàng)建數(shù)據(jù)表

 

  1. create table tb1用戶表( 
  2.                     id int not null auto_increment primary key
  3.                     name char(10), 
  4.                     department_id int
  5.                     p_id int
  6.                 )engine=innodb default charset=utf8; 

主鍵(primary key)一個表只能有一個主鍵,主鍵可以由一列或者多列組成

外鍵的創(chuàng)建

 

  1. CREATE TABLE t5 ( 
  2.                       nid int(11) NOT NULL AUTO_INCREMENT, 
  3.                       pid int(11) not NULL
  4.                       num int(11), 
  5.                       primary key(nid,pid) --這里就是把兩列設置成了主鍵 
  6.                     ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
  7.  
  8.                     create table t6( 
  9.                         id int auto_increment primary key
  10.                         name char(10), 
  11.                         id1 int
  12.                         id2 int
  13.                         CONSTRAINT fk_t5_t6 foreign key (id1,id2) REFERENCES t1(nid,pid) --這里是設置外鍵 
  14.                     )engine=innodb default charset=utf8; 

數(shù)據(jù)行的操作

數(shù)據(jù)的插入

 

  1. insert into tb1(name,age) values('ax',8); 
  2. insert into tb12(name,age) select name,age from tb11; 

表中的數(shù)據(jù)的刪除

 

  1. delete from t1; 
  2. truncate table t1; 
  3. drop table t1 
  4. delete from tb1 where id > 10 
  5. delete from tb12 where id >=2 or name='alex' 

數(shù)據(jù)的更新

 

  1. update tb1 set name='root' where id > 10 

數(shù)據(jù)的查詢

 

  1. select * from tb; 
  2. select id,name from tb; 

表結構的查看

 

  1. show create table t1; 
  2. desc t1; 

其他

 

  1. select * from tb12 where id != 1 
  2.   select * from tb12 where id in (1,5,12); 
  3.   select * from tb12 where id not in (1,5,12); 
  4.   select * from tb12 where id in (select id from tb11) 
  5.   select * from tb12 where id between 5 and 12; 

通配符

 

  1. select * from tb12 where name like "a%" 
  2. select * from tb12 where name like "a_" 

分頁

 

  1. select * from tb12 limit 10;                     
  2. select * from tb12 limit 0,10; 
  3. select * from tb12 limit 10,10; 
  4. select * from tb12 limit 20,10;                  
  5. select * from tb12 limit 10 offset 20; 
  6.  
  7. # page = input('請輸入要查看的頁碼'
  8. # page = int(page) 
  9. # (page-1) * 10 
  10. select * from tb12 limit 0,10; 1  
  11. select * from tb12 limit 10,10;2 

排序

 

  1. select * from tb12 order by id desc; 大到小 
  2. select * from tb12 order by id asc;  小到大 
  3. select * from tb12 order by age desc,id desc
  4.  
  5. 取后10條數(shù)據(jù) 
  6. select * from tb12 order by id desc limit 10; 

分組

 

  1. select count(id),max(id),part_id from userinfo5 group by part_id; 
  2. 聚合函數(shù)有下面幾個:                   
  3.                     count 
  4.                     max 
  5.                     min 
  6.                     sum 
  7.                     avg 
  8.  
  9. **** 如果對于聚合函數(shù)結果進行二次篩選時?必須使用having **** 
  10. select count(id),part_id from userinfo5 group by part_id having count(id) > 1; 
  11.  
  12. select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1; 

自增值設置

表自增值的設置

 

  1. alter table t1 auto_increment=20; 
  2. -- 這個就表示從開始20開始算,用上面的show create table t1\G 就可以看到當前的值是多少。 

基于會話級別

 

  1. -- 查看當前的會話值 
  2. show session variables like 'auto_incre%' 
  3. -- 設置會話步長 
  4. set session auto_increment_increment=2; 
  5. -- 設置起始值 
  6. set session auto_increment_offset=10; 

基于全局設置

 

  1. -- 查看全局的設置值 
  2. show global variables like 'auto_inc%'
  3. -- 設置全局步長值 
  4. set global auto_increment_increment=3; 
  5. -- 設置起始值 
  6. set global auto_increment_offset=11; 

sql server 是在創(chuàng)建表的時候就可以自己設置,靈活度很高REATE TABLE t5 (nid int(11) NOT NULL AUTO_INCREMENT,pid int(11) NOT NULL,num int(11) DEFAULT NULL,PRIMARY KEY (nid,pid)) ENGINE=InnoDB AUTO_INCREMENT=4, 步長=2 DEFAULT CHARSET=utf8

CREATE TABLE `t6` (

nid int(11) NOT NULL AUTO_INCREMENT,pid int(11) NOT NULL,num int(11) DEFAULT NULL,PRIMARY KEY (nid,pid)) ENGINE=InnoDB AUTO_INCREMENT=4, 步長=20 DEFAULT CHARSET=utf8

唯一索引

 

  1. create table t1( 
  2.     id int
  3.     num int
  4.     xx int
  5.     unique qu1 (num ,xx) -- 意思就是這兩列在一行上面數(shù)據(jù)不能相同,例如都是1,1,就不行 
  6. ); 

唯一索引:約束不能重復(可以為空)主鍵索引:約束不能重復(不可以為空)他們的特點都是加速查詢

外鍵一對一

 

  1. create table userinfo1( 
  2.                     id int auto_increment primary key
  3.                     name char(10), 
  4.                     gender char(10), 
  5.                     email varchar(64) 
  6.                 )engine=innodb default charset=utf8; 
  7.  
  8.                 create table admin( 
  9.                     id int not null auto_increment primary key
  10.                     username varchar(64) not null
  11.                     password VARCHAR(64) not null
  12.                     user_id int not null
  13.                     unique uq_u1 (user_id), 
  14.                     CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id) 
  15.                 )engine=innodb default charset=utf8; 

外鍵多對多

 

  1. 示例1: 
  2.                 用戶表 
  3.                 相親表 
  4.  
  5.             示例2: 
  6.                 用戶表 
  7.                 主機表 
  8.                 用戶主機關系表 
  9.             ===》多對多 
  10.  
  11.                 create table userinfo2( 
  12.                     id int auto_increment primary key
  13.                     name char(10), 
  14.                     gender char(10), 
  15.                     email varchar(64) 
  16.                 )engine=innodb default charset=utf8; 
  17.  
  18.                 create table host( 
  19.                     id int auto_increment primary key
  20.                     hostname char(64) 
  21.                 )engine=innodb default charset=utf8; 
  22.  
  23.                 create table user2host( 
  24.                     id int auto_increment primary key
  25.                     userid int not null
  26.                     hostid int not null
  27.                     unique uq_user_host (userid,hostid), 
  28.                     CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id), 
  29.                     CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id) 
  30.                 )engine=innodb default charset=utf8; 

連表操作

 

  1. select * from userinfo5,department5 
  2.  
  3.                     select * from userinfo5,department5 where userinfo5.part_id = department5.id 
  4.  
  5.                     select * from userinfo5 left join department5 on userinfo5.part_id = department5.id 
  6.                     select * from department5 left join userinfo5 on userinfo5.part_id = department5.id 
  7.                     # userinfo5左邊全部顯示 
  8.  
  9.                     # select * from userinfo5 right join department5 on userinfo5.part_id = department5.id 
  10.                     # department5右邊全部顯示 
  11.  
  12.                     select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id 
  13.                     將出現(xiàn)null時一行隱藏 
  14.  
  15. select * from  
  16.                         department5  
  17. left join userinfo5 on userinfo5.part_id = department5.id 
  18. left join userinfo6 on userinfo5.part_id = department5.id 
  19.  
  20. select  
  21.                         score.sid, 
  22.                         student.sid  
  23. from  
  24.                     score 
  25.  
  26. left join student on score.student_id = student.sid 
  27.  
  28. left join course on score.course_id = course.cid 
  29.  
  30. left join class on student.class_id = class.cid 
  31.  
  32. left join teacher on course.teacher_id=teacher.ti            
  33.  
  34. select count(id) from userinfo5; 

 

責任編輯:華軒 來源: 今日頭條
相關推薦

2013-10-29 16:27:23

MySQLSQL語句

2011-08-15 14:52:41

MySQL

2010-10-08 16:26:49

mysql查找

2023-03-30 09:10:06

SQLSELECTFROM

2010-09-03 14:39:15

SQLSELECT語句

2010-09-03 15:47:40

SQL語句鎖定

2010-05-11 10:12:50

mysql數(shù)據(jù)庫sql

2018-11-05 14:54:18

MySQLSQL語句數(shù)據(jù)庫

2017-05-16 11:20:51

SQL語句解析

2010-07-08 13:26:02

SQL Server

2010-09-17 16:03:17

鎖定SQL表

2010-09-08 16:26:26

SQL循環(huán)語句

2010-11-11 11:37:22

SQL SELECT語

2010-09-03 14:47:50

SQLSELECT語句

2010-11-25 11:07:28

MySQL慢查詢

2022-03-22 07:38:00

SQL語句MySQL

2010-09-03 11:25:58

SQL刪除

2022-04-10 23:42:33

MySQLSQL數(shù)據(jù)庫

2010-02-25 10:04:33

MySQL執(zhí)行SQL語

2010-05-14 14:12:58

MySQL數(shù)據(jù)庫優(yōu)化
點贊
收藏

51CTO技術棧公眾號