SQLserver基本语句使用

--数据库定义()
--创建数据库
--create database student
--使用数据库
use student 
--删除数据库
--drop database student1
--创建模式
--create schema s_t
--删除模式
--drop schema s_t 
--创建基本表
--创建学生表student
create table student
(
  sno char(9) primary key,
  sname char(9) not null,
  ssex char(5) ,
  sage int ,
  sdept varchar(20)
 );
 --创建课程表
 create table course
 (
   cno char(4) primary key,
   cname char(20),
   cpno char(4),--参照关系,数据类型一样,长度一样
   ccredit smallint,
   foreign key(cpno) references course(cno)
 );
 --创建学生选课表
 create table sc
 (
  sno char(9) ,
  cno char(4),
  grade int ,
  primary key(sno,cno),
  foreign key (sno) references student(sno),
  foreign key (cno) references course(cno),
 )
 --删除基本表
 drop table student 
 drop table course 
 drop table sc
 create table test(
  sno char(20),
 )
 --修改基本表
 use student
 alter table student 
 add s_entrance datetime
 --删除列
 alter table student
 drop column s_entrance
 --修改列
 alter table student
 alter column ssex char(2)
 --增加唯一性约束
 alter table student
 add unique(sname)
 --增加非空约束
 alter table student
 alter column ssex char(2) not null;--假如设计的时候有非空,再加就加不上了
 --建立索引
 --create index stusno on student(sno asc)
 --create unique index coucno on course(cno desc)
 --create clustered index scsno on sc(sno desc)
 --drop index.sc 
 --drop index sc.scsno
 --单表查询
 --投影查询
 --
 select sno,sname,sdept
 from student 
 --查询所有学生的基本情况
 select sno,sname,sage,sdept,ssex
 select *
 from student 
 --查询所有学生的姓名和出生年份
 select sname 姓名,2020-sage 出生年份
 from student 
--查询所有学生的学号、姓名、以及入学年份
select sno 学号,sname,'2018' 入学年份
from student 
select sname 姓名,'YEAR OF BIRTH:' 生日,2020-sage 出生年份,lower(sdept)
from student
--查询在student 表中一共有多少名学生。
select count(sno) 学生数
from student 
--查询SC表中多少同学选了课
select count( distinct sno)
from sc 
--查询Sc表中所有课程号和成绩
select distinct cno,grade
from sc

--查询所有小于20岁的学生的学号姓名及年龄
select sno,sname,sage
from student
where sage<20
drop database student
create database student
use student 
create table Student
(
 Sno char(9) primary key,
 Sname char(20) unique,
 Ssex char(2),
 Sage smallint,
 Sdept char(20)
)
create table Course
(
 Cno char(4) primary key,
 Cname char(40) not null,
 Cpno char(4),
 Ccredit smallint,
 foreign key (Cpno)references Course(Cno)
)

create table SC
(
  Sno char(9),
  Cno char(4),
  Grade smallint,
  primary key(Sno,Cno),
  foreign key(Sno)references Student(Sno),
  foreign key(Cno)references Course(Cno)
)
create database SPJ
use SPJ 
create table S
(
  SNO char(4) primary key,
  SNAME char(20),
  STATUS int,
  CITY char(20),
)
create table P
(
   PNO char(4) primary key,
   PName char(20),
   COLOR char(4),
   WEIGHT int,
)
create table J
(
  JNO char(4) primary key,
  JNAME char(20),
  CITY char(20),
)
create table SPJ
(
  SNO char(4),
  PNO char(4),
  JNO char(4),
  QTY int,
  primary key(SNO,PNO,JNO),
  foreign key(SNO)references S(SNO),
  foreign key(PNO)references P(PNO),
  foreign key (JNO)references J(JNO)
)
发布了53 篇原创文章 · 获赞 27 · 访问量 4373

猜你喜欢

转载自blog.csdn.net/qq_44867340/article/details/104939850