標籤:ref count 安裝mysql mysql資料庫 change 表名 truncate 基本操作 off
1.使用mysql資料庫的必備要素
a. 安裝MySQL服務端
b. 安裝MySQL用戶端
c. 【用戶端】串連【服務端】
d. 【用戶端】發送命令給【服務端MySQL】服務的接受命令並執行相應操作(增刪改查等)
2.串連資料庫命令
mysql -u root -p
3.常用命令
show tables; # 查看資料庫全部表
select * from 表名; # 查看錶所有內容
create table 表名( 列名 類型 是否可以為空白, 列名 類型 是否可以為空白)ENGINE=InnoDB DEFAULT CHARSET=utf8 #建立表
for example
CREATE TABLE `tab1` ( `nid` int(11) NOT NULL auto_increment, # not null表示不可為空,auto_increment表示自增 `name` varchar(255) DEFAULT zhangyanlin, # default 表示預設值 `email` varchar(255), PRIMARY KEY (`nid`) # 把nid列設定成主鍵) ENGINE=InnoDB DEFAULT CHARSET=utf8;
drop table 表名 #刪除表
delete from 表名truncate table 表名 #刪除表內容
添加列: alter table 表名 add 列名 類型刪除列: alter table 表名 drop column 列名修改列: alter table 表名 modify column 列名 類型; -- 類型 alter table 表名 change 原列名 新列名 類型; -- 列名,類型 添加主鍵: alter table 表名 add primary key(列名);刪除主鍵: alter table 表名 drop primary key; alter table 表名 modify 列名 int, drop primary key; 添加外鍵: alter table 從表 add constraint 外鍵名稱(形如:FK_從表_主表) foreign key 從表(外鍵欄位) references 主表(主鍵欄位);刪除外鍵: alter table 表名 drop foreign key 外鍵名稱 修改預設值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;刪除預設值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
4.增刪改查操作
增
insert into 表 (列名,列名...) values (值,值,...)insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)insert into 表 (列名,列名...) select (列名,列名...) from 表
例:
insert into tab1(name,email) values(‘zhangyanlin‘,‘[email protected]‘)
刪
delete from 表 # 刪除表裡全部資料delete from 表 where id=1 and name=‘zhangyanlin‘ # 刪除ID =1 和name=‘zhangyanlin‘ 那一行資料
改
update 表 set name = ‘zhangyanlin‘ where id>1
查
select * from 表select * from 表 where id > 1select nid,name,gender as gg from 表 where id > 1
select * from 表 where id > 1 and name != ‘aylin‘ and num = 12; select * from 表 where id between 5 and 16; select * from 表 where id in (11,22,33) select * from 表 where id not in (11,22,33) select * from 表 where id in (select nid from 表)
select * from 表 where name like ‘zhang%‘ # zhang開頭的所有(多個字串) select * from 表 where name like ‘zhang_‘ # zhang開頭的所有(一個字元)
select * from 表 limit 5; - 前5行 select * from 表 limit 4,5; - 從第4行開始的5行 select * from 表 limit 5 offset 4 - 從第4行開始的5行
select * from 表 order by 列 asc - 根據 “列” 從小到大排列 select * from 表 order by 列 desc - 根據 “列” 從大到小排列 select * from 表 order by 列1 desc,列2 asc - 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序
select num from 表 group by num
select num,nid from 表 group by num,nid
select num,nid from 表 where nid > 10 group by num,nid order nid desc
select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
select num from 表 group by num having max(id) > 10
特別的:group by 必須在where之後,order by之前
Mysql資料庫基本操作 (一)