当前位置:   article > 正文

SQL——表的操作

SQL——表的操作
  1. --1.创建数据库
  2. --create database studentDB
  3. --2.创建登陆名,并映射为数据库用户
  4. /*
  5. use studentDB
  6. go
  7. create login zhou1 with password = 'mypassword123'
  8. go
  9. create user zhou1 for login zhou1
  10. */
  11. --3.创建模式和删除模式
  12. --create schema sch1 authorization zhou1
  13. --drop schema sch1
  14. --4.创建表
  15. /*
  16. --4.1创建用户表
  17. create table uers
  18. (
  19. vusername varchar(18) not null,
  20. vpasswrod varchar(20) not null
  21. )
  22. */
  23. /*
  24. --4.2创建学生表
  25. create table student
  26. (
  27. Sno char(10) NOT NULL Constraint PK_Stu_No Primary Key,
  28. Sname char(20) NOT NULL,
  29. Ssex char(2) NOT NULL Constraint CK_Stu_Sex check(Ssex in ('男','女')),
  30. Smajor char(20) NOT NULL,
  31. Sdept char(20) NOT NULL,
  32. Sage tinyint Constraint CK_Stu_Age Check(Sage between 1 and 80),
  33. Tel char(15) NOT NULL,
  34. EMAIL varchar(30) NOT NULL
  35. )
  36. */
  37. --5.表|列的删除,添加,修改
  38. /*
  39. --删除约束
  40. alter table student drop constraint CK_Stu_Age
  41. --删除列Sage
  42. alter table student drop column Sage
  43. --添加列dBirth为时间格式
  44. alter table student add dBirth datetime
  45. --修改列
  46. alter table student alter column Smajor varchar(20)
  47. --删除表
  48. drop table student
  49. */
  50. --6.检索表
  51. /*
  52. --显示所有信息
  53. --select * from student
  54. --显示部分信息
  55. select Sno,Sname,Ssex,Smajor from student
  56. --友好列标题
  57. select Sno as 学号,Sname as 姓名,Ssex as 性别,Smajor as 专业,Sage as 年龄 from student
  58. --检索前三行信息
  59. select top 3 *from student
  60. --条件检索学生
  61. select *from student where sage=12
  62. select *from student where Ssex='男' and Sage<21
  63. select *from student where Ssex='男' or Sage>60
  64. select Sno,Sname,Ssex,Sage from student where Sage between 45 and 65
  65. select Sno,Sname,Ssex,Sage from student where Sage not between 45 and 65
  66. select * from student where Sage in(44,65,12)
  67. --distintc 不重复信息
  68. select distinct Sage from student
  69. --like模糊查询,前缀为1
  70. select * from student where Sage like '1%'
  71. --like模糊查询,只要含有2
  72. select * from student where Sname like '%2%'
  73. --like模糊查询,_表示任意字符,前面必须为两个任意字符后面跟着2
  74. select * from student where Sname like '__2%'
  75. select *from student where Sage is NULL
  76. select *from student where sage is not null
  77. --升序排列
  78. select *from student order by Sage asc
  79. --降序排列
  80. select *from student order by Sage desc
  81. --按Sage升,Tel降
  82. select *from student order by Sage asc,Tel desc
  83. select 3*5,sqrt(2)
  84. select *,Tel*10 as Tel2 from student
  85. --计算Sage=12的数目
  86. select count(*) from student where Sage=12
  87. --计算Tel的列数
  88. select count(Tel) from student
  89. select max(Sage),min(Sage),avg(Sage) from student
  90. select sum(Sage) from student
  91. --group分组
  92. select Sage,count(Sage) as 个数 from student group by Sage
  93. select Sage,Ssex,count(Ssex) from student group by Sage,Ssex order by count(Ssex) asc
  94. --大于三才显示
  95. select Ssex,count(Ssex) from student group by Ssex having count(Ssex)>3

 

  1. --多表查询,(接上单表查询的表)
  2. drop table sc
  3. create table sc
  4. (
  5. Sno char(10) primary key,
  6. Sname char(30),
  7. Ssex char(2)
  8. )
  9. --内连接,显示两个表的数据,当Sno相等时
  10. --写法一
  11. select student.Sno,student.Sname,sc.Sno,sc.Sname from sc,student
  12. where sc.Sno=student.Sno
  13. --写法二
  14. on sc.Sno=student.Sno
  15. select student.Sno,student.Sname,sc.Sno,sc.Sname from sc join student
  16. --左外连接,左边的表的集合与右边的表的交集
  17. select student.Sno,student.Sname,sc.Sno,sc.Sname from student left outer join sc
  18. on student.Sno=sc.Sno
  19. --右外连接,同上
  20. select student.Sno,student.Sname,sc.Sno,sc.Sname from sc right outer join student
  21. on student.Sno=sc.Sno
  22. --全外连接,两个之和
  23. select student.Sno,student.Sage,sc.Sno,sc.Sname from sc full outer join student
  24. on student.Sno=sc.Sno
  25. --交叉连接,两个表的排列组合
  26. select *from student cross join sc
  1. --集合查询
  2. --union 去重,union all 不去重
  3. use studentDB
  4. select Sno,Sname,Ssex from sc
  5. union
  6. select Sno,Sname,Ssex from student
  7. --intersect 交集
  8. select Sno,Sname,Ssex from student where Ssex='男'
  9. intersect
  10. select Sno,Sname,Ssex from student where Sno=1
  11. --except 差集
  12. select Sno,Sname,Ssex from student where Ssex='男'
  13. except
  14. select Sno,Sname,Ssex from student where Sno=1
  15. --数据更新
  16. insert into sc (Sno,Ssex,Sname) values ('21','21','43')
  17. --创建新表
  18. create table fsc
  19. (
  20. Sno char(12) not null primary key,
  21. Sname char(20) not null,
  22. Ssex char(2),
  23. new char(10)
  24. )
  25. --把子查询结果插入到新表
  26. insert into fsc(Sno,Sname,Ssex,new) select Sno,Sname,Ssex,'new' from sc
  27. --选择数据,插入到一个不存在的新表
  28. select * into wtf from student where Ssex='男'
  29. 数据更新
  30. update wtf set Ssex='女' where Tel=12
  31. update sc set Sname=(Select Sname from student where student.Sno=sc.Sno)
  32. --删除数据
  33. delete from wtf where Sno='12'
  34. delete from wtf where Sno in (select Sno from sc)
  35. --建立视图
  36. create view see
  37. as
  38. select * from sc
  39. --通过视图修改数据
  40. update see set Sname='22'
  41. --视图的as前加with encryption 可以给视图加密
  42. create view see
  43. with encryption as
  44. select * from sc
  45. --创建唯一索引
  46. create unique index only on wtf(Sname)
  47. --删除索引
  48. drop index wtf.only
  49. drop index only on wtf

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/671553
推荐阅读
相关标签
  

闽ICP备14008679号