数据库的基本操作

设计表

列:(字段field) 列名,列数据类型,列是否为空

列名:命名建议使用大驼峰,原因:为了映射成C#中的对象时,保留命名

StudentName student_name _studentName studentName

(PS: 两种语言中间作比较,类似于并不是完全相等,仅仅是可以理解成相同的类型和用法,但并不完全一样)

datetime 常用的时间类

字符串相关:

varchar 和 test 的区别 就是存储范围的大小,test比varchar 大

varchar :有限的字符 (类似于标题之类的有长度有限的数据)

test :无限的字符 (类似于文章之类的存储内容过大的数据)

写法:

If exists (select * from *,tables where name = ‘表明’)这一行是查询表中有没有重复语句,

Drop table 删除内容

Create table 创建一个列表

Id int not null primary key

列表 数据类型 是否为空 主键

SQL语句 : 增 删 改 查

sql注释 : -- (两个单横线)

插入:

--dbo == datebase owner 表的拥有者

--格式: insert into 表名(列1,列2......)values (值1,值2......)

--SQL中字符串使用单引号包裹,数字不需要包裹。

Insert into dbo.table001([name],age ) values (‘ 张三’ , 19 ) ;

Insert into dbo.table001([name],age,Birthday ) values (‘ 李四’ , 20, getdate() ) ;

一次插入多行数据

Insert into table001([name]) select StudentName from Student;

更新 ,修改数据

--格式: update 表名 set 列1 = 新值1,列2 = 新值2, where 条件

-- 切记: 别忘记where

update table001 set [name] = ‘王五’ where id = 5

update table001 set [name] = ‘王五2 ’ , age = 20 where id = 5

update 深入:表间关系,通过主键,外键建立关系

and 前是建立关系,and后面的才可以访问主键表中的其他列

删除 (物理删除)

在正式工作中很少用

-- 格式: delete from 表明 where 条件

-- 切记: 一定不要忘记where

--delete语句不加where 也会清空数据,单编号不会重置

delete from table001 where id = 6

delete from table001 where id >= 6

delete from table001 where id in (6 , 7)

清空数据表

自动编号的列重置,项目上线前,把测试数据全部清空

--truncate table table001

查询

只想执行某一行SQL语句,选中再执行

-- * all 全部,表示所有列

select * from Student --; 语句分割符可以省略

查询一列

select Id from Student

-- 多列用英文逗号分割

select Id,[Name] from Student

select * from Student

Select Id, [Name], Age, Birthday from table001;

Select * from table001 where id = 1

select * from table001 where [Name] = ‘王五’

区间

Select * from table001 where Age between 18 and 19; --between...and...

select * from table001 where age >= 18 and age <= 19; --and == && 并列

or 或者

select * from table001 where [Name] = ‘王五’ or [Name] = ‘赵六’

--in 在范围内 not in

select * from table001 where [Name] in ( ‘ 王五’, ‘ 赵六’, ‘ ’)

select * from table001 where [Name] not in ( ‘ 王五’, ‘ 赵六’, ‘ ’)

like 像...一样,SQL模糊

Select * from table001 where [Name] like ‘张% ’ -- %在结尾表示以...开头

Select * from table001 where [Name] like ‘ %三 ’ -- %在开头表示以...结尾

Select * from table001 where [Name] like ‘ %张三% ’ --% %包含某些字符

Select * from table001 where [Name] not like ‘ %三% ’ -- 不包含某些字符

as 别名

Select Id 编号 ,Name ‘姓名’ , Age as 年龄, Birthday AS 生日

From table001 as tb001

Where tb001.Age = 18

定义变量

第一种写法:declare @ 。。。

插入结果:

第二种写法:print @。。。

输出结果:

判断语句:

If @age > 9 
begin --相当于{ 
Print (‘age 大于9’) ;  -- 语句
end -- 相当于}
else
begin --相当于{
Print (‘age 小于9’) ;  -- 语句
end -- 相当于}

输出结果:

使用常量值合并成结果集

结果:

结果:

字符串合并

Select‘hello’ + ‘world’ as 标题

比较的运算

> < = >= <= !=

原始输出结果:

Select * from table001 where Age >= 20

Select * from table001 where Age != 20

Select count (Id ) from table001 where age <> 19

Select count (2 ) from table001 where age != 19

distinct去重

Select count (distinct [Name] ) fromtable001

子查询

exists 不常用

错误实例

排序

分组:

用来统计

人名: 个数

[ Name ] 列没有包含在聚合函数或GROUP BY 子句中

select [Name] ,age,count(*) from table001

group by [Name]

select [Name] ,age,count(*) from table001

group by [Name], age

分组之后下条件

select [Name], count (*) from table001

where count (*) > 2

group by [Name]

select [Name], count (*) from table001

group by [Name]

having count(*) >= 2 -- 拥有 having在SQL中专门给group不要加条件,where子句专门给select, update, delete加条件

带where , 带order by , 带 group by , 带 having

select [Name], count (*) as Num

From table001

Where [name] in(‘张三’, ’李四’) -- 1.先where

group by [Name] --2. 再分组

having count(*) >= 2 -- 3.对分组做筛选

order by Num desc -- 4. 排序

索引

优化查数据的性能。

主键:就是一种索引

默认情况下,表中除主键外的列不带索引。

索引要适度,不易过多,把经常用来查询条件中的列最好添加索引,提高查询速度。

视图

表的副本,虚拟的表,视图会跟着和视图有关的表的变化而变化

一般情况下,视图用来查询多,更新视图,删除视图,插入视图相对少

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值