随笔6-1
数据库;1、创建数据库;
use master
go
if db-id (表名)is null
create database 数据库名
on
(
name='逻辑名'
filename=‘文件名’
size=3mb,
filegrowth=1%
maxize
)
long on
(
name=//-log
filename=//
)
2/如果要创建一个新的数据库,但是当前实例中已经存在了,要判断
if db-id(’数据库名‘) is null
create database 数据名
3、建表;
create table student
(
id int
name nvarchar(10)
age int
gender bit
)
4、判断建表是否存在;
if object-id('表名','u') is not null
drop table 表名
if(表存在)
(
删除表
)
5、删除;
--->删除数据库
drop database 数据库名
----》删除表;
drop table 表名
——》删除表中数据
delete form 表名 where 条件
注解:删除;
(1)删除表中全部数据;delete from 表名
-》只删除数据,如果不加where条件表明删除所有数据,delete只是删除数据,表还在,或drop,table不同。
delete也可以带where子句来删除一部分数据;
delete from 表名 where 条件;
例子;delete from test where fname='王五'
6、查询(重点);
select * from 表名
7、怎样将主键清零;
truncate table 表名。(truncate是将表中的数据全部删除)
如果想主键清零,从头来过则使用truncate;
-truncate table 表名
-truncate清空表中的数据,不能加where子句;
-truncate删除不计入日志,慎用。
8、查看数据表结构;
->select*from 表名
9、插入数据;
(1)插入全部数据;
-》insert into 表名(列名)values(值),在值的赋值过程中要用单引号。
-》bit类型只能赋值0与1.
(2)-部分插入;只给部分字段赋值的插入;
语法 ;intsert into 表名(对应字段列表)values(值列表)
例子;intsert innto test
(fname,fage,fgender)
values
('赵六',18,’true‘)
go
10、数据的修改(updata);
(1)修改单个值;
updata 要修改的表名 set 字段名=值 where 条件
例子;updata test set fage=17 where fid=22(意思是删除id号为22的,将其中的年龄改为17岁)。
(2)修改追加;(字段+值)
updata test set fname+='风' where fid=22
4、删除数据库的语法;
-》删除数据时要注意找到自己的数据库,使用use 加数据名 go
-》然后使用 drop database 数据名 go
11、在数据库中的语法;
在有许多where条件一起使用时,用and连接每个where条件之间的关系。
优先级;not->and ->or 意思是首先考虑not,再考虑and,最后考虑or。
不等于的表示符 <> 或 !=
delect from test where fage!=22
12、备份表数据;
-语法;select * into 新表名 from test
一句代码结束后用分号结束,一段代码结束后用go。
--------------------编程问答-------------------- --------------------编程问答--------------------
补充:.NET技术 , 其他语言