数据库语言(书上总结,复习用)

1.数据库操作语句

一、创建数据库

create database 数据库名称
on(

)
log on(

)

如:create database test1
on(
    name=测试数据1,filename='H:\SQL Server\测试数据1.MDF',
    size=10mb,maxsize=50mb,filegrowth=5mb
)
log on(
    name=测试数据1日志,filename='H:\SQL Server\测试数据1日志.LDF'.
    siae=10mb,maxsize=20mb,filegrowth=5mb
)

    创建一个名称为test1的数据库,并设定数据文件为"H:\SQL Server\测试数据1.MDF”,大小为10mb,最大为50mb,每次增长5mb。事务日志文件为"H:\SQL Server\测试数据1日志.LDF",大小为10mb,最大为20mb,每次增长为5mb。

二、修改数据库

alter database 数据库名称
{    
    add file filespec               -- 指定添加的文件
    add log file filespec           -- 指定添加的日志文件
    remove file logical_file_name   -- 删除文件描述并删除物理文件
    modify file filespec            -- 指定要更改的文件
    modify name = new_dbname        -- 重命名
}

三、使用和删除数据库

使用数据库:
use database 数据库名称

删除数据库:
drop database 数据库名称

 

2.表操作语句

一、表的创建

(1)基本用法

create table 表名
(
列1 数据类型 [null|not null] [primary | unique]
列2 ……
)

如:create table student
(
sname char(10) 
sno char(10) not null primary key//创建主键,输入不能为空值
)
--primary key(列1,列2)创建多个主键
--unique 指定列具有唯一性

(2)与其他表关联

foreign key reference 关联表名(关联列名)
如:
create table authors
(
    authorid int not null primary key,
    authorname char(20)
)


create table book
(
    authorid int foreign key references authors(authorid)
)

(3)由其他表来创建新表

select 列表名 into 表1 from 表2

如:
use school
select 学号,姓名,班号 into student1
from student

从student表创建student1表,它包含student表的学号、姓名和班号三个列和对应记录

(4)修改表结构

alter table 表名
    add[列名 数据类型]
        [primary key|constrain]
        [foreign key(列名)references 关联表名(关联列名)]
    drop [constraint]约束条件 | column 列名

如:
use school
alter table student1 add 民族 char(10)

use school
drop table student1

二、数据操作

(1)数据插入

insert [into] 表名 [列表名] values(数据值)

如:
use school
insert into student values('200','小明','男','1992-2-3','0035')
--若into没有指定列名,则插入的数据在每个列上均要有值,且与表中顺序一致

(2)数据修改

update 表
set 列 = 数据值
(where)   --条件

如:
update student
set 性别 = '男'
where 学号 = '200'

(3)数据删除

delete 表 (where条件)

如:
use student 
delete student where 学号 = '200'

四、数据查询

一般形式

select 列表名
from 表名
[where 条件]
[group by 分组]
[having 分组条件]
[order by 排序(默认升序)(desc 降序)]

    创建数据库和表

Create database myDB
GO
Use myDB
GO

create table student
(sno varchar(10) primary key,
 sname varchar(20) not null,
 sage tinyint,
 ssex varchar(2),
 sdept varchar(30))
GO

create table course
(cno varchar(10) primary key,
 cname varchar(20) not null,
 credit tinyint,
 snumber tinyint)
GO

create table sc
(sno varchar(10) ,
 cno varchar(10) ,
 grade tinyint,
 primary key(sno,cno)
)
GO

   添加数据

use myDB
insert into student  values('001','王倩',18,'m','CS');
insert into student  values('002','张三',18,'m','E');
insert into student  values('003','张小明',19,'f','E');
insert into student  values('004','刘佳',20,'m','CS');
insert into student  values('005','李四',18,'f','CS');
insert into student  values('006','王小平',19,'m','C');
insert into student  values('007','吴红梅',18,'f','CS');
insert into student  values('008','杜海',20,'m','E');
insert into student  values('009','杨然',18,'f','C');
insert into student  values('010','郭晨光',17,'m','CS');
insert into student  values('011','江冰',19,'m','CS');

insert into course values('c01','数据库',2,5);
insert into course values('c02','C语言',2,6);
insert into course values('c03','数据结构',3,4);
insert into course values('c04','计算机基础',2,7);
insert into course values('c05','操作系统',3,5);
insert into course values('c06','信息安全',2,10);
insert into course values('c07','英语',2,6);
insert into course values('c08','高数',3,8);

insert into sc values('001','c01',78);
insert into sc values('001','c02',34);
insert into sc values('001','c03',56);
insert into sc values('001','c04',69);
insert into sc values('001','c05',55);
insert into sc values('001','c06',33);
insert into sc values('001','c07',64);
insert into sc values('001','c08',35);
insert into sc values('002','c03',65);
insert into sc values('002','c05',88);
insert into sc values('002','c06',93);
insert into sc values('002','c07',74);
insert into sc values('003','c01',78);
insert into sc values('003','c04',45);
insert into sc values('003','c05',77);
insert into sc values('003','c06',23);
insert into sc values('003','c07',80);
insert into sc values('004','c02',46);
insert into sc values('004','c03',74);
insert into sc values('004','c05',42);
insert into sc values('004','c06',70);
insert into sc values('004','c07',90);


