数据库使用的方法 一.DDL建库create database 库名删库drop database库名建表create table 表名字段名 数据类型字段名 数据类型li:create table student( id int, name varchar(50), sal decimal(5,2), lasttime datetime );int 整数 decimal小数 char 字符verchar字符串 datetime时间日历删除表 drop table 表名例drop table student修改表名例exec sp_renamestudent,stu;修改表中字段名例exec sp_renamestudent.lasttime,time,column;删除表中的字段例alter table student drop colum time;向表中添加字段信息例alter table student add time datetime;二.DML添加语句 全表添加insert into 表名 values(1,23.45,‘’,‘2026-07-07 15:20:34’);例insert into student values(1,abc,23.45,2026-07-07 15:20:3);添加可以指定只添加在哪部分的内容insert into student(name,sal) values(‘a’,56);查询 全表查询 查询所有的字段信息select * from student;查询想要的字段信息select name from student;where 查询 根据指定条件来进行查询 ! select*from student where id1;修改语句 update 表名 set 字段名修改的内容 where 条件update student set sal60 where id1;删除 delete from 表名 where 条件delete from student where name‘aa’;–and同时满足 or满足其中之一select*from emp where deptno10 and sal1000;select ename,deptno,sal from emp where deptno10 or sal2000;select ename,sal from emp where sal1300and sal3000;–在一个区间内 between andselect ename,sal,deptno from emp where sal between 1300 and 3000 and deptno10;–查询没有提成的员工 is null 不为空is not nullselect ename,comm from emp where comm is not null;–查询编号为7369 7499 7521 in包含select ename,empno from emp where empno in (7369,7499,7521);–模糊查询 like–查询名字中包含a的员工编号和名字– % 代表0个或者多个字符 _下划线 一个下划线就代表一个字符select empno,ename from emp where ename like’%a%‘;–查询名字中第二个字符是A的员工编号和名字select empno,ename from emp where ename like’%_A%;–别名select empno as ‘员工编号’ from emp where empno7900;–去重select distinct(job)from emp;–排序 order by 默认为升序 如果降序 在后面加descselect empno,sal from emp order by sal desc;– 按照部门进行升序 如果部门相同 那么按照工资降序select empno,sal,deptno from emp order by deptno ,sal desc;–查询20号部门的员工编号 工资 要求按照从高到低显示出来select deptno,empno,sal from emp where deptno20 and sal2000 order by sal desc;– top 简单分页– 只能查询前面的几条数据 从0开始查询– 查询最高工资的人的信息select top 1 *from emporder by sal desc;– 约束 主键 唯一 非空 检查– 外键约束insert into emp(deptno) values(50);select *from emp;– 对deptno设置外键约束 只能是有的部门你才能添加– 主表 主表里面一定有一个主键create table zhu(id int primary key);–constraint 约束名 yu1 foreign key 本表的字段 references 主表字段create table fu(id int ,kid int,constraint yu1 foreign key (kid) references zhu(id));insert into zhu values(2);insert into fu values(2,1);–redis 缓存 Datatable 离线数据表格–索引 提高查询效率 一般是给字段设置索引–二分 优点查询效率快–缺点 占数据库内存–如何优化查询速度 where 不要全表查询 创建索引 创建视图–分页select*from emporder by sal descoffset 0 rowsfetch next 1 rows only;–聚合函数 max最大 min最小 avg平均值 sum求和 count统计–group by 分组 一般和聚合函数–求每个部门的平均工资select deptno,avg(sal) as a from emp group by deptno;–主键约束非空和唯一 非空 唯一 检查 外键–主键自增 IDENTITY(i,j) i初始值 j步长–not null 非空 identity(1,1)主键自增 primary key 主键–unique 唯一 check检查约束create table hh(id int identity(1,1) primary key,name varchar(20) unique,sex int check(sex0 or sex1))–子查询 把查询出来的结果当作条件 继续查询–1.查询最高工资的员工名字–查询最高工资是多少select max(sal) from emp;select ename,sal from emp where sal(select max(sal) from emp);–视图 伪表 只能用来查询操作 简化复杂的查询create view tasselect,sal1212*ISNULL(comm,0) as nxfrom emp where ename!‘king’;–查询所有员工信息 emp 和对应的部门地点 dept–等值连接 内连接select e.,d.from emp e,dept dwhere e.deptnod.deptno;–外连接 左外 已谁为主表 右外–如果主表存在 副表不存在 那么补null– join onselect e.,d.from emp eright join dept don e.deptnod.deptno;–自连接 一张表的连接select e.empno,e.mgr,d.empnofrom emp e,emp dwhere e.mgrd.empno;–查询 year 函数 month dayselect day (hiredate),hiredate from emp;public void test(){try{SqlConnection SqlConnection new SqlConnection();//连接字符串赋值 属性 构造器赋值 类初始化器//服务器地址 数据库 用户名 密码SqlConnection.ConnectionString “serverlocalhost;databasehm0707;uidsa;pwd1234”;SqlConnection.Open();Console.WriteLine(SqlConnection.State.ToString());//关闭字符串SqlConnection.Dispose();}catch (Exception ex){Console.WriteLine(ex.Message);}}string connStr “serverlocalhost;databasehm0707;uidsa;pwd1234”;public void test1(){try{SqlConnection SqlConnection new SqlConnection(connStr);//打开连接SqlConnection.Open();//创建你要执行的sql语句string sql “create table animal(id int,name nvarchar(30))”;//创建执行sql语句的对象SqlCommand sqlCommandnew SqlCommand (sql,SqlConnection);//执行sql语句 返回结果int i sqlCommand.ExecuteNonQuery();Console.WriteLine( i);//关闭连接对象SqlConnection.Close();sqlCommand.Dispose();} catch (Exception ex) { Console.WriteLine( ex.Message); }}public void test2(){try{using (SqlConnection sqlconnnew SqlConnection(connStr)){sqlconn.Open();string sql “insert into animal values(1,‘郭圣杰’)”;using (SqlCommand comm new SqlCommand(sql, sqlconn)){int icomm.ExecuteNonQuery();Console.WriteLine( i);}}}catch (Exception ex){Console.WriteLine( ex.Message );}}public void test3(){try{using (SqlConnection sqlconn new SqlConnection(connStr)){sqlconn.Open ();string sql “select * from animal”;using(SqlCommand comm new SqlCommand(sql, sqlconn)){SqlDataReader sr comm.ExecuteReader();while (sr.Read()){//索引方式取出元素Console.WriteLine(sr[0] “,” sr[1]);Console.WriteLine(sr[“id”] “,” sr[“name”]);Console.WriteLine( sr.GetInt32(0)“,”sr.GetString(1));}} } } catch (Exception ex) { Console.WriteLine( ex.Message); }string sql $“insert into student $“values(name,age,sex,school,zy)”;using (SqlCommand comm new SqlCommand(sql, conn)){comm.Parameters.AddWithValue(”name, s.StuName);comm.Parameters.AddWithValue(“age”, s.Age);comm.Parameters.AddWithValue(“sex”, s.Sex);comm.Parameters.AddWithValue(“school”, s.School);comm.Parameters.AddWithValue(“zy”, s.zy);return comm.ExecuteNonQuery();}