一、DDL概念
DDL(Data Definition Language)語言:資料定義語言,用來定義資料庫物件,如資料庫、資料表和資料欄位,主要是進行定義/改變表的結構、資料型別、表之間的鏈接等操作,常用的陳述句關鍵字有 CREATE、DROP、ALTER 等,
二、資料庫操作
2.1、linux環境連接資料庫
語法:mysql -u用戶名 -p 回車后再輸入資料庫登錄密碼

2. 2、查看所有資料庫
語法:show databases;

2.3、創建資料庫
語法:create database [if not exists] 資料庫名 [default charset 字符集] [collate 排序規則];
方括號中的內容可填可不填

2.4、使用某個資料庫
語法:use 資料庫名;

2.5、查看當前資料庫
語法:select database();

2.6、洗掉資料庫
語法:drop database [if exists] 資料庫名;

2.7、查看資料庫編碼
語法:show variables like 'character%';

其中:
character_set_client 為客戶端編碼方式;
character_set_connection 為建立連接使用的編碼;
character_set_database 為資料庫的編碼;
character_set_results 為結果集的編碼;
character_set_server 為資料庫服務器的編碼;
三、資料庫表操作
3.1、創建表
語法:
create table 表名(
欄位1 欄位1型別 [comment 欄位1注釋],
欄位2 欄位2型別 [comment 欄位2注釋],
欄位3 欄位3型別 [comment 欄位3注釋],
......
欄位n 欄位n型別 [comment 欄位n注釋]
) [comment 表注釋];
注意:方括號是可選引數,在最后一個欄位中沒有逗號

3.2、查看當前資料庫下所有表
語法:show tables;

3.3、查看表結構
語法:desc 表名;

3.4、查看指定表的建表陳述句
語法:show create table 表名;

3.5、添加表欄位
語法:alter table 表名 add 欄位名 型別(長度) [comment 欄位注釋] [約束];

3.6、修改表欄位型別
語法:alter table 表名 modify 欄位名 欄位新資料型別(長度);

3.7、修改欄位名、欄位型別
語法:alter table 表名 change 舊欄位名 新欄位名 欄位型別(長度) [comment 注釋] [約束];
3.8、修改表名
語法:alter tabe 表名 rename to 新表名;

3.9、洗掉資料表欄位
語法:alter table 表名 drop 欄位名;

3.10、洗掉資料表
語法:drop table [if exists] 表名;

學習沒有一蹴而就,放下急躁,一步一步扎實前進
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/549101.html
標籤:其他
