SQL语句(建库、建表、修改语句)

 这是如何使用SQL server来  编写 数据库  表的 操作方式 

学习要点:

  SQL之-建库、建表、建约束、关系SQL基本语句大全.txt举得起放得下叫举重,举得起放不下叫负重。头要有勇气,抬头要有底气。学习要加,骄傲要减,机会要乘,懒惰要除。人生三难题:思,相思,单相思。
SQL之-建库、建表、建约束、关系、部分T-sql语句


---创建库 创建库之前 先进行 查看数据库中是否 已存在 次数据库 有便删除 

--- if exists(select * from sys.sysdatabases where name='ConstructionDB')begin use master drop database ConstructionDB end go create database ConstructionDB on()

if exists(select * from sysobjects where name ='ConstructionDB') --查找命令
drop DATABASE ConstructionDB --删除 命令

Create database ConstructionDB
on(
name='ConstructionDB_date',
filename='E:技能抽查试题第二模块(数据库)试题——1任务一ConstructionDB_date.mdf',
size=3mb,
maxsize=10mb,
filegrowth=5% --增长速度为
)
log on(
name='ConstructionDB_log',
filename='E:技能抽查试题第二模块(数据库)试题——1任务一ConstructionDB_date.ldf',
size=2mb,
maxsize=5mb,
filegrowth=1mb
)
--使用T-SQL语句创建表
use ConstructionDB
go
---查询 库中是否存在 此表 存在则删除
if exists(select * from sysobjects where name = 'T_flow_step_def') 
drop table T_flow_step_def
--- 方法二
IF OBJECT_ID (N'bas_CardType') IS NULL
BEGIN --如果不存在该表,则进行创建
--drop table com_CodeRecord

--流程步骤定义表 
create table T_flow_step_def(
Step_no    int not null,     --流程步骤ID 
Step_name    varchar(30)    not null, --流程步骤名称 
Step_des    varchar(64)    not null,    --流程步骤描述
Limit_time    int not null,     --时限
URL     varchar(64)    not null,     --二级菜单链接 
备注    varchar(256)    not null, 
)

---流程类别表
create table T_flow_type(
Flow_type_id char(3) not null, --流程类别号 
Flow_type_name    varchar(64)    not null, --流程类别名称 
In_method_id    char(3) not null, --招标方式代号 
In_choice_id    char(3) not null, --项目选项代号 
备注    varchar(256)    not null, 
)
---标段情况表
create table T_sub_project(
Project_id varchar(32)    not null, ---工程编号 
Sub_pro_id char(2) not null, -- 标段编号 
Flow_type_id char(3) not null, --流程类别号 
Sub_pro_name varchar(64)    not null,--标段名称(招标项目名称) 
Usb_no varchar(64)    not null, --密码锁号
In_method_id char(3) not null, --招标方式代号 
In_scope_id char(3) not null, --招标范围代号 
In_choice_id char(3) not null, --项目选项代号 
Proj_type_id char(3) not null, --项目性质代号 
Engi_type_id char(1) not null, --工程性质代号
Pack_type char(1) not null, ---发包方式 
Grade_type_idv char(1) not null,--评分类别号
Flag_done char(1) not null,--完成标志 
Flag_forcebreak char(1) not null,--强制中断标志 
备注    varchar(256)    not null,
)
 

