MySQL的基本使用

2021-10-25 12:11:41 字數 2024 閱讀 4916

1、建立/刪除/選擇資料庫:

create database `資料庫名`;

drop database `資料庫名`;

use `資料庫名`;

show tables;

2、建立/檢視/刪除表:

資料庫名、表名、欄位名使用反引號包裹,姓名、男等字串使用單引號包裹。

create table `student`(

`id` int unsigned primary key auto_increment,

`name` varchar(4) not null comment '姓名',

`gender` enum('男','女') default '男' not null comment '性別',

`birthday` date not null comment '出生日期'

)charset=utf8;

desc `student`;

show create table `student`\g

drop table `student`;

int

表示該字段的資料型別是整型

int unsigned

表示該字段的資料型別是無符號整型(正整數)

varchar(4)

表示該欄位儲存可變長度的字串,最多儲存4個字元

enum('男','女')

表示該字段是列舉型別,只能儲存「男」和「女」兩種值

date

表示該欄位儲存日期,如:1999-05-25

primary key

表示該字段是表的主鍵,用於唯一地標識表中的某一條記錄

auto_increment

表示該字段是自動增長的,每增加一條記錄,該字段會自動加1

not null 

表示該字段不允許出現null值

default '男

表示該字段的預設值為「男」

comment'姓名'

表示該字段的注釋為「姓名」

charset=utf8

指定該錶的字符集為utf8

3、資料的新增、查詢、更新、刪除

insert into `student` (`name`,`gender`,`birthday`) values

('張三','男','2000-10-01'),

('李四','男','2008-11-01'),

('王五','女','2012-12-01');

select * from `student`;

select * from `student` where `gender`='男';

select * from `student` where `gender`='男' and `name` like '張%';

select `name`,`gender` from `student` where `id`=2;

select * from `student` where `gender`='男' order by `birthday` asc;

update `student` set `name`='趙六',`gender`='女' where `id`=2;

delete from `student` where `id`=2;

truncate `student`;

增:insert into `表名` (`欄位名`) values (資料),(資料),(資料),...(資料);

查:select `欄位名` from `表名` where 查詢條件 [order by `排序的欄位名` asc(公升序)/desc(降序)];

改:update `表名` set `欄位名`=`更新值` where 更新條件;

刪:delete from `表名` where 刪除條件;

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的基本使用

sql ddl 資料定義語言 create drop alter dml 資料操作語言 select insert update delete dcl 資料控制語言 grant revoke commit rollback 最基礎的 建庫 建表 show databases use 資料庫名稱 cr...