MySQL操作命令大全

2021-10-10 15:47:45 字數 4081 閱讀 9604

二、表操作

三、資料操作

四、日期時間處理

create

database mydb;

show

databases

;

drop

database mydb;

use mydb;
select

database()

;

show

tables

;

create

table

user

( id int(11

)not

null

comment

'序號'

, username varchar(50

)null

comment

'姓名'

, *** tinyint(4

)null

comment

'性別'

, age int(11

)null

comment

'年齡'

, education int(11

)null

comment

'學歷,列舉值..'

,primary

key(

`id`

)using

btree

);

desc

user

;

drop

table

user

;

truncate

table

user

;

delete

from

user

;

rename

table old_user to new_user;

rename

table

user

to old_user, user_bak to

user

;--多表間快速修改

alter

table

user

addcolumn id int(11

)not

null

;

alter

table

user

drop

column id;

alter

table

user

addindex index_name (username)

;

alter

table

user

drop

index index_name;

insert

into

user

(id, username, ***)

values(1

,'tom',1

);

delete

from

user

where id =

1;

update

user

set username =

'jay'

where id =

1;

select

*from

user

where id =

1;

select

distinct id from

user

;

# mysql沒有merge into方法

update

user u,

user_tmp ut

set u.username = ut.username

where

u.id = ut.id;

select

*from

user

order

by id;

select education from

user

group

by education;

# mysql沒有merge into方法

select

education,

group_concat( username order

by username )

from

user

group

by education;

select

now();

select

current_timestamp()

;

select date_format(

'2020-11-19 20:06:00'

,'%y-%m-%d %h:%i:%s');

--2020-11-19 20:06:00

select str_to_date(

'2020-11-19'

,'%y-%m-%d');

--2020-11-19

select str_to_date(

'2020-11-19 09:00:00'

,'%y-%m-%d %h:%i:%s');

--2020-11-19 09:00:00

select to_days(

'2020-01-01');

--737790

select time_to_sec(

'01:00:02');

--3602

select sec_to_time(

3602);

--'01:00:02'

select makedate(

2020,65

);--'2020-03-05'

select maketime(20,

25,30)

;--'20:25:30'

select unix_timestamp(

'2020-11-19 20:30:00');

-- 1605789000

select from_unixtime(

1605789000);

--'2020-11-19 20:30:00'

日期時間單位:year、month、week、day、hour、quarter、minute、second、microsecond

增加函式:date_add()

減少函式:date_sub()

select date_add(

'2020-11-19'

,interval

1year);

--2021-11-19

select date_sub(

'2020-11-19'

,interval

1month);

--2020-10-19

使用函式:timestampdiff

單位:second、minute、hour、day、month、year

select timestampdiff(

month

,'2020-09-01'

,'2021-09-01');

--12

mysql 命令大全 Mysql 命令大全

輸入mysql進入,如同輸入cmd模式 1 使用show語句找出在伺服器上當前存在什麼資料庫.mysql show databases 2 建立乙個資料庫mysqldata mysql create database mysqldata.3 選擇你所建立的資料庫 mysql use mysqldat...

網頁mysql命令大全 Mysql命令大全

monica sehgal hal simlai joseph irvine 3 rows in set 0.00 sec 注意 這裡用到concat 函式,用來把字串串接起來。另外,我們還用到以前學到的as給結果列 concat f name,l name 起了個假名。5.1 建立資料表 命令 c...

網頁mysql命令大全 mysql命令大全

7.1 乙個建庫和建表的例項1 drop database if exists school 如果存在school則刪除 create database school 建立庫school use school 開啟庫school create table teacher 建立表teacher id ...