安装SQL Server 2008 后的几件事:
1:开启远程(sa)登陆,数据库属性=>连接=>允许远程连接此服务器;sa账户属性=>状态=>登录选项中勾选启用.
2:修改默认用户名,新增用户.
3:配置权限,在高权限的账户中(sa)查看需要配置账户的属性=>在用户映射中把不想给此账户查看的数据库勾去掉.
数据库语言
数据库查询语言-DQL(数据查询)
数据库操纵语言-DML(插入,删除,修改,查找)
数据定义语言-DDL(创建)
数据库控制语言-DCL(更改用户的角色权限 )
DML语言部分:
SELECT - 从数据库表中获取数据 UPDATE - 更新数据库表中的数据 DELETE - 从数据库表中删除数据 INSERT INTO - 向数据库表中插入数据 DDL语言部分:CREATE DATABASE - 创建新数据库 ALTER DATABASE - 修改数据库 CREATE TABLE - 创建新表 ALTER TABLE - 变更(改变)数据库表 DROP TABLE - 删除表 CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引
SQL基础查询语句:
SQL Server不区分大小写
---------------------------------------------------------------------------
select * from student //查询student表的所有
条件查询
select 列名 from 表名 where 条件 //条件查询的语法格式
select name,sex,age from student //查询name,sex,age指定列
select * from student where name = '小明' //where条件查询,只查找小明的所有记录
select * from student where age>22 //查询年龄大于22的
select count(*) student //查询所有人数的数量(count聚合函数)
select * from student where age <> 22 //查询年龄不等于22岁的
模糊查询
select * from student where iphone like '123%' //模糊查询(百分号代表所有,但不包括空)
select * from student where name like '张' //查询所有包含张字的名字
select * from student where age like '2' //查询年龄包含2的所有记录
范围查询
select * from student where id between 1 and 5 //查询ID1-5的记录
匹配开头、结尾、中间
select * from student where iphone like '1%' //匹配开头为1的所有记录
select * from student where iphone like '%23' //匹配结尾为23的所有记录
select * from student where iphone like '%23%' //匹配中间为23的所有记录
select * from student where iphone like '2%3' //匹配开头和结尾为2,3的所有记录
通配单个
select * from student where Phone like '_8%' //通配查询phone的第二位是8的记录
匹配多个字符(正则表达式)
select * from student where phone like '_[A-Z0-9]6%' //查询的第一位为所有,第二位为正则表达式,也是所有,第三位是6,后面的也是所有记录
不匹配
select * from student where phone like '!2[1-2]4' //不匹配第一位为2,第二位为1-2,第三位为4的记录
查询null值
select * from student where phone is null //查询null值
select * from student where phone is not null //查询非null值
SQL注释
第一种:--select * from student //使用--注释SQL语句
第二种:/*select * from student*/ // /**/大批量进行注释
逻辑查询
AND
OR
select * from student where name = '小明' and sex = '男' //两边都为真的时候才成立,一边为假就不能成立
select * from student where name = '小红' and sex = '男' //两都其中一边为真就成立
in查询
源查询语句:select * from student where id=1 or id=2 or id=3 or id=4;
in查询语句:select * from student where id in(1,2,3,4); //查询ID为1,2,3的记录
not in 查询:select * from student where id not in(1,2,3); //不查询ID为1,2,3记录
TOP字句
select top 2 * from student //查询前2行的记录
order by 排序语句
select * from student order by id asc //正序排列
select * from student order by id desc //倒序排列
select * from student order by 1,2,3,4,5,6,7 //按列排序
distinct去重复数据
select distinct age from student order by 1 //去重复数据