SQL語句基礎總結

2021-10-03 22:12:56 字數 1750 閱讀 1354

判斷是否存在資料庫

if

(exists

(select *

from sysdatabases where name=

'資料庫名'))

//exists:根據檢查子查詢是否至少返回一行資料,exists 對應的返回true或false

// 子查詢實際上並不返回任何資料,只用來檢測 "行" 的存在

//sysdatabases:包含所有的資料庫

查詢除系統資料庫以外的資料庫

select

*from sysdatabases

where name not in

(select name from sysdatabases wherename=

'master' or name=

'model' or name=

'msdb' or name=

'tempdb'

)

檢視使用者自己建立的所有的表

use 資料庫名

select

*from sysobjects where type =

'u'

檢視資料庫中所有的列名(字段)

use 資料庫名

select

*from syscolumns

檢視某張表中所有的列名(字段)

use 資料庫名

select

*from syscolumns where id =

object_id

('表名'

)

檢視某一列名(字段)在表中的列的排號,名稱,資料型別,長度

select colid , name,

type_name

(xtype)

as, length

from syscolumns where id =

object_id

('表名'

)

檢視資料庫中所有的表

use 資料庫名

select

*from sysobjects where name=

'表名'

新建資料庫

create database 資料庫名
新建表

create table 表名(

)

建表流程

create database 資料庫名

create table 表名(

列名 型別 primary key --設定主鍵 identity(1

,1)--設定標識列

列名 型別 check()

--約束 default

--預設值 not null

--不允許為空

列名 型別 [foreign key]

(可加,可不加) references 引用表名(列名)

--設定外來鍵

列名 型別 unique --唯一約束

)

刪除資料庫

drop database 資料庫名
刪除表

use 資料庫名

drop table 表名

基礎SQL語句總結

一般對於資料庫的操作有增刪改查。增是指在資料庫中增加一行或者多行資料 insert into.刪是指刪除資料庫中一行或多行資料 delect from.改是指在資料庫中修改某些字段 update.set.查指查詢資料庫中的資料。可以是 的也可以是針對某乙個欄位的 一 查詢語句 1 簡單的查詢 列如資...

MYSQL 基礎SQL語句總結

dml 資料操縱語句 增刪改 1 插入記錄 insert into tablename field1,field2,fieldn values value1,value2,valuen insert into test uid,name values 100,0,peter 如例,通過insert ...

MySQL基礎sql語句總結(一)

mysql資料庫基礎知識點總結 一 資料庫 儲存有組織的資料的容器 主鍵 primary key 一一列 或一組列 其值能夠唯一區分表 中每個行。主鍵的最好習慣 除mysql強制實施的規則外,應該堅持的 幾個普遍認可的最好習慣為 不更新主鍵列中的值 不重用主鍵列的值 不在主鍵列中使用可能會更改的值。...