2022年数据库 2.pdf
1.创建数据库 create database zhangsan; 2.删除数据库 drop database zhangsan; 3.创建表 Createtablelisi4.删除表 drop table lisi; 5.查询 Select * from studscoreinfo Where studscore60 and courseid= 英语 ; 6.数据插入 Insert intoclassinfo values( 4 班 , 数学 , good ); 7.数据更新Update classinfosetclassdesc =goodwhere classid=3 班 ; select* from classinfo; 8.数据删除Delete fromstudinfoWhere studno =2011003 ; select* from studinfo; 9. select语句里面加表达式selectstudno , courseid , studscore*2 from studscoreinfo; 10. 选择前 n 行的信息Select * from studscoreinfo; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - - - - Select top 10 * from studscoreinfo; 11. 去除重复行Select distinct * from studscoreinfo; 12. 更改列名显示selectstudnoas 学号 from studscoreinfo; 13. select 结果直接导入另外一个表Use database; Select * into stud1 from studinfo where studsex= 男 ; Insertintostud1 select* from studinfowhere studsex = 男 ; 14运算符use Select*from studscoreinfowhere studscoreBETWEEN 70 AND 80 ;15. 模糊查询Select* from studinfo where studnamelike_ 林%; 16. 排序Select* from studscoreinfoorder bystudscoreasc; Select* from studscoreinfoorder bystudscoredesc; Select* from studscoreinfo名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - orderby studscoredesc, studnodesc; 17 分组Selectstudno , AVG ( studscore )from studscoreinfogroup by studno ; Selectstudno , sum ( studscore ), avg( studscore ), min( studscore ), max ( studscore ) from studscoreinfogroup by studno ; 18 计数函数 Count()use zyj2011 ; SelectCOUNT (*)from studscoreinfowhere courseid = 英语 ; 19 连接selecta. studname,b.studscore from studinfo as a,studscoreinfo as bwherea. studno=b. studno and b. courseid= 思想 andb. studscore60; 1. 使用 T-SQL 编程输出A 到 Z 之间的26 个大写字母。declare i int set i =ascii( A) while i =ascii( Z) begin 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - printchar ( i) set i =i+1 end2. 使用 T-SQL 编程计算N!(即 N 的阶乘),测试5!(即设置初值N=5) 。declare i int, s int set i =1 set s =1 while i =5 begin set s=s * i set i =i+1 end print s3 使用 T-SQL 编程求s=1+2+3+100 declare i int, s int set i =1 set s =0 while i =100 begin set s=s +i set i =i+1 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 10 页 - - - - - - - - - end print s4. 使用 T-SQL 编程计算S=1+1/2+2/3+3/5+5/8+8/13+,计算前 20 项的和。declare i float, j float, l float, n float, s float set i =1 set j =1 set n=1 set s =0 while n=20 begin select i , j set s=s +i/ j set l =i set i =j set j =l+j set n=n +1 end print s 5. 使用T-SQL 编程计算S=1+(1+2)+(1+2+3)+(1+2+3+4)+ +(1+2+3+N),计算N=10 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - 时的和。declare i int, j int, s int set i =1 set s =0 while i =10 begin set j =1 while j 0 Then MsgBox 用户登陆成功 Form2.Show Unload Form1 End If End If cn.Close End Sub Private Sub Command2_Click() cn.ConnectionString = Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 10 页 - - - - - - - - - Security Info=False;Initial Catalog=zyj2011;Data Source=KKKSQL2005 cn.Open If Text1.Text = Or Text2.Text = Then MsgBox 用户名和密码不能为空 Else Dim rs As New ADODB.Recordset str1 = insert into us values( & Text1.Text & , & Text2.Text & ) rs.Open str1, cn, 1, 3 MsgBox 新用户注册成功 End If cn.Close End Sub (5) 在 form2 中添加以下控件:Label1: caption 为查询窗口Label2: caption 为输入姓氏text1: text 为空commondbutton1: caption为查询窗口adodc1: 连接配置如上,不过属性纪录源中命令文本填上:select * from studinfo; datagrid 控件 datasource属性为 ADODC1 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 10 页 - - - - - - - - - (6) 代码窗口编写如下:Public cn As New ADODB.Connection Private Sub Command1_Click() cn.ConnectionString = Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=zyj2011;Data Source=KKKSQL2005 cn.Open Dim rs As New ADODB.Recordset str1 = select * from studinfo where studname like & Text1.Text & % rs.Open str1, cn, 1, 3 If rs.EOF Then MsgBox 该信息不存在 Else Adodc1.RecordSource = str1 Adodc1.Refresh End If cn.Close End Sub 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 10 页 - - - - - - - - -