1. 程式人生 > >mysql基本操作命令

mysql基本操作命令

聚合 姓名 之間 primary multi database 不執行 更改 平均值

數據庫增刪改查

//創建數據庫
create database school;
//創建表
create table info (id int not null primary key auto_increment,name char(10) not

null,score decimal(5,2),hobby int(2));
#primary key 主鍵 auto_increment 自增列
//查看表結構
desc info;
//多表查詢(關聯表)
select * from info inner join hob where info.hobby=hob.id;

select info.name,score,hob.hobname from info inner join hob where

info.hobby=hob.id;
//別名查詢
select i.name,score,h.hobby from info as i inner join hob as h where i.hobby=h.id;

//聚合函數
統計count(): 可以改為1
select count(
) from info;
平均值avg ()
select avg(score) from info;
//查看數據庫
show databases;
//進入數據庫
use myschool;
//查看myschool中的表
show tables;
//查看info中的數據
select from info;
//在info中插入數據
insert into info (id,name,score) values (‘tianqi‘,55); #前後匹配,如果為空:null

//篩選信息
mysql> select
from 表名 where id=2[and name=?] [or name=?]
//更新信息
update info set score=75 where id=6;
//刪除信息
delete from info where name=‘test‘; #整行刪除
//刪除表、數據庫
drop table info; drop database school;
//排序
select from info where 1=1 order by score ; asc--升序,可不寫 #默認升序
select
from info where 1=1 order by score desc ; desc--降序

數據庫索引、事務、視圖

索引:快速查詢數據  條件:數據數目大於兩千條 

相當於一本書前的目錄頁

create index 索引名稱 on tablename 列;
id name score address hobby

create index id_index on info(id); 創建普通索引

show index from info \G; 查看索引折行顯示

drop index id_index on info; 刪除索引

create unique index id_index on info(id); 創建唯一索引

alter table info add primary key(id); 主鍵索引

alter table info add column age int(3); 添加列

alter table info drop column age; 刪除列

create table infos (descript TEXT,FULLTEXT(descript));全文索引,descript列名描述

create index multi_index on info(name,address); 多頁索引,講兩個條件聯合起來進行

查詢

事務:一組操作共同執行或者都不執行,結果保持一致

舉個栗子:銀行轉賬
條件:轉賬條件余額大於0
姓名 余額
張三 100
李四 200

張三轉賬100 to 李四

begin 開始

updata bank set money=money-100 where name=‘zhangsan‘

updata bank set money=money+100 where name=‘lisi‘

commit 提交

savepoint s1; 設定回滾點

rollback to savepoint s1; 回到s1回滾點

set autocommit=0 禁止自動提交
set autocommit=1 開啟自動提交
rollback 回滾

原子性 不可分割

一致性 前後結果保持一致

隔離性 事務之間隔離,互不影響

持久性 一旦執行成功,不可更改

視圖      數據庫中的虛擬表

作用:一張表或者多張表中的數據給不同的權限用戶提供訪問

create view 視圖名稱 AS

select 語句

select * from info where score > 80; 查看大於80分的人

create view score_view as select * from info where score >80; 形成視圖進行查看

select * from score_view; 查看視圖

mysql基本操作命令