MySQL的基本使用

2022-09-01 15:36:12 字數 1632 閱讀 6248

sql

ddl:資料定義語言  create drop alter

dml:資料操作語言  select insert update delete

dcl:資料控制語言  grant revoke commit rollback

最基礎的(建庫、建表)

show databases;

use 資料庫名稱;

create database 資料庫名稱;

drop database 資料庫名稱;

show tables;     <<<<<<<< 需要先切換資料庫

create table [庫名.]表名(欄位1 屬性,欄位2 屬性,......)

create table [庫名.]表名 like [庫名.]表名      <<<<<<< 按照某個庫中的表建立新錶

craete table [庫名.]表名 as select 欄位1,欄位2.... from [庫名.]表名;    >>>>>>>>>>>

drop table [庫名.]表名

desc [庫名.]表名

alter命令

作用:修改表的字段屬性

1、新增新字段

格式:alter table 表名  add 字段 屬性

2、刪除字段

格式:alter table 表名  drop 字段

3、修改欄位名稱

格式:alter table 表名  change 字段  新字段 屬性

4、新增索引

設定主鍵:alter table 表名 add primary key(字段)

設定索引:alter table 表名 add index [索引名稱](字段)

設定唯一約束:alter table 表名 add unique [索引名稱](字段)

5、新增外來鍵

格式:alter table 表名1 add froeign key (字段) references 表2(字段)

6、刪除索引

刪除主鍵:alter table 表名 drop primary key

刪除索引:alter table 表名 drop index 索引名稱

刪除外來鍵:alter table 表名 drop foreign key

insert命令

作用:新增記錄

格式:insert into 表名 (欄位1,欄位2,.....) values (值1,值2...),(值1,值2...),......

刪除一條或者多條記錄

delete from 表名 條件

update命令

作用:修改表中的資料

注意:

update 通常和where、order by、limit聯合使用

格式:

update 表名 set 欄位1=值,欄位2=值... [where 判斷條件]

update 表名 set 欄位1=值,欄位2=值... [order by 判斷條件]

ps:歡迎指正,謝謝。

mysql 使用 MySQL 基本使用

資料庫 create database 名字 建立資料庫 show databases 檢視所有資料庫 show create database book g 檢視建立好的資料庫的定義 drop database if exists 名字 刪除資料庫 use 名字 使用資料庫 引擎 show eng...

MySQL的基本使用

1.資料庫 資料庫就是一種特殊的檔案,其中儲存著需要的資料 2.資料庫分為關係型資料庫和非關係型資料庫 關係型資料庫 oracle db2 microsoft sql server microsoft access mysql 非關係型資料庫 nosql cloudant mongodb redis...

MySQL的基本使用

1 建立 刪除 選擇資料庫 create database 資料庫名 drop database 資料庫名 use 資料庫名 show tables 2 建立 檢視 刪除表 資料庫名 表名 欄位名使用反引號包裹,姓名 男等字串使用單引號包裹。create table student id int u...