(1)disintct

    去掉重复行

  (2)  between ……and……

   选择数据范围:between 60 and 100 在60~100的范围

  (3)   order by(asc)(desc)

  对记录进行升、降排序

 (4)    聚合函数

avg        --计算平均值
count        --计算选择的项数,不忽略空值
min        --计算最小值
max        --计算最大值
sum        --计算和
stdev        --计算表达式所以数据标准差
stdevp        --计算总体标准差

  (5)  as

   改变列名

  (6)  year()

 year()函数接受date参数,并返回日期的年份、

(7)检测空值

where 表达式 is null

where 表达式 is not null

isnull(数据,0)--如果是空值将非空值代替空值,如果不是空值则返回原值

五、简单连接查询

  (1)

select 列1,列2
from 表1,表2
where 表1.列 = 表2.列(连接条件)

  (2)

use myDB
select y.cno,avg(x.grade)
from sc x,sc y
where x.sno=y.sno
group by y.cno

  (3)

use myDB
select grade
from sc
where sno=(select sno from student where sname='张三')

  (4)  并

use myDB
select cno
from sc
where cno='c01'
union 
select cname
from course
where cno='c01'

六、数据类型

分类数据类型定义符
整数型bigint,int,smallint,tinyint
逻辑数值型bit
小数数据类型decimal,numeric
货币型money,smallmoney
近似数值型float,real
字符型char,varchar,text
Unicode字符型nchar,nvarchar,ntext
二进制数据类型binary,varbinary,image
日期时间类型datetime,smalldatetime

    七、变量                                                                         

  (1)  局部变量:以@开头

定义:

declare {@局部变量名称 数据类型}
如:

declare @f float,@i int

赋值:

set @局部变量名 = 表达式    --直接赋值
select {@局部变量 = 表达式}    --查询语句里赋值

(2)  全局变量:以@@开头

系统定义,不能自己定义,不能赋值

   八、运算符

  (1)  算数运算符

+加  -减  *乘  /除

  (2)  赋值运算符

=赋值

  (3)  比较运算符

>大于 <小于 >=大于等于 <=小于等于 !=不等于 !<不小于 !>不大于

(4)逻辑运算符

逻辑运算符含义
all当一组比较关系的值都为true时,返回true
and当要比较的两个布尔表达式的值都为true,返回true
any

只要一组比较关系中有一个值为true,返回true

between只有操作数在定义范围内,返回true
exists如果在子查询中存在就返回true
in如果操作数在所给的列表中,返回true
like

如果操作数与模式相匹配,返回true

not对其他所有布尔运算取反
or

只要比较的两个表达式有一个为true,返回true

some如果一组比较关系中有一些为true,返回true

 

通配符含义
%包含0个或多个任意字符
_任何单字符
[]指定[]范围中任何单个字符
[^]不属于指定[]范围内任何单字符

 九、 控制流语句

控制流语句说明
begin   end定义语句
if   else条件成立执行if,否则执行else
case分支语句
while循环语句
goto无条件跳转语句
waitfor延迟语句
break跳出循环语句
continue重新开始循环语句

  (1)  begin  end

begin 
{
T-SQL语句 | 语句块
}
end

  (2)  if   else

use myDB
if(select grade from sc where sno='001' and cno ='c01')>80
begin
  print '001同学的c01课程'
  print '考的还不错'
end
else
begin
  print '001同学的c01课程'
  print '还需继续努力'
end

  (3)  case

use school
select 姓名,单位
    case 职称
        when '教授' then '高级职称'
        when '副教授' then '高级职称'
        when '讲师' then '中级职称'
        when '助教' then '初级职称'
    end as '职称类型'
from teacher

  (4)  while

declare @s int,@i int
set @i=0
set @s=0
while@i<=100
    begin
        set @s+=@i
        set @i+=1
    end
print '1+2+3+……+100='+cast(@s as char(25))

计算1加到100

  (5)  goto

declare @s int,@i int
set @i=0
set @s=0
my_loop:
set @s+=@i
set @i+=1
if @i<=1 goto my_loop
print '1+2+……+100='+cast(@s as char(25))

  (6)  函数

日期和时间函数
函数参数功能
dateadddatepart,number,date以datepart指定的方式,返回date加上number之和
datediffdatepart,datel,date2以datepart指定的方式,返回date2与datel的差
datenamedatepart,date返回日期date中datepart指定部分所对应字符串
datepaptdatepart,date返回日期date中datepart指定部分所对应整数值
daydate返回日期天数
getdate 返回当前的日期和时间
monthdate返回指定日期的月份数
yeardate返回指定日期的年份数

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值