所有操作命令:
service mysql start
mysql -uroot -pok
show databases; 檢視所有資料庫(都是分號結尾)
create database lol; 建立資料庫(資料庫不能直接改名 刪了重來)
drop database lol; 刪除資料庫
use myschool; 切換到某個資料庫
key不能作為列名 關鍵字
show tables; 檢視所有表: 只顯示表 具體資訊不顯示
describe/desc grade; 檢視表結構
show create table grade; 檢視建表語句
drop table if exists grade; 刪除表
create table if not exists grade( 建立表:取中文名字要加特殊符號tab上面那個符號引漢字
gradeid in(4)primary key,
gradename varchar(10)
);修改表名:alter table 舊表名 rename(to) (as) 新錶名;alter是修改的意思
新增字段:alter table 表名 add欄位名
資料型別; 加陣列也要引起來 first第一列 after加在某個後面 不寫在最後 不加符號 要是加的話注意欄位名符號!!!在最後加first 或者after那個欄位後;
修改字段:alter table 表名 change 原欄位 新字段 資料型別;(不加符號 加的話左上角那個)
修改字段:修改列不改名字 alter table modify 字段 只有altertable這裡用那個特殊符號
刪除字段:alter table 表名 drop 欄位名; 注意:刪除時一次只能刪乙個!!!
檢視所有修改表的結構:help alter table ;
dml:資料操作語言
insert 新增資料語句 insert into 表名 欄位1,欄位2 values (值1),(值2);(不寫字段要全寫一一對應)
update 更新資料語句 update 表名 set 列=值1 where(沒有from)
delete 刪除資料語句 delete from 表名 where
insert into [欄位1,欄位2]values(『值1』,…)對應即可 欄位可省略 value 不加s也行
新增主鍵:constraint 約束 references 參考
1:表存在的時候新增主鍵: alter table tablename add primary key(id) 新增主鍵
2:表存在的時候刪除主鍵:alter table tablename drop primary key; 刪除主鍵
3:建表時新增主鍵:create table b(b_name varchar(10),b_age int(4) primary key, b_class varchar(10));
新增外來鍵:
1:表已經存在的情況下加外來鍵:
alter table 表1 add constraint 外鍵名 foreign key (表1的字段) references 表2(表2的字段)
(表1欄位受表2欄位的約束 此時表2欄位必須為主鍵)
因為要唯一
解釋:alter table 從表 add constraint 外來鍵(形如:fk_從表_主表) foreign key 從表(外來鍵字段) references 主表(主鍵字段);
2:建表時新增外來鍵:
create table b (age int (4),name varchar(10),foreign key(name) references a(classes));前提是a中欄位classes是主鍵。
ctrl+c 退出==exit
dql命令:
select * from 表; 查詢表中所有資訊
select 列1,列2 from 表; 查詢指定列的值
show create table 表; 檢視約束名 然後 : alter table 表名 drop foreign key 約束名;
distinct 不能放在某乙個字段前面 去重就全部去重
select[distinct]
from table [as 別名]
round he rand()查資料
null is not null 用這個來判斷 不能用=null 有null值和空是兩回事 和null計算都為null
select * from result where score between 80 and 90; 用between and 兩邊都包含80和90
round 四捨五入
rand()產生0~1不可重複的隨機數
rand(n) 產生可以重複的隨機數
like 模糊匹配 name like '李%'2個或者三個或者多個 '李_'兩個字 '李__'三個字
where 字段 in(1,2,3) 可以多不能少 少了empty set
in子查詢where 字段 in (select round(rand()2+1000));
shift+ entry 在裡面換行
笛卡爾集 就是兩個表的組成所有可能都列出來 a100 b3 則300條
table1 as t1;
join table2 on table1.id =t2.id
join table3 t3 on t1.id=t3.id
1:當怎麼樣時操作:case 字段 when 值 then 操作 else 操作 end 判斷null 要is null (可以沒有else)
2: case 字段
when 值
then 操作
when 值
then操作
else
endunion: 聯合並去重
union all: 聯合不去重
表的賦值 和結構複製 testd 和tmp——testd
連線:1內連線:兩個表符合關聯條件的資料才進行展示 取交集
select a.,b. from a[inner] jorn b on a.id=b.id;)(先判斷))
select a.,b.* from a,b where a.id=b.id;(先產生笛卡爾集)
2外連線:主表的資料全部展示 附表只展示符合關聯條件的
左外連線:以left join 左邊的表為主表,關聯右表的資料
右外連線:以riget join 右邊的表為主表, 關聯左邊的資料
3全連線:
左外連線 union 右外連線 聯合並去重 union all 不去重
4自連線: 自己關聯自己 不用考慮外聯 因為都有
加上auto_increment 自增
truncate table 表面;清空重來
select * from 表 order by 段 (desc 降序); 預設asc
limit 3為0到3 左閉右開 limit3,5 3到5 如果表123456 則 45
limit 3,5=limit 5 offset 3 個數還是3,5 ???
round(253.3)四捨五入
round(5.2354,2)四捨五入保留2位小數5.24
truncate (5.363712,3)保留3位 後面u四捨五入 都截掉5.363
select count(*/lie)from 表 不計null
select count(1) from 表 相當於新建了乙個表叫1 每個資料都為1 就能知道多少個了
select sum(scores)/count(1) from tb_score;
null 相加都等於空 ifnull(a,0)如果a空就等於0;
limit加空格再加數字
ddl:資料定義語言
create drop
dql:資料查詢語言
select
dcl:資料控制語言
grant revoke
查詢表中所有資料列結果:
select * from 表;(效率低)不是form
查詢表中某一項資料:
select 項 from 表; 指定時:select 表.項 from 表;
給項改名:
as select 項 as「嘿嘿」 from 表;(as可以省略)
去重:select distinct 欄位1,欄位2(…)from 表名;(這裡預設是all 返回所有的)
use information_schema 先輸入這個再去找
整體新增乙個字刪除乙個字 左閉右開
清空表資料 包括自增:truncate table subject;
truncate 不能加where條件
預設值:default
備註:comment();表備註在括號後面
無符號:unsigned 宣告該資料列不允許負數
0填充: zerofill 例如:int(3),5 則為005
主鍵通常加1: auto_increment 加一條資料 自動在記錄上加1;
結束mysql 操作命令 MYSQL操作命令
mysql操作命令 mysql 命令 mysql h主機位址 u使用者名稱 p密碼 連線mysql 如果剛安裝好mysql,超級使用者root是沒有密碼的。例 mysql h110.110.110.110 uroot p123456 注 u與root可以不用加空格,其它也一樣 exit 退出mysq...
mysql 操作命令
不在 mysql 命令列 匯出資料 1.將資料庫mydb匯出到e mysql mydb.sql檔案中 c mysqldump h localhost u root p mydb e mysql mydb.sql 然後輸入密碼,等待一會匯出就成功了,可以到目標檔案中檢查是否成功。2.將資料庫mydb中...
MYSQL 操作命令
1.進入mysql安裝目錄 cd mysql mysql seiver 5.6 bin 1.備份乙個資料庫表資料 mysqldump u root p scms e test.sql 如果要備份一張表 mysqldump u root p scms sc sys use e test.sql 備份沒...