最近用到的 sql sever 語句
查看表結構:
sp_help 表名
建表
create table 表名
(
id int identity(1,1) primary key,--identity(1,1)設置增長率,primary key設置主鍵
name nvarchar(50) not null,
age int not null
)
- 刪除表中的數據, 表還在
delete from 表名
- 清空表中數據, 重置表中數據
truncate table 表名
delete會產生很多的日志(表中有多少數據, 就產生多少日志), 如果是 truncate 就會產生一行日志
- 直接刪表, 徹底刪除 表不存在了
drop table 表名
查看數據
select * from 表名 where 字段=值
修改數據
UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
增加語句
insert into 表名(列名) values(對應的值)
insert into Db(name, age) values('admin, 123')
刪除
delete from 表名
delete from 表名 where 字段=值
查看所有表名:
select name from sysobjects where xtype='u'
查看所有表名, 和每個表中的行數
select schema_name(t.schema_id) as [Schema], t.name as TableName,i.rows as [RowCount]
from sys.tables as t, sysindexes as i
where t.object_id = i.id and i.indid <=1
查看所有表名 和表的一些基本信息
select * from sys.tables
查詢所有用戶定義表
select * from sys.objects Where type='U' And type_desc='USER_TABLE'用戶定義表個數
select Count(0) as '用戶定義表的個數' from sys.objects Where type='U' And type_desc='USER_TABLE'