SqlServer编写数据库表的操作方式(建库、建表、修改语句)
学习要点:
SQL之-建库、建表、建约束、关系SQL基本语句大全.txt举得起放得下叫举重,举得起放不下叫负重。头要有勇气,抬头要有底气。学习要加,骄傲要减,机会要乘,懒惰要除。人生三难题:思,相思,单相思。
SQL之-建库、建表、建约束、关系、部分T-sql语句
---创建库创建库之前先进行查看数据库中是否已存在次数据库有便删除 ---ifexists(select*fromsys.sysdatabaseswherename='ConstructionDB')beginusemasterdropdatabaseConstructionDBendgocreatedatabaseConstructionDBon() ifexists(select*fromsysobjectswherename='ConstructionDB')--查找命令 dropDATABASEConstructionDB--删除命令 CreatedatabaseConstructionDB on( name='ConstructionDB_date', filename='E:\技能抽查试题第二模块(数据库)\试题——1\任务一\ConstructionDB_date.mdf', size=3mb, maxsize=10mb, filegrowth=5%--增长速度为 ) logon( name='ConstructionDB_log', filename='E:\技能抽查试题第二模块(数据库)\试题——1\任务一\ConstructionDB_date.ldf', size=2mb, maxsize=5mb, filegrowth=1mb ) --使用T-SQL语句创建表 useConstructionDB go ---查询库中是否存在此表存在则删除 ifexists(select*fromsysobjectswherename='T_flow_step_def') droptableT_flow_step_def ---方法二 IFOBJECT_ID(N'bas_CardType')ISNULL BEGIN--如果不存在该表,则进行创建 --droptablecom_CodeRecord --流程步骤定义表 createtableT_flow_step_def( Step_nointnotnull,--流程步骤ID Step_namevarchar(30)notnull,--流程步骤名称 Step_desvarchar(64)notnull,--流程步骤描述 Limit_timeintnotnull,--时限 URLvarchar(64)notnull,--二级菜单链接 备注varchar(256)notnull, ) ---流程类别表 createtableT_flow_type( Flow_type_idchar(3)notnull,--流程类别号 Flow_type_namevarchar(64)notnull,--流程类别名称 In_method_idchar(3)notnull,--招标方式代号 In_choice_idchar(3)notnull,--项目选项代号 备注varchar(256)notnull, ) ---标段情况表 createtableT_sub_project( Project_idvarchar(32)notnull,---工程编号 Sub_pro_idchar(2)notnull,--标段编号 Flow_type_idchar(3)notnull,--流程类别号 Sub_pro_namevarchar(64)notnull,--标段名称(招标项目名称) Usb_novarchar(64)notnull,--密码锁号 In_method_idchar(3)notnull,--招标方式代号 In_scope_idchar(3)notnull,--招标范围代号 In_choice_idchar(3)notnull,--项目选项代号 Proj_type_idchar(3)notnull,--项目性质代号 Engi_type_idchar(1)notnull,--工程性质代号 Pack_typechar(1)notnull,---发包方式 Grade_type_idvchar(1)notnull,--评分类别号 Flag_donechar(1)notnull,--完成标志 Flag_forcebreakchar(1)notnull,--强制中断标志 备注varchar(256)notnull, ) --创建一个数据库名为‘sql_test' createdatabasesql_test go --打开数据库sql_test usesql_test go --建立学生表 createtable学生 (学生编号char(4)primarykey,学生名字varchar(50)notnull) go --修改学生表 altertable学生 add班级编号char(4)null--添加班级编号字段 --(注意如果添加的字段不为空的话,是不能被添加的) go --建立班级表 createtable班级 (班级编号char(4)primarykey,班级名称varchar(50)notnull) go --建立课程表 createtable课程 (课程编号char(4)primarykey,课程名称varchar(50)notnull,开课日期datetime) go --修改课程表 altertable课程 add课程代号varchar(10)null--添加课程代号字段 go altertable课程 dropcolumn开课日期--删除开课日期字段 go altertable课程 altercolumn课程名称varchar(20)notnull--修改课程名称字段 go --建立一个product_test_one表,与下个表类似,只不过在constraint前面有个‘逗号'不影响执行 createtableproduct_test_one ( idchar(10)notnull,namevarchar(20)null,pricemoneydefault20.5,quantitysmallintnull,constraintpk_idprimarykeyclustered(id) ) go --建立一个product_test_two表 createtableproduct_test_two ( idchar(10)notnull,namevarchar(20)null,pricemoneydefault20.5,quantitysmallintnullconstraintpk_id2primarykeyclustered(id) ) go --删除表pruduct_test_one表 droptableproduct_test_one go --建立一个student表,使其中的name字段具有唯一性 createtablestudent ( idchar(8),namechar(10)--表字段 constraintpk_idprimarykey(id),--添加一个主键约束 constraintuk_nameunique(name)--添加一个唯一性约束 ) go --建立一个student4表,同上(注意:constraint与constraint之间一定要有逗号,否则出错!) createtablestudent4 ( idchar(8),namechar(10)--表字段 constraintpk_id4primarykey(id),constraintuk_name4unique(name) ) go --删除表student4 droptablestudent4 go --建立一个student3表,同上 createtablestudent3 ( idchar(8),namechar(10),--表字段 constraintpk_id3primarykey(id),constraintuk_name3unique(name) ) go --删除表student3 droptablestudent3 go --constraint约束名check(逻辑条件表达式) --创建一个‘员工‘表,使其输入的性别字段(sex)只能接受‘m'或则‘f',而不能接受其他数据 --并且为phone字段创建检查约束,限制只能输入类似0108564712之类的数据,而不能随意输入其他数据 createtable员工 ( idchar(5),namechar(20),sexchar(2),phoneint constraintpk_zidprimarykey(id),--此间一定要有‘逗号'分隔,定义主键约束 constraintchk_sexcheck(sexin(‘f‘,‘m‘)), constraintchk_phonecheck(phonelike‘(010)[0-9][0-9][0-9][0-9][0-9][0-9][0-9]‘) ) go --constraint约束名default约束表达式[for字段名] --创建一个表‘默认约束',为字段sex创建默认约束 createtable默认约束 ( idchar(5)primarykey,sexvarchar(2)constraintcon_sexdefault‘m‘ ) go --修改‘默认约束'表 altertable默认约束 addnamevarchar(10)nullconstraintcon_namedefault‘你好宝贝‘--增加一个字段为‘name',默认值为‘你好宝贝' go --往班级表里添加8条记录 insertinto班级values(‘bj01‘,‘一班‘) insertinto班级values(‘bj02‘,‘二班‘) insertinto班级values(‘bj03‘,‘三班‘) insertinto班级values(‘bj04‘,‘四班‘) insertinto班级values(‘bj05‘,‘五班‘) insertinto班级values(‘bj06‘,‘六班‘) insertinto班级values(‘bj07‘,‘七班‘) insertinto班级values(‘bj08‘,‘八班‘) go --显示班级所以记录 select*from班级 go --删除班级表里班级编号大于bj06的记录 deletefrom班级where班级编号>‘bj06‘ go --显示班级所以记录 select*from班级 go --向学生表里添加记录 insertinto学生values(‘xs01‘,‘one‘,‘bj01‘) insertinto学生values(‘xs02‘,‘two‘,‘bj01‘) insertinto学生values(‘xs03‘,‘three‘,‘bj01‘) insertinto学生values(‘xs04‘,‘four‘,‘bj02‘) insertinto学生values(‘xs05‘,‘five‘,‘bj03‘) insertinto学生values(‘xs06‘,‘six‘,‘bj02‘) insertinto学生values(‘xs07‘,‘seven‘,‘bj04‘) insertinto学生values(‘xs08‘,‘eight‘,‘bj03‘) insertinto学生values(‘xs09‘,‘nine‘,‘bj04‘) insertinto学生values(‘xs10‘,‘ten‘,‘bj05‘) insertinto学生values(‘xs11‘,‘eleven‘,‘bj06‘) insertinto学生values(‘xs12‘,‘twleve‘,‘bj06‘) go --显示学生所有的记录 select*from学生 go --连接查询 select*from学生,班级where学生.班级编号=班级.班级编号 go --以下效果同上一条相同 --选择的连接查询 select学生.学生编号,班级.班级编号,学生.学生名字,班级.班级名称from学生,班级where学生.班级编号=班级.班级编号 go --以下效果同上一条相同 --查询一班的学生 select*from学生where班级编号in(select班级编号from班级where班级编号=‘bj01‘) go --与上面一条查询语句一样功能 selecta.学生编号,a.学生名字,a.班级编号from学生asa,班级asbwherea.班级编号=b.班级编号andb.班级编号=‘bj01‘ go --统计一班学生人数 selectcount(学生编号)as学生统计from学生 where班级编号in(select班级编号from班级where班级编号=‘bj01‘) go --group的用法和count()函数的用法 --统计一班学生人数,并显示学生的名字和所在班级 selectcount(学生编号)as学生统计,学生名字,班级编号from学生 where班级编号in(select班级编号from班级where班级编号=‘bj01‘) groupby班级编号,学生名字 go
以上所述是小编给大家介绍的SqlServer编写数据库表的操作方式(建库、建表、修改语句),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!