MySQL基礎操作語法(小白系列)
Mysql基礎操作語法:
以下是我本人看一些視訊自學總結的一些關於資料庫的一些最最基本操作語法,並附上操作截圖以便於理解,都是舉例,沒有貼具體的語法,以便於大家理解,小白系列,請諸位同為菜鳥的小白遷就著看一下,相信對於新手系列還是有一定幫助的。
show databases; //顯示所有的資料庫
create database gc; //建立資料庫gc
drop database gc; //刪除資料庫gc
create database table2; //建立表table2
use table2; //使用表table2 ,然後可以進行表的增刪查改
show tables; //顯示所有的表
create table account;
show tables;
describe account; //獲取表結構資訊
drop table account1;
//建立表的資料型別
CREATE TABLE account1 (
id bigint(20),
createTime datetime,create
ip varchar(255),
mobile varchar(255),
nickname varchar(255),
passwd varchar(255),
username varchar(255),
avatar varchar(255),
brief text,
job varchar(255),
location varchar(255),
qq varchar(255),
gender int(11),
city varchar(255),
province varchar(255)
) ;
//增刪table列
alter table [table_name] add [column_name] [data_type] [not null] [default]
alter table account add c1 int(11) not null default 1;
alter table [table_name] drop [column_name]
alter table account drop c1;
//修改table--修改列資訊和表名
alter table [table_name] change [old_column_name] [new_column_name] [data_type]
1, 只改列名;[old_column_name] !=[new_column_name]
alter table account change city country varchar(255);
2,只改資料型別;[old_column_name]== [new_column_name],data_type改變
alter table account change country country text;alte
3,列名和資料型別都改;
修改表名: alter table [table_name] rename [old_column_name]
alter table account rename newaccount;
//插入&查看錶資料
insert into [table_name] values(值1,值2,.....)
insert into [table_name](列1,列2,......) values(值1,值2,.....)
insert into book values
insert into book(id) values(6)[其他的必須設定預設值]
select * from table_name;//檢視全部表資料
select * from account;
select col_name1,col_name2,......from table_name;//檢視特定列*
//where條件查詢
select * from table _name where col_name [運算子] 值
select * from book where title = 't' ;[在book表中在title列下查詢並顯示值為t的資料行]
組合條件and,or;
where 後面可以通過and與or 運算子組合多個條件篩選;語法如下:
select * from table_name where col1 = xxx and col2 = xx or col3>xx
select * from book where id = 5 and title = 't2' or id<6;[除數字外其他的資料項內容都要加單引號]
運算子
//where 的null判斷
select * from table_name where col_name is null;
select * from table_name where col_name is not null;
//select distinct(精確的)
select distinct col_name from table_name;
select distinct title from book;
//select 結果按order by排序【asc升序,desc降序排列】
按單一列名排序:select * from table_name [where 子句] order by col_name[asc/desc]
按多列排序:select * from table_name [where 子句] order by col1_name[asc/desc],col1_name[asc/desc]……
//select 結果按limit擷取
select * from table_name [where 子句] [order 子句] limit [offset,] rowCount;
offset:查詢結果的起始位置,第一條記錄的其實是0
rowCount:從offset位置開始,獲取的記錄條數
需注意的是 limit rowCount = limit 0,rowCount [行從0開始]
//insert into 與select組合【資料遷移】
insert into[表名1] select 列1,列2 from [表2]
insert into book2 select * from book where id<4;
insert into[表名1](列1,列2 ) select 列3,列4 from [表2]
insert into book2(title) select content from book where id=5 ;
//更新表資料
修改單列:update 表名 set 列名 = xxx[where 字句]
update book set content = 'nice day' where id=3;
修改多列:update 表名 set 列名1 = xxx,列名2 = xxx……[where 字句]
//高階where 的in操作符
select * from 表名 where 列名 in(values1,values2……)
select * from book where title in('t','h');
select * from 表名 where 列名 in(select 列名 from表名)
select * from book2 where title in(select title from book where id<4);
註解:列名 in(values,values2……)等同於 列名=values1 or 列名= values2;
//高階between操作符
select* from 表名 where列名 between 值1 and 值2;
select* from 表名 where列名 not between 值1 and 值2;
//高階where的like 操作符【字串模糊匹配】
select * from 表名 where 列名 [not] like pattern【like 後面加單引號】
select * from book where content like 'c%';
pattern:匹配模式,比如 ‘abc’僅僅匹配abc
'%abc'[以abc結尾的字串]
‘abc%’[以abc開頭的字串];
‘%abc%’[包含abc的字串]
"%"是一個萬用字元,理解上可以把她當成任何字串;
以上就是我總結的一些最基礎的資料庫語法,希望會對大家有所幫助,祝大家在敲鍵盤這條道路上越走越遠。