自增補充
這是查看怎么創建的表, \G示旋轉90度顯示表的內容
表的自增的關鍵是** AUTO_INCREMENT=3**,在表中添加數據后,這個會自動改變,通過alert可以改變這個默認值
mysql> show create table t1 \G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
下一次添加的內容的id會從20處添加
alter table t10 AUTO_INCREMENT=20;
自增步長
mysql是的默認步長是基於會話session的,sqlserver是基於表的。
查看全局變量,其中默認是1
mysql> show session variables like 'auto_inc%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
2 rows in set (0.00 sec)
設置步長基於會話步長,只能自該自己的會話
set session auto_increment_increment=2; 設置會話步長
set session auto_increment_offset=10; 設置開始的位置
基於全局的級別的,可以修改全部的會話
show global variables like 'auto_inc%'; 查看全局變量
set global auto_increment_increment=2; 設置會話步長
set global auto_increment_offset=10;
唯一索引
unique
create table t1(
id int ....,
num int,
xx int,
unique 唯一索引名稱 (列名,列名),
constraint ....
)
這了唯一的意思是:
- 約束不能重復(可以為空)
- 主鍵不能重復(不能為空)
作用是加速查找
外鍵的變種
-
單列
-
聯合
關聯一對多
create table userinfo(
id int auto_increment primary key,
username varchar,
usertype int,
)engine=innodb default charset=utf8;
create table admin(
id int auto_increment primary key,
user_id int,
passwprd varchar,
unique index(user_id),
constraint fk_key1 foreign key (user_id) references userinfo(id)
)engine=innodb default charset=utf8;
-- 外鍵關聯多個列
create table t2(
nid int not null auto_increment,
pid int not null,
num int,
primary key(nid,pid)-- 這里的關聯兩個列的主鍵
)engine=innodb default charset=utf8;
create table t3(
id int auto_increment primary key,
name char,
id1 int,
id2 int,
constraint fk_t3_t2 foreign key (id1,id2) references t2(nid,pid)
)engine=innodb default charset=utf8;
多對多
-- 多對多
-- 用戶表
create table userinfo(
id int auto_increment primary key,
username varchar,
gender int,
)engine=innodb default charset=utf8;
-- 主機表
create table computer(
id int auto_increment primary key,
name varchar,
)engine=innodb default charset=utf8;
-- 用戶主機關系表
create table userandcom(
id int auto_increment primary key,
user_id int,
host_id int,
unique index(user_id,host_id),
constraint fk_key2 foreign key (user_id) references userinfo(id),
constraint fk_key3 foreign key (host_id) references computer(id)
)engine=innodb default charset=utf8;
SQL語句數據行操作補充
增
-- 增加單條數據
insert into t1 (name) values('ddd');
增加多條數據
-- insert into t1 (name) values('ddd'),('eee');
-- 從一個表中添加另一個內容
insert into t4(name) select name from t1;
+------+------+
| id | name |
+------+------+
| NULL | aaa |
| NULL | aaa |
| NULL | ccc |
| NULL | ddd |
| NULL | eee |
+------+------+
這里出現null的原因是在創建表的時候沒有添加自增和主鍵
在調試中發現char后面不加長度,默認的長度是1,所以要添加一個長度。這個是根據需求
刪
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='a'
改
update tb12 set name='a' where id>12 and name='xx'
update tb12 set name='a',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';
select name,age,11 from tb12;
select * from tb12 where id != 1
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11)
select * from tb12 where id between 5 and 12;
- 通配符
通配符的意識替換的意思
%能夠替換多個字符
_只能替換一個字符
select * from tb12 where name like "a%"
select * from tb12 where name like "aa_"
- 分頁
select * from tb12 limit 10;
select * from tb12 limit 0,10;
select * from tb12 limit 10,10;
select * from tb12 limit 20,10;
后期的Python應用
# page = input('請輸入要查看的頁碼')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 第一頁1
# select * from tb12 limit 10,10;第二頁2
- 排序
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc; # 這是優先級 先按照age倒序,后按照id排序(ID中有相同的)
取后10條數據:先倒序后去取
select * from tb12 order by id desc limit 10;
mysql> select * from t5 order by name desc,id desc;
+----+------+
| id | name |
+----+------+
| 5 | eee |
| 4 | ddd |
| 3 | ccc |
| 2 | aaa |
| 1 | aaa |
+----+------+
5 rows in set (0.00 sec)
mysql> select * from t5 order by name desc,id asc;
+----+------+
| id | name |
+----+------+
| 5 | eee |
| 4 | ddd |
| 3 | ccc |
| 1 | aaa |
| 2 | aaa |
+----+------+
- 分組
select count(id),max(id),part_id from userinfo5 group by part_id;
- count
- max
- min
- sum
- avg
如果對於聚合函數結果進行二次篩選時?必須使用having ,不能使用where
select count(id),part_id from userinfo5 group by part_id having count(id) > 1;
- 連表操作
連表操作主要是把兩張表顯示在一張表上,主要用過join
select * from userinfo5,department5 -- 這種是笛卡爾積的形式 即所有的乘積
select * from userinfo5,department5 where userinfo5.part_id = department5.id;
左邊全部顯示
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id
右邊全部顯示
select * from userinfo5 right join department5 on userinfo5.part_id = department5.id
select * from department5 left join userinfo5 on userinfo5.part_id = department5.id;這種就是變相的right
如果一張表顯示全部,但是另一張表還有多的內容的時候,就會出現空null
inner join 將出現null時一行隱藏
select * from userinfo5 innder join department5 on userinfo5.part_id = department5.id
mysql> select * from t1 left join t5 on t1.id=t5.id;
+----+-------+------+------+
| id | name | id | name |
+----+-------+------+------+
| 1 | aaa | 1 | aaa |
| 2 | aaa | 2 | aaa |
| 3 | ccc | 3 | ccc |
| 4 | ddd | 4 | ddd |
| 5 | eee | 5 | eee |
| 6 | hahah | NULL | NULL |
+----+-------+------+------+
隱藏空行
mysql> select * from t1 inner join t5 on t1.id=t5.id;
+----+------+----+------+
| id | name | id | name |
+----+------+----+------+
| 1 | aaa | 1 | aaa |
| 2 | aaa | 2 | aaa |
| 3 | ccc | 3 | ccc |
| 4 | ddd | 4 | ddd |
| 5 | eee | 5 | eee |
+----+------+----+------+
5 rows in set (0.00 sec)
數據庫的備份
數據庫導出
-
mysqldump -u用戶名 -p密碼 數據庫名稱 >導出文件路徑 # 結構+數據(導入的時候會自動穿件表並把表的內容插入)
-
mysqldump -u用戶名 -p密碼 -d 數據庫名稱 >導出文件路徑 # 僅僅結構
導入現有數據庫數據: -
mysql -uroot -p密碼 數據庫名稱 < 文件路徑
注意的是導入的時候不能用dump
