Mysql数据库常用命令总结
一、刪除表數(shù)據(jù)
1、用truncate,它會重新計算自增,重新從1開始,對事務無影響,不能恢復。 一般上線前使用,清空表格。
truncate table table_name2、用delete,對事務會影響,使用后刪除的數(shù)據(jù)能恢復。自增不是從1開始。
delete from me二、數(shù)據(jù)庫表去重復手段
1、用distinct
select distinct email from student;2、用group by分組讀取
select email from student group by email三、查詢語句
1、查詢關(guān)鍵字順序
where group by having order by limit2、范圍查詢
select * from subject where SubjectNo is not null and SubjectNo between 6 and 8;3、模糊查詢
(1)使用like,%匹配任意個,_匹配一個字符。
select * from subject where SubjectName like '%sfs%'4、多表連接方式
inner join #條件滿足的顯示 left join #以左為基準,右邊沒有的為空 right join #與左連接相反 full join #左右連接,左邊沒有顯示空,右邊沒有顯示空,顯示所有數(shù)據(jù)5、子查詢
子查詢的功能也可以通過連接查詢完成,但是子查詢使得 MySQL 代碼更容易閱讀和編寫。
把一個查詢語句當做一個或者一系列數(shù)據(jù)在另一個語句的使用。
select * from result where StudentResult in(select SubjectNo+50 from result);6、不等于查詢
select * from result where StudentNo <> 1012;7、正則查詢
select * from result where StudentNo REGEXP '16$';8、查詢中having與where區(qū)別
(1)having 是對分組后的數(shù)據(jù)進行條件查詢;對組篩選;
(2)where 是直接排序,在分組之前;
9、查詢數(shù)據(jù)表類型
show table status like 'test';10、查看mysql的元數(shù)據(jù)
select version() #看版本 select database() #看數(shù)據(jù)庫名 select status #看服務器狀態(tài) select variables #看服務器配置變量11、查看表結(jié)構(gòu)
show create table aa(表名));12、查看表格詳情
desc result四、對一張表的設(shè)計進行操作
1、修改表字段類型
alter table student modify StudentNo varchar(33) primary key;2、增加一個字段
alter table student add column pwd varchar(50);3、增加一個字段到某個字段之后
alter table test add m int after c;4、修改某個字段名
alter table student change pwd Pwd varchar(20);5、刪除某個字段
alter table student drop column aaa;6、對某個字段更改且設(shè)置默認值
alter table test modify iii bigint not null default 100;7、對表的自增字段設(shè)置初始值
alter table cc auto_increment = 100;8、修改表名
alter table test rename to Test1;9、創(chuàng)建表設(shè)置id自增
id INT UNSIGNED NOT NULL AUTO_INCREMENT,五、事務
1、改變自動提交
set autocommit=0; #禁止自動提交2、事務處理方法
保證事務的原子性,一致性,隔離性,持久性。
(1)begin:開始一個事務
(2)rollback:事務回滾
(3)commit:事務提交
六、索引
1、對表增加index索引
index 可以換primary key 主鍵,unique 唯一索引,full text 全文索引.例如:但是主鍵索引必須是唯一性的。
(1)第一種
alter table result add index indexname(StudentNo);(2)第二種
create index idaaa on aa(id);
2、增加主鍵索引
(1)有索引名
alter table result add primary key indexname(StudentNo);(2)無索引名
alter table cc add primary key(id);3、創(chuàng)建表時添加索引
create table aa(-> id int null,-> username varchar(15) not null,-> index idfirst(id));4、刪除索引
alter命令刪除索引,刪除主鍵時只需指定PRIMARY KEY,但在刪除索引時,你必須知道索引名。
(1)第一種
drop index SubjectNo(索引名字)) on result(表名);(2)第二種
alter table cc drop index indexa(索引名);5、顯示某表索引
show index from result;6、全文索引
全文索引只在varchar、char、text上添加,表的類型必須是myisam類型的。
(1)、全文索引表創(chuàng)建
create table aa{fulltext() }engine=myisam(2)、與innodb類型的表的區(qū)別
myisam類型支持全文索引,不支持事務。單表存貯的數(shù)據(jù)結(jié)構(gòu)不同,允許單表最多存4G(單表對應一個文件)
innodb類型不能用全文索引,支持事務,允許最大存儲4t。
七、刪除表中重復數(shù)據(jù)
1、通過創(chuàng)建新表
(1)創(chuàng)建一個tmp表
create table tmp select StudentNo,SubjectNo,ExamDate,StudentResult from result group by StudentNo;(2)刪除原來的表
drop table result;(3)把tmp表名改為原來名字
alter table tmp rename to result;2、復制表
輸入下面命令,然后把展示的建表語句復制下來修改一下創(chuàng)建就好了。
show create table student;八、數(shù)據(jù)庫轉(zhuǎn)儲
1、導入sql數(shù)據(jù)庫
(1)在mysql命令行輸入:
create database abc; use abc; set names utf8; source D:/test.sql;(2)在命令行輸入,這個aaa數(shù)據(jù)庫必須是已經(jīng)創(chuàng)建的。
mysql -u root -p aaa<D:/aaamyresult.sql2、導出數(shù)據(jù)庫到本地
注意:在命令行輸入的語句不要在后面加分號,會報錯
mysqldump -uroot -p abc>D:/aaamyresult.sql3、對txt文件操作
(1)將txt文件中的數(shù)據(jù)導入到數(shù)據(jù)庫表中
load data local infile 'D:/aa.txt' into table student;(2)把一張表導出到txt文件中
select * from aa into outfile D:/aaa.txt';九、觸發(fā)器
1、語法:
create trigger trigger1 trigger_time(after or before)) trigger_even(事件) on tablename for each row 操作語句2、eg:在插入grade表之前插入一條result數(shù)據(jù)
create trigger tri_grade_i before insert on grade for each row insert into result values(9999,1,'1000-11-11',888);3、eg:在插入grade表之前拿到插入grade的gradeid插入到result表中
create trigger tri_grade_i before insert on grade for each rowinsert into result values(new.GradeID,1,'1000-11-11',888);4、刪除觸發(fā)器:
drop trigger tri_grade_i;十、創(chuàng)建數(shù)據(jù)庫
1、在命令行創(chuàng)建數(shù)據(jù)庫
mysqladmin -u root -p create mysql2、在mysql>下創(chuàng)建
create database mysql3、本地登錄mysql,然后輸入登陸密碼
mysql -uroot -proot;十一、總結(jié)
以上是我對數(shù)據(jù)庫常用命令的總結(jié),堅持原創(chuàng),更多精彩歡迎關(guān)注:
總結(jié)
以上是生活随笔為你收集整理的Mysql数据库常用命令总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++中的explicit关键字用法
- 下一篇: 今天把房子定下来了