Mysql表結構定義及相關文法

來源:互聯網
上載者:User

標籤:rename   通過   creat   drop   建表   mysql文法   協助   databases   index   

mysql文法及相關命令
1.每個sql命令都需要使用分號來完成
2.可以將一個命令寫成多行
3.可以通過\c來取消本行命令
4.可以通過\g、exit、ctrl+c或者quit來退出當前用戶端
5.可以通過使用help命令來查詢快速鍵
6.將查詢後的資料豎起來\G
7.顯示當前伺服器狀態\s
8.更改執行符\d
9.使用?查詢命令協助,如 ?create


資料庫相關操作:
create database [if not exists] databasename 建立名為databasename的資料庫

show databases 查看資料庫

drop database databasename 刪除databasename資料庫

select database() 查看當前資料庫

use databasename 使用databasename資料庫,建立表之前必須選定資料庫


資料表相關操作:
1.建立表
create table table_name(
欄位名稱 欄位類型 [欄位屬性] [索引],
欄位名稱 欄位類型 [欄位屬性] [索引]
)表引擎 字元集

2.查看錶結構
desc table_name

3.查看建表語句
show create table table_name

4.刪除表(表和資料都刪掉了)
drop table table_name

5.清空表(表還在,只是沒有資料,相當於刪除表和資料再重建立一個結構一樣的表)
truncate table_name


修改表結構:
添加表欄位
alter table table_name add 欄位名稱 欄位類型 欄位屬性

刪除表欄位
alter table table_name drop 欄位名稱

修改表欄位
alter table table_name change 舊欄位名稱 新欄位名稱 欄位類型 欄位屬性

alter table table_name modify 舊欄位名稱 欄位類型 欄位屬性


修改表名稱
alter table oldtablename rename as newtablename
將名為oldtablename的表改為名為newtablename

表索引
主鍵索引:primary key 一張表中只能存在一個主鍵,可以沒有,且主索引值不可為空
唯一索引:unique 這一列的值不能重複,一張表中可以有多個唯一索引
常規索引:index
全文索引:fulltext

查看當前表索引
show indexes from table_name

添加索引
alter table table_name add index/unique/primary key(欄位名稱)

表索引相關操作
刪除主鍵索引
先去掉自動成長屬性,再刪掉主鍵索引
a.使用alter修改表欄位將自動成長屬性去掉
b.alter table table_name drop primary key

刪除唯一索引和常規索引都使用index
alter table table_name drop index indexname
如果沒有指定索引名稱(indexname)可使用欄位名稱


建立新使用者並授權
grant privileges on databasename.tablename to [email protected] identified by password
如:grant select,insert,update,delete on *.* to ‘zhangsan‘@‘%‘ identified by ‘123‘
授權使用者zhangsan,讓他可以從任何主機通過密碼‘123‘登入,對資料庫databasename中的tablename資料表
有增刪改查許可權

Mysql表結構定義及相關文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.