Mysql基礎命令學習

今天下午在實驗室學習了Mysql的基礎命令,在這裏概括整理一下,也算是備忘吧。ps:個人第一篇博客。mysql

  • 數據庫鏈接: mysql -u root -p
  • 查看數據庫:show databases
  • 選擇數據庫:use database

一、說明:建立數據庫sql

CREATE DATABASE database-name

二、說明:刪除數據庫數據庫

drop database dbname

三、說明:備份sql server
--- 建立 備份數據的 device學習

USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'

--- 開始 備份code

BACKUP DATABASE pubs TO testBack

四、說明:建立新表server

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根據已有的表建立新表:排序

A:create table tab_new like tab_old (使用舊錶建立新表)
B:create table tab_new as select col1,col2… from tab_old definition only

五、說明:刪除新表索引

drop table tabname

六、說明:增長一個列博客

Alter table tabname add column col type

注:列增長後將不能刪除。DB2中列加上後數據類型也不能改變,惟一能改變的是增長varchar類型的長度。
七、說明:添加主鍵:
Alter table tabname add primary key(col)
說明:刪除主鍵:it

Alter table tabname drop primary key(col)

八、說明:建立索引:

create [unique] index idxname on tabname(col….)

刪除索引:drop index idxname
注:索引是不可更改的,想更改必須刪除從新建。

九、說明:建立視圖:

create view viewname as select statement

刪除視圖:

drop view viewname

十、說明:幾個簡單的基本的sql語句

選擇:select * from table1 where 範圍
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 範圍
更新:update table1 set field1=value1 where 範圍
查找:select * from table1 where field1 like ’%value1%’ ---like的語法很精妙,查資料!
排序:select * from table1 order by field1,field2 [desc]
總數:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
相關文章
相關標籤/搜索