sqlserver
发布时间:2020-12-12 14:37:37 所属栏目:MsSql教程 来源:网络整理
导读:一、T-SQL语句回顾 1、添加数据 insert [into] 表名(字段1,字段2,....)values(值1,值2,....) 2、修改数据 update 表名 set 字段1=值1,字段2=值2,.....where (条件) 3、查询数据 select 字段1,字段2,.....from 表名 where 条件 order by字段名
一、T-SQL语句回顾 1、添加数据 insert [into] 表名(字段1,字段2,....)values(值1,值2,....) 2、修改数据 update 表名 set 字段1=值1,字段2=值2,.....where (条件) 3、查询数据 select 字段1,字段2,.....from 表名 where 条件 order by字段名 4、删除数据 delete from 表名 where条件 5、数据库文件 主数据文件:.mdf 次数据文件:.ndf 日记文件: .ldf 二、创建数据库 语法: create database 数据库名 on [primary] ( --数据文件参数--- name=逻辑文件名 filename=物理文件名 size=大小 maxsize=最大容量 filegrowth增长量 ) [log on] ( 日记文件参数 ) 示例: create database stuDB on primary --默认就属于primary主文件组,可省略 ( /*--数据文件的具体描述--*/ name = 'stuDB_data',--主数据文件的逻辑名称 filename='D:projectstuDB_data.mdf',--主数据文件的物理名称 size=5mb,--主数据文件的初始大小 maxsize=100mb,--主数据文件增长的最大值 filegrowth=15% --主数据文件的增长率 ) log on ( /*--日记文件的具体描述,各参数同上--*/ name = 'stuDB_log',filename='D:projectstuDB_log.ldf',size=2mb,filegrowth=1mb ) 三、删除数据库 语法: drop database 数据库名 完整创建数据库示例: use master --设置当前数据库为Master,以便访问sysdatabases表 go if exists (select * from sysdatabases where name ='stuDB') drop database stuDB create database stuDB on( .......... ) log on ( .......... ) 四、使用SQL语句创建和删除表 1、sqlserver中的数据类型 整形:int 、smallint、tinyint 浮点型: numeric、real、float、decimal 字符型:char、varchar、text Unicode型:nchar、nvarchar、ntext 是/否型:bit(其值为0/1) 二进制型:binary、varbinary、image 货币型:money 、smallmoney 日期时间型: datetime(1753-1-1~~9999-12-31)、smalldatetime (1900-1-1~2079-6-6) 特殊类型:timestamp、uniqueidentifer 2、创建表 语法: create table 表名 ( 字段1 数据类型 列的特征 字段2 数据类型 列的特征 ..................... ) 示例: use stuDB go if exists(select * from sysobjects where name='stuinfo') drop table stuinfo create table stuinfo ( stuname varchar(20) not null,--非空 stuno char(6) not null,stuAge int not null,stuid numeric(18,0),stuseat smallint identity(1,1),--自动编号 stuaddress text ) 3、删除表 语法: drop table 表名 五、使用SQL语句创建和删除约束 1、常用的数据类型 主键约束(Primary Key constraint):要求主键列数据唯一,并且不允许为空 唯一约束(Unique Constraint):要求该列唯一,允许为空,但只能出现一个空值 检查约束(Check Constraint):某列取值范围限制、格式限制等 默认约束(Default Constraint):某列默认值 外键约束(Foreign Key Constraint):用于在两表之间建立关系,需要指定引用主键的哪一列 2、添加约束 语法: alter table 表名 add Constraint 约束名 约束类型 具体的约束说明 约束名的命名规则推荐采用“约束类型_约束字段”的表示形式: 主键:PK_ 唯一:UQ_ 默认:DF_ 检查:CK_ 外键:FK_ 示例: --添加主键约束-- alter table stuinfo add Constraint PK_stuNo primary key (stuNo) --添加唯一约束-- alter table stuinfo add Constraint UQ_stuID unique (stuID) --添加默认约束-- alter table stuinfo add Constraint DF_stuAddress Default(’地址不详‘) for stuAddress --添加检查约束,要求年龄只能在15~40岁之间-- alter table stuinfo add Constraint CK_stuAge check(stuAge between 15 and 40) --添加外键约束(主表stuinfo 和从表stuMarks 建立关系,关键字段为stuNo) alter table stuMarks add Constraint Fk_stuNo foreign key(stuNo)references stuinfo(stuNo) 3、删除约束 语法: alter table 表名 drop constraint 约束名 示例: ---删除默认约束-- alter table stuInfo drop constraint DF_stuAddress (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- SQL Server一个过滤重复数据的 SQL 语句
- SQL Server 2008上的批量删除(有删除数据的批量复制(bcp)吗
- sqlserver 更改默认schema DBO
- [SQLServer 2000 Driver for JDBC][SQLServer]传入的表格格
- sqlserver数据类型总结
- sqlserver中datetime和timestamp之间的区别?
- sqlserver alter 语句
- sql-server – SQL Server查询LEFT JOIN,SUM和GROUP BY和我
- sql-server – TSQL:Try-Catch Transaction in Trigger
- sql里将重复行数据合并为一行数据使用逗号进行分隔