--创建一个数据库名为‘sql_test’

  1 create database sql_test
  2 go 
  3 --打开数据库 sql_test
  4 use sql_test
  5 go
  6 
  7 --建立学生表
  8 create table 学生
  9 (学生编号 char(4) primary key, 学生名字 varchar(50)not null)
 10 go
 11 
 12 --修改学生表
 13 alter table 学生 
 14 add 班级编号 char(4) null --添加班级编号字段
 15 -- (注意如果添加的字段不为空的话,是不能被添加的)
 16 go
 17 
 18 --建立班级表
 19 create table 班级
 20 (班级编号 char(4) primary key ,班级名称 varchar(50)not null)
 21 go
 22 
 23 --建立课程表
 24 create table 课程
 25 (课程编号 char(4) primary key ,课程名称 varchar(50) not null,开课日期 datetime )
 26 go
 27 
 28 --修改课程表
 29 alter table 课程
 30 add 课程代号 varchar(10) null --添加课程代号字段
 31 go
 32 
 33 alter table 课程
 34 drop column 开课日期  --删除开课日期字段
 35 go
 36 
 37 alter table 课程
 38 alter column  课程名称 varchar(20) not null   --修改课程名称字段
 39 go
 40 
 41 --建立一个product_test_one 表,与下个表类似,只不过在constraint前面有个‘逗号’不影响执行
 42 create table product_test_one
 43 (
 44 id char(10) not null, name varchar(20) null, price money default 20.5,quantity smallint null, constraint pk_id primary key clustered (id)
 45  )
 46 go
 47 
 48 
 49 --建立一个product_test_two 表
 50 
 51 create table product_test_two
 52 (
 53 id char(10) not null, name varchar(20) null, price money default 20.5,quantity smallint null constraint pk_id2 primary key clustered (id)
 54  )
 55 go
 56 
 57 --删除表 pruduct_test_one表
 58 drop table product_test_one
 59 go
 60 
 61 --建立一个student表,使其中的 name 字段具有唯一性
 62 create table student 
 63 (
 64 id char(8), name char(10) --表字段
 65 constraint pk_id primary key (id), --添加一个主键约束 
 66 constraint uk_name unique (name) --添加一个唯一性约束
 67 )
 68 go
 69 
 70 --建立一个student4表,同上 (注意:constraint 与constraint 之间一定要有逗号,否则出错!)
 71 create table student4 
 72 (
 73 id char(8), name char(10) --表字段
 74 constraint pk_id4 primary key (id), constraint uk_name4 unique (name)
 75 )
 76 go
 77 -- 删除表student4
 78 drop table student4
 79 go
 80 
 81 --建立一个student3表,同上
 82 create table student3
 83 (
 84 id char(8), name char(10), --表字段
 85 constraint pk_id3 primary key (id) ,constraint uk_name3 unique (name)
 86 )
 87 go
 88 
 89 --删除表student3
 90 drop table student3
 91 go
 92 
 93 
 94 --constraint 约束名 check(逻辑条件表达式)
 95 
 96 --创建一个‘员工‘表,使其输入的性别字段(sex)只能接受‘m’或则‘f’,而不能接受其他数据
 97 --并且为phone字段创建检查约束,限制只能输入类似0108564712之类的数据,而不能随意输入其他数据
 98 create table 员工
 99 (
100 id char(5),name char(20),sex char(2),phone int
101 constraint pk_zid primary key (id),      --此间一定要有‘逗号’分隔 ,定义主键约束
102 constraint chk_sex check (sex in (‘f‘,‘m‘) ),
103 constraint chk_phone check (phone like ‘(010) [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]‘)
104 )
105 go
106 
107 
108 --constraint 约束名 default 约束表达式 [for 字段名]
109 
110 -- 创建一个表‘默认约束’,为字段sex创建默认约束
111 create table 默认约束
112 (
113 id char(5) primary key ,sex varchar(2) constraint con_sex default ‘m‘ 
114 )
115 go
116 
117 --修改‘默认约束’表
118 alter table 默认约束
119 add name varchar(10)null constraint con_name default ‘你好宝贝‘ --增加一个字段为‘name’,默认值为‘你好宝贝’
120 go
121 
122 --往班级表里添加8条记录
123 insert into 班级 values(‘bj01‘,‘一班‘)
124 insert into 班级 values(‘bj02‘,‘二班‘)
125 insert into 班级 values(‘bj03‘,‘三班‘)
126 insert into 班级 values(‘bj04‘,‘四班‘)
127 insert into 班级 values(‘bj05‘,‘五班‘)
128 insert into 班级 values(‘bj06‘,‘六班‘)
129 insert into 班级 values(‘bj07‘,‘七班‘)
130 insert into 班级 values(‘bj08‘,‘八班‘)
131 go
132 --显示班级所以记录
133 select * from 班级
134 go
135 --删除班级表里班级编号大于bj06的记录
136 delete from 班级 where 班级编号>‘bj06‘
137 go
138 --显示班级所以记录
139 select * from 班级
140 go
141 
142 --向学生表里添加记录
143 insert into 学生 values(‘xs01‘,‘one‘,‘bj01‘)
144 insert into 学生 values(‘xs02‘,‘two‘,‘bj01‘)
145 insert into 学生 values(‘xs03‘,‘three‘,‘bj01‘)
146 insert into 学生 values(‘xs04‘,‘four‘,‘bj02‘)
147 insert into 学生 values(‘xs05‘,‘five‘,‘bj03‘)
148 insert into 学生 values(‘xs06‘,‘six‘,‘bj02‘)
149 insert into 学生 values(‘xs07‘,‘seven‘,‘bj04‘)
150 insert into 学生 values(‘xs08‘,‘eight‘,‘bj03‘)
151 insert into 学生 values(‘xs09‘,‘nine‘,‘bj04‘)
152 insert into 学生 values(‘xs10‘,‘ten‘,‘bj05‘)
153 insert into 学生 values(‘xs11‘,‘eleven‘,‘bj06‘)
154 insert into 学生 values(‘xs12‘,‘twleve‘,‘bj06‘)
155 go
156 --显示学生所有的记录
157 select * from 学生
158 go
159 
160 --连接查询
161 select * from 学生,班级 where 学生.班级编号=班级.班级编号
162 go
163 
164 --以下效果同上一条相同
165 
166 --选择的连接查询
167 select 学生.学生编号,班级.班级编号, 学生.学生名字,班级.班级名称 from 学生,班级 where 学生.班级编号=班级.班级编号
168 go
169 --以下效果同上一条相同
170 
171 
172 
173 --查询一班的学生
174 select* from 学生 where 班级编号 in(select 班级编号 from 班级 where 班级编号=‘bj01‘)
175 go
176 --与上面一条查询语句一样功能
177 select a.学生编号,a.学生名字,a.班级编号 from 学生 as a ,班级 as b where a.班级编号=b.班级编号 and b.班级编号=‘bj01‘
178 go
179 
180 --统计一班学生人数
181 select count(学生编号)as 学生统计 from 学生 
182 where 班级编号 in(select 班级编号 from 班级 where 班级编号=‘bj01‘)
183 go
184 
185 --group的用法和count()函数的用法
186 
187 
188 --统计一班学生人数,并显示学生的名字和所在班级
189 select count(学生编号)as 学生统计, 学生名字,班级编号 from 学生 
190 where 班级编号 in(select 班级编号 from 班级 where 班级编号=‘bj01‘)
191 group by 班级编号,学生名字
192 go
原文地址:https://www.cnblogs.com/yangliping/p/5884162.html