常用的SQL命令

2022-09-01 16:39:21 字數 1436 閱讀 3876

資料型別

就四個,其他用不著,時間可以用字串時間戳的形式存

建立表

create table student(

id int auto_increment primary key, 自增長&&主鍵

name varchar(20) not null, 非空約束

age int not null, 非空約束

gender bit default 1, 預設約束

address varchar(20) unique, 唯一約束

isdelete bit default 0 預設約束

) charset=utf8;;

當前表的條數,別用count(*)

select count(1) from t_it
多表查詢注意點

# 多表查詢如果有乙個表是沒值的,會導致沒有結果,因為笛卡爾積是相乘的,0*100也是0

select

a.*,b.*,c.*

from

t_a a,t_b b,t_c c

# 所以需要要外連線

select

a.*,b.*,c.*

from

t_a a

left join

t_b b

on a.id == b.id

left join

t_c c

on b.id == c.id

分頁查詢

select * from t_it order by createtime desc limit 10,10

select a.*,b.id 'nextid',c.id 'lastid'

from

(select * from t_mine where id='001') a

left join

(select id from t_mine where createtime > (select createtime from t_mine where id='001') order by createtime limit 1) b

on a.id!=b.id

left join

(select id from t_mine where createtime < (select createtime from t_mine where id='001') order by createtime desc limit 1) c

on a.id!=c.id

常用SQL命令

create table 表名 列名 資料型別 長度 列名 資料型別 長度 主鍵 create table 表名 列名 資料型別 長度 primary key,自增主鍵 create table 表名 列名 資料型別 長度 primary key auto increment,非空約束 create...

oracle常用的sql命令

檢視使用者和預設表空間的關係 select username,default tablespace from dba users 檢視當前使用者能訪問的表 select from user tables oracle查詢使用者表 select from user all tables oracle查...

MYSQL常用SQL命令

顯示資料庫或表 show databases 然後可以use database name show tables 更改表名 alter table table name rename new t 新增列 alter table table name add column c n column att...