mysql學習之配套sql語句例項學習

2021-12-30 07:39:27 字數 3637 閱讀 5349

– 雙中劃線 + 空格: 注釋(單行注釋),也可以使用 # 號

– 建立資料庫

create database mydatabase charset utf8;

– 建立關鍵字資料庫

create database database charset utf8; # 報錯,該sql語句用了關鍵字,所以是一句錯誤的sql語句.

– 使用反引號

create database database charset utf8;

– 建立中文資料庫

create database 中國 charset utf8;

create database 中國 charset utf8;

– 解決方案: 告訴伺服器當前中文的字符集是什麼

set names gbk;

create database 中國 charset utf8;

– 檢視所有資料庫

show databases;

– 建立資料庫

create databases informationtest charset utf8;

– 檢視以information_ 開始的資料庫: _ 需要被轉義

show databases like 『information_%』;

show databases like 『information_%』; – 相當於information%

– 檢視資料庫建立語句

show create database yangyang;

show create database database; –關鍵字需要使用反引號.

– 修改資料庫informationtest 的字符集

alter database informationtest charset gbk;

– 刪除資料庫

drop database informationtest;

表操作的語句

– 建立表

create table if not exists student(

name varchar(10),

gender varchar(20),

number varchar(10),

age int

)charset utf8;

– 檢視所有表

show tables;

– 檢視以s結尾的表

show tables like 『%t』;

– 檢視表建立語句

show create table student\g – \g === ; 斜槓g 等價於分號;

show create table student\g – 將查到的結構旋轉90度 變成縱向的.

– 檢視表結構

desc student;

describe student;

show columns from student;

– 重新命名表名: student表 –> my_student(取資料庫名字前兩個字母)

rename table student to my_student;

– 修改表選項字符集

alter table my_student charset = gbk;

– 給學生表增加乙個id 放到第乙個位置

alter table my_student

add column id int

first; – mysql會自動尋找分號作為語句結束符.

– 將學生表中的number 學號變成固定長度,且放到第二位(id)之後.

alter table my_student

modify number char(10) after id;

– 修改學生表中的gender欄位為 ***

alter table my_student

change gender *** varchar(10);

– 刪除學生表中的年齡字段(age)

alter table my_student drop age;

– 刪除資料表

drop table class;

資料操作語句

– 插入資料

insert into my_student values

(1,』shifan』,』jim』,』male』),

(2,』lanou』,』jiarong』,』nv』);

– 插入資料: 指定字段列表:

insert into my_student (number,***,name, id) values(『lanou』,』male』,』yom』,3),

(『lanou』,』female』,』lily』,4);

– 檢視所有資料

select * from my_student;

– 檢視指定字段,指定條件資料

select id,number,***,name from my_student where id = 1; –id為1的對應字段資料

– 更新資料

update my_student set *** = 『famale』 where name = 『jim』;

– 刪除資料

delete from my_student where *** = 『male』;

– 插入資料(中文資料)

insert into my_student values(5,』藍鷗』,』張悅』,』男』);

–檢視伺服器到底識別哪些字符集,所有字符集.

show character set;

– 檢視伺服器預設的對外處理的字符集.

show variables like 『character_set%』;

– 修改伺服器認為的客戶端資料的字符集

set character_set_client = gbk;

– 修改伺服器給定資料的字符集為gbk;

set character_set_results = gbk;

– 快捷設定字符集

set names gbk;

– 檢視資料庫所有校對集.

show collation;

– 建立表使用不同的校對集

create table my_collate_bin(

name char(1)

)charset utf8 collate utf8_bin;

create table my_collate_ci(

name char(1)

)charset utf8 collate utf8_general_ci;

– 插入資料

insert into my_collate_bin value(『a』),(『a』),(『b』),(『b』);

insert into my_collate_ci value(『a』),(『a』),(『b』),(『b』);

– 排序查詢.

select * from my_collate_bin order by name;

select * from my_collate_ci order by name;

– 有資料後修改校對集

alter table my_collate_ci collate = utf8_bin;

alter table my_collate_ci collate = utf8_general_ci;

MySQL學習之SQL語句(二)

一 sql簡介 sql 結構化查詢語言 structured query language 是一種資料庫查詢和程式語言,用於訪問資料以及查詢 更新和管理關係資料庫系統。二 sql分類 1 資料定義語言 簡稱ddl data definition language 用來定義資料庫物件。關鍵字 crea...

mysql學習之五 sql語句學習3

好吧,大家覺得這種字型還是比較好看,所有我們就換這種字型了。insert into 語句用於向 中插入新的行。語法insert into 表名稱 values 值1,值2,我們也可以指定所要插入資料的列 insert into table name 列1,列2,values 值1,值2,update...

mysql學習之六 sql語句學習4

where 子句 如需有條件地從表中選取資料,可將 where 子句新增到 select 語句。語法select 列名稱 from 表名稱 where 列 運算子 值如果只希望選取居住在城市 beijing 中的人,我們需要向 select 語句新增 where 子句 select from per...