MySQL基礎操/下

來源:互聯網
上載者:User

標籤:foreign   log   顯示   其他   ffffff   inno   offset   遞增   優先順序   

MySQL基礎操

 

一、自增補充
desc (表名)t1;  查看錶格資訊內容 表的資訊show create table t1(表名):也是查看資訊,還不多是橫向查看show  create table t1 \G; 豎向查看自增資訊alter table t1 AUTO_INCREMENT=3; 可以修改自增
MySQL:自增步長  基於會話層級:
 show session variables like “auto_inc%;查看全域變數 set session auto_increment_increment=2; 設定繪畫步長  set global auto_increment_offset=10; 表示自增長欄位每次遞增的量,其預設值是1;
基於全域層級:
show global variables like ‘auto_inc%‘;    查看全域變數set global auto_increment_increment=2;     設定會話步長# set global auto_increment_offset=10;
補充主鍵:一張表只有一個主鍵,但主鍵可以有多列組成;  
CREATE 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  //設定步長 及自動增加

 

二、 唯一索引

 唯一索引:索引列的值必須唯一,但允許有空值。如果是複合式索引,則列值的組合必須唯一。

 主鍵索引:不允許有空值。一般是在建表的時候同時建立主鍵索引。

 unique 唯一索引名稱 (列名,列名)//聯合索引,  unique uq_u1 (user_id), //唯一索引 

 create table t1(            id int ....,            num int,            xx int,            unique 唯一索引名稱 (列名,列名) //聯合索引            constraint ....        )

 

三、 外鍵的變種 a :使用者表和部門表(一對多形式)
使用者:                1 alex     1                2 root       1                3 egon       2                4 laoyao   3                            部門:                1 服務                2 保安                3 公關            ===》 一對多
View Code b :使用者表和部落格表(一對一形式) 
使用者表:                1 alex                    2 root                       3 egon                       4 laoyao               部落格表:                                  FK() + 唯一                1   /yuanchenqi/   4                2    /alex3714/    1                3    /asdfasdf/    3                4    /ffffffff/    2                            ===> 一對一
View Code
create table userinfo1(                    id int auto_increment primary key,                    name char(10),                    gender char(10),                    email varchar(64)                )engine=innodb default charset=utf8;                create table admin(                    id int not null auto_increment primary key,                    username varchar(64) not null,                    password VARCHAR(64) not null,                    user_id int not null,                    unique uq_u1 (user_id),   //唯一索引                    CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id)                )engine=innodb default charset=utf8;
View Code  c: 使用者表(百合網) 相親記錄表
 樣本1:                使用者表                相親表                            樣本2:                使用者表                主機表                使用者主機關係表            ===》多對多      create table userinfo2(                    id int auto_increment primary key,                    name char(10),                    gender char(10),                    email varchar(64)                )engine=innodb default charset=utf8;                create table host(                    id int auto_increment primary key,                    hostname char(64)                )engine=innodb default charset=utf8;                create table user2host(                    id int auto_increment primary key,                    userid int not null,                    hostid int not null,                    unique uq_user_host (userid,hostid),  //聯合唯一                    CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id),                    CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id)                )engine=innodb default charset=utf8;
View Code

 

、SQL語句資料行操作補充建立:
create table tb12(                id int auto_increment primary key,                name varchar(32),                age int            )engine=innodb default charset=utf8;
增: 
 insert into tb11(name,age) values(‘alex‘,12);  //單行插入  insert into tb11(name,age) values(‘alex‘,12),(‘root‘,18); //多行插入  insert into tb12(name,age) select name,age from tb11; //將tb11裡面表的內容插入tb12;
 刪:
delete from tb12;delete from tb12 where id !=2 delete from tb12 where id =2 delete from tb12 where id > 2 delete from tb12 where id >=2 delete from tb12 where id >=2 or name=‘alex‘
 改:
update tb12 set name=‘alex‘ where id>12 and name=‘xx‘  update tb12 set name=‘alex‘,age=19 where id>12 and name=‘xx‘
查:
select * from tb12;select id,name from tb12;select id,name from tb12 where id > 10 or name =‘xxx‘;select id,name as cname from tb12 where id > 10 or name =‘xxx‘;  //as可以修改序列名;select name,age,11 from tb12;  //查看另一張表 
其他:
a:條件select * from tb12 where id != 1select * from tb12 where id in (1,5,12); //查看錶id是1,5,12的select * from tb12 where id not in (1,5,12);//查看錶id不是1,5,12的select * from tb12 where id in (select id from tb11) select * from tb12 where id between 5 and 12; //閉開間取範圍的b:萬用字元:select * from tb12 where name like "a%"  // a開頭的所有(%多個字串)select * from tb12 where name like "a_"  //a開頭的(— 一個字元)c:限制(分頁):select * from tb12 limit 10;     // 前10行select * from tb12 limit 0,10;   //從1行開始的10行select * from tb12 limit 10,10;  //從10行開始的10行select * from tb12 limit 10 offset 20;  //從第10行開始的20行d:排序select * from tb12 order by id desc; 大到小select * from tb12 order by id asc;  小到大select * from tb12 order by age desc,id desc;  優先順序先第一個,如果資料相同在從第二個開始從大小排序取後10條資料 (先排序在取資料)select * from tb12 order by id desc limit 10;e:分組:****select count(id), part_id from userinfo5 group by part_id;countmaxminsumavg******如果對於彙總函式結果進行第二次篩選時,必須使用having*****select count(id),part_id from userinfo5 group by part_id having count(id) > 1;select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1;特別的:group by 必須在where之後,order by之前f:連表:********select * from userinfo5 left join department5 on userinfo5.part_id = department5.idselect * from department5 left join userinfo5 on userinfo5.part_id = department5.id                //usernfo5 左邊全部顯示//department5 左邊全部顯示# select * from userinfo5 right join department5 on userinfo5.part_id = department5.id# department5右邊全部顯示         userinfo5表所有顯示,如果department5中無對應關係,則值為null            select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id 將出現null時一行隱藏         
其他

 

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.