## MySQL沒拿過來
## 創建/刪除/查看數據庫
創建一個數據庫
mysql -uroot? –p密碼 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#進入數據庫控制臺
MySQL [(none)]> create database 數據庫名稱;? ? ?#特別注意有分號
MySQL [(none)]>? show? databases;?? ? ? ? ? ? ? ? ? ? ?#查看數據庫
MySQL [(none)]> ?exit;? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#退出數據庫控制臺,特別注意有分號
刪除一個數據庫
MySQL [(none)]> drop database 數據庫名稱;? ? ?#刪除數據庫
MySQL [(none)]> exit;?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #退出數據庫控制臺,特別注意有分號
查看數據庫: show databases; ? ? ? ? ??#如下圖中3個數據庫是默認數據庫,不可刪除

選擇數據庫: use dbname;
## 表的操作
顯示庫下面的表
show tables;
查看表的結構:
desc tableName;
查看表的創建過程:
show create table tableName;
創建表:
create table tbName ( 列名稱1 列類型 [列參數] [not null default ], ….列2… …. 列名稱N 列類型 [列參數] [not null default ] )engine myisam/innodb charset utf8/gbk
例子:
create table user ( id int auto_increment, name varchar(20) not null default ”, age tinyint unsigned not null default 0, index id (id) )engine=innodb charset=utf8;
注:innodb是表引擎,也可以是myisam或其他,但最常用的是myisam和innodb,
charset 常用的有utf8,gbk;
修改表
1.修改表之增加列:
alter table tbName add 列名稱1 列類型 [列參數] [not null default ] #(add之后的舊列名之后的語法和創建表時的列聲明一樣)
2.修改表之修改列
alter table tbName change 舊列名 新列名 列類型 [列參數] [not null default ]
(注:舊列名之后的語法和創建表時的列聲明一樣)
3.修改表之減少列:
alter table tbName drop 列名稱;
4.修改表之增加主鍵
alter table tbName add primary key(主鍵所在列名);
例:alter table goods add primary key(id)
該例是把主鍵建立在id列上
5.修改表之刪除主鍵
alter table tbName drop primary key;
6.修改表之增加索引
alter table tbName add [unique|fulltext] index 索引名(列名);
7.修改表之刪除索引
alter table tbName drop index 索引名;
8.清空表的數據
truncate tableName;
## 表數據操作
1.插入數據
insert into 表名(col1,col2,……) values(val1,val2……); -- 插入指定列
insert into 表名 values (,,,,); -- 插入所有列
insert into 表名 values -- 一次插入多行
(val1,val2……),
(val1,val2……),
(val1,val2……);
2.修改數據
update tablename
set
col1=newval1,
col2=newval2,
...
...
colN=newvalN
where 條件;
3.刪除數據 delete from tablenaeme where 條件;
4.select查詢
(1) 條件查詢 where a. 條件表達式的意義,表達式為真,則該行取出
b. 比較運算符 = ,!=, =
c. like , not like ('%'匹配任意多個字符,'_'匹配任意單個字符)
in , not in , between and
d. is null , is not null
(2) 分組 group by
一般要配合5個聚合函數使用:max,min,sum,avg,count
(3) 篩選 having
(4) 排序 order by
(5) 限制 limit
5.連接查詢
5.1 左連接
.. left join .. on
table A left join table B on tableA.col1 = tableB.col2 ;
例句:
select 列名 from table A left join table B on tableA.col1 = tableB.col2
5.2 右鏈接: right join
5.3 內連接: inner join 左右連接都是以在左邊的表的數據為準,沿著左表查右表. 內連接是以兩張表都有的共同部分數據為準,也就是左右連接的數據之交集. 7 子查詢 where 型子查詢:內層sql的返回值在where后作為條件表達式的一部分 例句: select * from tableA where colA = (select colB from tableB where ...); from 型子查詢:內層sql查詢結果,作為一張表,供外層的sql語句再次查詢 例句:select * from (select * from ...) as tableName where ....
- 關于本書
- 獲取一鍵安裝包
- MySQL/MariaDB
- 鏡像組成一覽(表)
- MySQL鏡像使用
- MySQL基礎知識
- 關于 test 數據庫
- phpMyAdmin使用
- phpMyAdmin限制特定IP訪問
- MySQL-Front使用
- 如何開啟MySQL遠程連接?
- 如何啟停MySQL服務?
- 如何修改MySQL密碼?
- 如何重置MySQL密碼?
- 如何實現MySQL遷移?
- 附:常用賬號與密碼說明
- 附:MySQL命令
- 故障處理
- SQLServer
- 鏡像組成一覽(表)
- SQLServer Studio管理器
- 如何開啟SQLServer遠程連接?
- 如何在本地管理SQLServer?
- 如何啟停SQLServer服務?
- 如何重置SQLServer密碼?
- 如何實現SQLServer遷移?
- 如何實現SQLServer Express自動備份?
- 如何使用Reporting Services功能?
- 常見問題
- 附:常用賬號與密碼說明
- Oracle
- 表:鏡像組成一覽
- 附:常用賬號與密碼說明
- Oracle基礎知識
- 如何可視化管理Oracle Database?
- 如何通過SSH管理Oracle Database?
- 如何開啟Oracle遠程連接?
- 如何啟停Oracle服務?
- 如何修改Oracle密碼?
- 如何重置Oracle密碼?
- 如何實現Oracle遷移?
- 附:Oracle命令
- PostgreSQL
- 表:鏡像組成一覽
- 附:常用賬號與密碼說明
- PostgreSQL基礎知識
- 如何通過SSH管理PostgreSQL?
- 如何可視化管理PostgreSQL?
- 如何開啟PostgreSQL遠程連接?
- 如何啟停PostgreSQL服務?
- 如何修改PostgreSQL密碼?
- 如何重置PostgreSQL密碼?
- 如何實現PostgreSQL遷移?
- 附:PostgreSQL命令
- 云服務器操作(參考)
- Linux:登錄與文件管理(SFTP)
- Windows:遠程桌面與文件管理
- 安全組設置
- 快照備份
- 附:常見鏈接
- 后記