資料庫 SQL語法一

2021-09-07 07:21:26 字數 2241 閱讀 4226

建立表語句

create table tablename(col_name1 type,col_name2 type,......);

常用type說明

int 正數

char(length) 定長字串,短於指定長度用空格填充

varchar(length) 變長字串

刪除表語句

drop table tablename;

create table 語句,同時建立乙個序列的例子

create table table2(id

int(11) not null auto_increment,name varchar(20),age int

,primary key (id));

id int(11

):指定int型別整數的大小是11位,不足的前面填充0

not null:不可以為空值

auto_increment:表示自動增長這個字段

primary key (id):設定id為主鍵,並且建立唯一索引

插入資料語句

insert into tablename(col1,col2,...) values(val1,val2,...);

向表插入一行資料,前面指定列名,後面是要插入的值。

ssql語言當中字串用單引號。

自增主鍵一般不會手動設定值。

檢視表結構

desc 表名;

查詢資料語句

select *from tablename;

select col1,col2,... from tablename;

第乙個select語句表示查詢指定表的所有字段。

第二個select語句表示查詢指定表的指定字段。

返回指定行數的select查詢語句

limit字句的語法

select *from tablename limit m,n

其中m是指記錄開始的index,從0開始,表示一條記錄

n是指從第m+1條開始,取n條

slect * from table1 limit 0,5

select * from table1 limit 6,3

帶有條件的查詢語句

select col1,col2,... from tablename whree conditional;

where字句後面是條件

--and 與

--not 非

--or 或

--=等於

--like 通過"%"

萬用字元匹配指定模式

--in 包含子集

--<>不等於

--《小於

--<=小於等於

-->大於

-->=大於等於

例子:①mysql> select * from table1 where age > 30

;②mysql> select * from table1 where name='張大'

;③mysql> select * from table1 where ***='

男' and age > 30

;④select * from table1 where name like '王%'

;⑤select * from table1 where age in (20,40

);⑥select * from table1 where age =20 or age =40

;⑦select * from table1 where age not in (20,40

);⑧select * from table1 where name not like '

測試%';

別名

--列別名

select fieldname alias from tablename;

--表別名

select alias fieldname from tablename alias;

例如select name 姓名,*** 性別,age 年齡,

class

班級 from table1;

--把列名name輸出為姓名顯示出來

select a.name,a.*** from table1 a;

--為表起乙個別名

資料庫SQL 基礎語法(一)

資料庫結構 1.服務端 用於接收並處理其它程式發出的請求的程式 軟體 或者是安裝此類程式的裝置 計算機 2.客戶端 向伺服器發出請求的程式 軟體 或者是安裝此類程式的裝置 計算機 3.庫 就是一堆表組成的資料集合 4.表 table 類似excel,由行和列組成的二維表喪。5.欄位 表的列 垂直方向...

資料庫及SQL語法

常見資料庫 1 oracle database 甲骨文公司 2 sqlserver 微軟 3 db2 ibm公司 4 postgresql 開源 5 mysql 開源 滲透測試常用函式 gpoup concat col 返回由屬於一組的列值連線而成的結果 ascii char 返回字元的ascll碼...

資料庫之SQL語法

建立資料庫 create database mytest 建立表 create table t user primary key 定義該列為主鍵列 auto increment表示該列的值,由dbms自動生成,為自動增長列 auto tab鍵 id int primary key auto incr...