--Nchar, nvarchar, ntext... etc. with n contain Chinese, and one character occupies 2 digits (regardless of whether it is Chinese or not)
- char, varchar, text... etc. without n are not Contains Chinese, and one character occupies 1 character
- create database
create database student
- delete database
drop database student
- create table (in student database)
use student
create table test1(
testId int primary key,
#primary
key designation testName varchar(40))
- delete table data (not including table structure)
delete from test1
- delete table (including table structure)
drop table test1
- query
select * from test1
select testId from test1
- add
insert into test1 values( 12,'fasdf')
insert into test1(testId) values(11)
- delete row data
delete from test1 where testId = 11
- modify row data
UPDATE table name SET column name = new value WHERE column name = some value
update test1 set testName = '1111' where testId = 12
- can modify the primary key
-Set the syntax of foreign keys
foreign key references
goodsId nvarchar(50) foreign key referencesgoods(goodsId),
--其他样例, 表结构就没有写了
--显示高于部门平均工资的员工的信息
select avg(sal),deptno from emp group by deptno havingsal>avg(sal) ????(未检验此语句)
--显示高于部门平均工资的员工的信息
select emp.ename, emp.sal, tem.myavg from emp,(selectavg(sal) sal,deptno from emp group by deptno) temp
where tmep.deptno=emp.deptno and emp.sal>temp.sal
--请显示第5个到第10个入职的雇员(按照时间的先后顺序), 分页查询
select top 4 *from emp order by hiredate --显示前4个
--5到10个
select top 6 * from emp where empno not in
(select top 4 empno from emp order by hiredata)
order by hiredate;
--显示第5到9的人的信息(按照薪水的高低)
select top 5 * from emp where empno not in
(select top 4 empno from emp order by sal)
order by sal;
--identity(1,1),表示该testId字段自增,从1开始, 每次加1
--创建一个test表
create table test(
testId int primary key identity(1, 1)),
testName varchar(30),
testPass varchar(30))
--加入一行数据(主键是自增的,所以不用添加)
insert into test (testName, testPass) values('chengfeng','chengfeng')
--不断加入数据,每次加入test表的一倍,(testName,testPass)的值都是('chengfeng', 'chengfeng')
insert into test (testName, testPass) selecttestName,testPass from test
--查行数
select count(*) from test
--查整个表
select * from test
--分页查找
--test表按照id排序 100 105
select top 6 * from test
where testId not in
(select top 99 testId from test)
--创建一个没有主键的表
create table cat(
catId int,
catName varchar(40)
)
insert into cat values(1, 'aa')
select * from cat
--删除重复的记录, 借助#temp表,#开头一般表示临时
--使用distinct,可以去除重复
select distinct * into #temp from cat
delete from cat
insert into cat select * from #temp
drop table #temp
--左外连接和右外连接
--显示公司每个员工和它的上级的名字(自内连接)
select w.ename,b.ename from emp w, emp b where w.mgr = b.empno
--显示公司每个员工和它的上级的名字,要求没有上级的人的名字也要显示
--左外连接 table1 leftjoin table on ...
--指左边的表记录全部显示,如果没有匹配的记录就用null填
--右外连接 table1right join table on ...
--指右边的表记录全部显示,如果没有匹配的记录就用null填
select w.ename,b.ename from emp w left join emp b on w.mgr =b.empno
--约束
--not null(非空), uniue(唯一), primary key(主键), foreignkey(外键), check(约束表达式)
--创建一个表
create table test1(
testId int primary key identity(1, 1), --identity自增
testName varchar(30) not null, --不为空
testPass varchar(30),
testAge int --可以为空
)
--添加部分字段,其他字段为空
insert into test1 (testName, testAge) values('', 3)
--添加完整
insert into test1 values('', '', 3)
--创表
create table test2(
testId int primary key identity(1, 1), --identity自增
testName varchar(30) unique, --唯一, (行级定义)
testPass varchar(30),
testAge int --可以为空
)
--复合主键
create table test3(
testId int,
testName varchar(30),
testPass varchar(30),
testAge int,
primary key (testId, testName) --复合主键,(表级定义)
)
--check演示
create table test4(
testId int,
testName varchar(30),
testPass varchar(30),
sal int check(sal >= 1000 and sal<=2000)
)
--default使用
create table mes(
mesId int primary key identity(1, 1),
mescon varchar(2000) not null,
mesDate datetime default(getdate()) --默认给当前的值
)
--设计数据库示例
--商品表
create table goods(
goodsId nvarchar(50) primary key,
goodsName nvarchar(50) not null,
unitprice numeric(10, 2) check(unitprice > 0),
category nvarchar(50) check(category in('食物','日用品')),
providert nvarchar(50)
)
--客户表
create table customer(
customerId nvarchar(50) primary key,
name nvarchar(50) not null,
address nvarchar(50),
email nvarchar(50) unique,
sex nchar(1) check(sex in('男','女')) default '男',
cradId nvarchar(50)
)
--purchase表
create table purchase(
customerId nvarchar(50) foreign key referencescustomer(customerId),
goodsId nvarchar(50) foreign key references goods(goodsId),
nums int check(nums>0)
)
--数据库的备份和恢复
--备份数据库
--语法
backup database 要备份的数据库名 to disk='路径'
backup database aaa to disk='e:/sp.bak'
--删除数据库
drop database aaa
--恢复数据库
restore database 你的数据库名 from disk = '备份文件路径'
restore database aaa from disk='f:/sp.bak'
--最后,JDBC的连接代码
1、Oracle8/8i/9i数据库(thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为数据库的SID
String user="test";
String password="test";
Connection conn=DriverManager.getConnection(url,user,password);
2、DB2数据库
Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
3、Sql Server7.0/2000数据库(三个jar包:msbase.jar,mssqlserver.jar,msutil.jar)
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
//mydb为数据库
String user="sa";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
注意:一个jar包(sqljdbc4.jar)的时候,应该是这样的,除掉“microsoft”
Class.forName("com.jdbc.sqlserver.SQLServerDriver").newInstance();
Stringurl="jdbc:sqlserver://localhost:1433;DatabaseName=mydb";
(注意,如果是sqljdbc4.jar包),Class.forName可以不用写
4、Sybase数据库
Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/myDB";
//myDB为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
5、Informix数据库
Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/myDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);
6、MySQL数据库
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url="jdbc:mysql://localhost/myDB?user=soft&password=soft1234&useUnicod
e=true&characterEncoding=8859_1"
//myDB为数据库名
Connection conn= DriverManager.getConnection(url);
7、PostgreSQL数据库
Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/myDB"
//myDB为数据库名
String user="myuser";
String password="mypassword";
Connection conn=DriverManager.getConnection(url,user,password);