diff --git a/JDBC/bin/Demo/DEMO_EmpManage_JDBC.rar b/JDBC/bin/Demo/DEMO_EmpManage_JDBC.rar new file mode 100644 index 0000000..91d61df Binary files /dev/null and b/JDBC/bin/Demo/DEMO_EmpManage_JDBC.rar differ diff --git a/JDBC/bin/JDBC_day01/demo/JDBCDemo01.class b/JDBC/bin/JDBC_day01/demo/JDBCDemo01.class new file mode 100644 index 0000000..60e6f54 Binary files /dev/null and b/JDBC/bin/JDBC_day01/demo/JDBCDemo01.class differ diff --git a/JDBC/bin/JDBC_day01/demo/JDBCDemo02.class b/JDBC/bin/JDBC_day01/demo/JDBCDemo02.class new file mode 100644 index 0000000..8c64bc6 Binary files /dev/null and b/JDBC/bin/JDBC_day01/demo/JDBCDemo02.class differ diff --git a/JDBC/bin/JDBC_day01/demo/JDBCDemo03.class b/JDBC/bin/JDBC_day01/demo/JDBCDemo03.class new file mode 100644 index 0000000..5fb45a4 Binary files /dev/null and b/JDBC/bin/JDBC_day01/demo/JDBCDemo03.class differ diff --git a/JDBC/bin/JDBC_day01/demo/JDBCDemo04.class b/JDBC/bin/JDBC_day01/demo/JDBCDemo04.class new file mode 100644 index 0000000..77ebcd7 Binary files /dev/null and b/JDBC/bin/JDBC_day01/demo/JDBCDemo04.class differ diff --git a/JDBC/bin/JDBC_day01/lib/ojdbc5.jar b/JDBC/bin/JDBC_day01/lib/ojdbc5.jar new file mode 100644 index 0000000..4c02ee3 Binary files /dev/null and b/JDBC/bin/JDBC_day01/lib/ojdbc5.jar differ diff --git "a/JDBC/bin/JDBC_day01/note/JDBC\345\216\237\347\220\206.jpg" "b/JDBC/bin/JDBC_day01/note/JDBC\345\216\237\347\220\206.jpg" new file mode 100644 index 0000000..131028f Binary files /dev/null and "b/JDBC/bin/JDBC_day01/note/JDBC\345\216\237\347\220\206.jpg" differ diff --git a/JDBC/bin/JDBC_day01/note/summary.txt b/JDBC/bin/JDBC_day01/note/summary.txt new file mode 100644 index 0000000..12daaeb --- /dev/null +++ b/JDBC/bin/JDBC_day01/note/summary.txt @@ -0,0 +1,49 @@ +JDBCDemo01�� + 1.�������� + (1)�������ݿ�/����������(ע������): + 1)Class.forName("oracle.jdbc.driver.OracleDriver");//����������(ע������) + ��DriverManager.registerDriver(new OracleDriver()); + 2)Connection conn = DriverManager.getConnection(url, user, pwd);//�������Ӷ��� + (2)Statement statement = conn.createStatement();//ͨ��Connection���󴴽�Statement���� + (3)��ɾ��/��ѯ����Ҫ���Ρ� + (4)�ͷ���Դ + 2.DML��ɾ��:int rows = statement.executeUpdate(sql);//Ӱ���˶�����//ִ��sql��䣨DML��,Ҫ���Ρ� + 3.DQL��ѯ: + (1)ResultSet rs = statement.executeQuery(sql);//ִ��sql��䣨DQL������ȡ�����,Ҫ���Ρ� + (2)ResultSet��boolean next(); + (3)getInt(),getString(),getDouble(),getDate(),getObject();//getInt(1),getInt(empno);//��ȡ��һ�У���ֱ��д������ + 4.�淶д��; + +JDBCDemo02: + Java����ʵ���û���¼����֤�û���������. + ��ʾSQLע�빥���� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ�� + 3.ͨ��Connection���󴴽�Statement���� + 4.ִ��sql��䣨�����Dz�ѯ�� + 5.��ȡ�����������next()���������ж��û��Ƿ��½�ɹ��� + 6.�ͷ���Դ + +JDBCDemo03: + ʹ��PrepareStatement��ʵ�����ݱ��IJ�ѯ������ + PreparedStatement:Ԥ���루sql�Ƿ�Ϸ��淶������Statement�ӿڵ��ӽӿڣ���ֹSQL������������Ҫ���ܣ�����ߴ���Ŀɶ��ԣ���ά���ԣ����Ч�ʡ� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ��(�������ã�����ƴ��)//String sql = "select * from users where username=? and password=?"; + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.���÷�����ִ��sql��䣬��ȡ����� + rs = ps.executeQuery();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeQuery()����Ҫ��sql�����ˡ� + 5.��������� + 6.�ͷ���Դ + +JDBCDemo04: + ʹ��PrepareStatement��ʵ�����ݱ��ĸ��²����� + 1.�������ݿ� + 2.����Ҫ�޸ĵ��ñ�ź����û�����sql���ƴ��(�������ã�����ƴ��) + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.ִ��sql��� + int rows = ps.executeUpdate();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeUpdate()����Ҫ��sql�����ˡ� + 5.�ͷ���Դ \ No newline at end of file diff --git a/JDBC/bin/JDBC_day02/demo/JDBCDemo01.class b/JDBC/bin/JDBC_day02/demo/JDBCDemo01.class new file mode 100644 index 0000000..e1475da Binary files /dev/null and b/JDBC/bin/JDBC_day02/demo/JDBCDemo01.class differ diff --git a/JDBC/bin/JDBC_day02/demo/JDBCDemo02.class b/JDBC/bin/JDBC_day02/demo/JDBCDemo02.class new file mode 100644 index 0000000..21b3b99 Binary files /dev/null and b/JDBC/bin/JDBC_day02/demo/JDBCDemo02.class differ diff --git a/JDBC/bin/JDBC_day02/demo/JDBCDemo03.class b/JDBC/bin/JDBC_day02/demo/JDBCDemo03.class new file mode 100644 index 0000000..5c7fa13 Binary files /dev/null and b/JDBC/bin/JDBC_day02/demo/JDBCDemo03.class differ diff --git a/JDBC/bin/JDBC_day02/demo/bean/Users.class b/JDBC/bin/JDBC_day02/demo/bean/Users.class new file mode 100644 index 0000000..4f4be83 Binary files /dev/null and b/JDBC/bin/JDBC_day02/demo/bean/Users.class differ diff --git a/JDBC/bin/JDBC_day02/demo/util/DBUtil.class b/JDBC/bin/JDBC_day02/demo/util/DBUtil.class new file mode 100644 index 0000000..fd9782e Binary files /dev/null and b/JDBC/bin/JDBC_day02/demo/util/DBUtil.class differ diff --git a/JDBC/bin/JDBC_day02/note/summary.txt b/JDBC/bin/JDBC_day02/note/summary.txt new file mode 100644 index 0000000..8d8a551 --- /dev/null +++ b/JDBC/bin/JDBC_day02/note/summary.txt @@ -0,0 +1,62 @@ +DBUtil: + 实现JDBC的工具类 + 定义静态方法,直接返回数据库的连接对象 + 定义静态方法,关闭所有资源 + +JDBCDemo01: + 使用了DBUtil工具类 + 使用PrepareStatement,实现模糊查询:ps.setString(1, "%"+condition+"%"); + +JDBCDemo02: + JDBC读取数据表users,每行数据封装到USers类的对象中。 + +JDBCDemo03: + 银行转账 + conn.setAutoCommit(false);//将事务的自动提交取消。 + conn.commit();//手动提交事务,可将上面的逻辑全部控制在一个事务中。 + conn.rollback();//回滚事务。 + +EmpManage_JDBC: + 1.bean:实体类 + 2.dao:实现类(包)daoimpl,接口 + 3.test:测试类 + 4.DBUtil: + 静态--连接数据库的方法 + public static Connection getConnection (){ + Connection conn = null; + try{ + conn = Class.forName("jdbc.oracle.driver.OracleDriver"); + String url = ? + String username = ? + String password = ? + conn = DriverManager.getConnection(url, username, password); + }catch(Exception e){ + e.printStackTrace(); + } + return conn; + }; + 静态--关闭资源的方法 + public static void close(Connection conn,Statement ps,ResultSet rs) { + try { + if(rs != null) { + rs.close(); + } + if(ps != null) { + ps.close(); + } + if(conn != null) { + con.close(); + } + }catch(SQLException e) { + e.printStackTrace(); + } + } + +补充: + 1.sql.date转util.date可以自动转换。 + util.date--->sql.date: java.sql.Date date = new java.sql.Date(emp.getHiredate().getTime()); + //sql.Date的构造可以传1970的毫秒值,util.Date的getTime()可以得到1970的毫秒值。 + 2.String--->util.Date + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + emp.setHiredate(sdf.parse(sc.next()));//将String--->util.date,会抛出ParseException异常。 + diff --git a/JDBC/bin/note/JDBC_day01/JDBC_day01_summary.txt b/JDBC/bin/note/JDBC_day01/JDBC_day01_summary.txt new file mode 100644 index 0000000..12daaeb --- /dev/null +++ b/JDBC/bin/note/JDBC_day01/JDBC_day01_summary.txt @@ -0,0 +1,49 @@ +JDBCDemo01�� + 1.�������� + (1)�������ݿ�/����������(ע������): + 1)Class.forName("oracle.jdbc.driver.OracleDriver");//����������(ע������) + ��DriverManager.registerDriver(new OracleDriver()); + 2)Connection conn = DriverManager.getConnection(url, user, pwd);//�������Ӷ��� + (2)Statement statement = conn.createStatement();//ͨ��Connection���󴴽�Statement���� + (3)��ɾ��/��ѯ����Ҫ���Ρ� + (4)�ͷ���Դ + 2.DML��ɾ��:int rows = statement.executeUpdate(sql);//Ӱ���˶�����//ִ��sql��䣨DML��,Ҫ���Ρ� + 3.DQL��ѯ: + (1)ResultSet rs = statement.executeQuery(sql);//ִ��sql��䣨DQL������ȡ�����,Ҫ���Ρ� + (2)ResultSet��boolean next(); + (3)getInt(),getString(),getDouble(),getDate(),getObject();//getInt(1),getInt(empno);//��ȡ��һ�У���ֱ��д������ + 4.�淶д��; + +JDBCDemo02: + Java����ʵ���û���¼����֤�û���������. + ��ʾSQLע�빥���� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ�� + 3.ͨ��Connection���󴴽�Statement���� + 4.ִ��sql��䣨�����Dz�ѯ�� + 5.��ȡ�����������next()���������ж��û��Ƿ��½�ɹ��� + 6.�ͷ���Դ + +JDBCDemo03: + ʹ��PrepareStatement��ʵ�����ݱ��IJ�ѯ������ + PreparedStatement:Ԥ���루sql�Ƿ�Ϸ��淶������Statement�ӿڵ��ӽӿڣ���ֹSQL������������Ҫ���ܣ�����ߴ���Ŀɶ��ԣ���ά���ԣ����Ч�ʡ� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ��(�������ã�����ƴ��)//String sql = "select * from users where username=? and password=?"; + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.���÷�����ִ��sql��䣬��ȡ����� + rs = ps.executeQuery();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeQuery()����Ҫ��sql�����ˡ� + 5.��������� + 6.�ͷ���Դ + +JDBCDemo04: + ʹ��PrepareStatement��ʵ�����ݱ��ĸ��²����� + 1.�������ݿ� + 2.����Ҫ�޸ĵ��ñ�ź����û�����sql���ƴ��(�������ã�����ƴ��) + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.ִ��sql��� + int rows = ps.executeUpdate();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeUpdate()����Ҫ��sql�����ˡ� + 5.�ͷ���Դ \ No newline at end of file diff --git "a/JDBC/bin/note/JDBC_day01/JDBC\345\216\237\347\220\206.jpg" "b/JDBC/bin/note/JDBC_day01/JDBC\345\216\237\347\220\206.jpg" new file mode 100644 index 0000000..131028f Binary files /dev/null and "b/JDBC/bin/note/JDBC_day01/JDBC\345\216\237\347\220\206.jpg" differ diff --git a/JDBC/bin/note/JDBC_day02/JDBC_day02_summary.txt b/JDBC/bin/note/JDBC_day02/JDBC_day02_summary.txt new file mode 100644 index 0000000..9a00174 --- /dev/null +++ b/JDBC/bin/note/JDBC_day02/JDBC_day02_summary.txt @@ -0,0 +1,17 @@ +DBUtil: + 实现JDBC的工具类 + 定义静态方法,直接返回数据库的连接对象 + 定义静态方法,关闭所有资源 + +JDBCDemo01: + 使用了DBUtil工具类 + 使用PrepareStatement,实现模糊查询:ps.setString(1, "%"+condition+"%"); + +JDBCDemo02: + JDBC读取数据表users,每行数据封装到USers类的对象中。 + +JDBCDemo03: + 银行转账 + conn.setAutoCommit(false);//将事务的自动提交取消。 + conn.commit();//手动提交事务,可将上面的逻辑全部控制在一个事务中。 + conn.rollback();//回滚事务。 diff --git a/JDBC/bin/note/project/one/project_EmpManage_summary b/JDBC/bin/note/project/one/project_EmpManage_summary new file mode 100644 index 0000000..8a69bae --- /dev/null +++ b/JDBC/bin/note/project/one/project_EmpManage_summary @@ -0,0 +1,43 @@ +EmpManage_JDBC�� + 1.bean:ʵ���� + 2.dao:ʵ����(��)daoimpl���ӿ� + 3.test:������ + 4.DBUtil: + ��̬--�������ݿ�ķ��� + public static Connection getConnection (){ + Connection conn = null; + try{ + conn = Class.forName("jdbc.oracle.driver.OracleDriver"); + String url = �� + String username = �� + String password = �� + conn = DriverManager.getConnection(url, username, password); + }catch(Exception e){ + e.printStackTrace(); + } + return conn; + }; + ��̬--�ر���Դ�ķ��� + public static void close(Connection conn,Statement ps,ResultSet rs) { + try { + if(rs != null) { + rs.close(); + } + if(ps != null) { + ps.close(); + } + if(conn != null) { + con.close(); + } + }catch(SQLException e) { + e.printStackTrace(); + } + } + +���䣺 + 1.sql.dateתutil.date�����Զ�ת���� + util.date--->sql.date�� java.sql.Date date = new java.sql.Date(emp.getHiredate().getTime()); + //sql.Date�Ĺ�����Դ�1970�ĺ���ֵ��util.Date��getTime()���Եõ�1970�ĺ���ֵ�� + 2.String--->util.Date + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + emp.setHiredate(sdf.parse(sc.next()));//��String--->util.date,���׳�ParseException�쳣�� diff --git "a/JDBC/bin/note/project/two/JAVA\345\237\272\347\241\200\345\256\236\350\256\255\347\273\203\344\271\240.docx" "b/JDBC/bin/note/project/two/JAVA\345\237\272\347\241\200\345\256\236\350\256\255\347\273\203\344\271\240.docx" new file mode 100644 index 0000000..1b1ea81 Binary files /dev/null and "b/JDBC/bin/note/project/two/JAVA\345\237\272\347\241\200\345\256\236\350\256\255\347\273\203\344\271\240.docx" differ diff --git a/JDBC/bin/note/project/two/carRent_JDBC.sql b/JDBC/bin/note/project/two/carRent_JDBC.sql new file mode 100644 index 0000000..f96570d --- /dev/null +++ b/JDBC/bin/note/project/two/carRent_JDBC.sql @@ -0,0 +1,52 @@ +--���� +create table car( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + type varchar2(20) not null +); +create table bus( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + seat number(2) not null +); +create table truck( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + tonnage number(2) not null +); +create table rented( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + type varchar2(20), + seat number(2), + tonnage number(2) +); +--�������� +insert into car values('����','��NY28588',800,'X6'); +insert into car values('����','��CNY3284',600,'550i'); +insert into car values('���','��NT37465',300,'������'); +insert into car values('���','��NT96968',600,'GL8'); +insert into bus values('��','��6566754',800,16); +insert into bus values('����','��8696997',800,16); +insert into bus values('��','��9696996',1500,34); +insert into bus values('����','��8696998',1500,34); +insert into truck values('һ�����','��MH98725',50,5); +insert into truck values('�������','��L593216',50,5); +insert into truck values('һ�����','��NU98631',50,10); +insert into truck values('�������','��CY56312',50,10); +--�鿴�� +select * from car; +select * from bus; +select * from truck; +select * from rented; +--ɾ���� +drop table car; +drop table bus; +drop table truck; +drop table rented; + + diff --git "a/JDBC/bin/note/sql/Oracle\345\216\237\345\247\213\347\254\224\350\256\260.txt" "b/JDBC/bin/note/sql/Oracle\345\216\237\345\247\213\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..d971d98 --- /dev/null +++ "b/JDBC/bin/note/sql/Oracle\345\216\237\345\247\213\347\254\224\350\256\260.txt" @@ -0,0 +1,810 @@ +�����Ĺ��ɣ� +1��ǰ�� HTML��CSS��JS��JQuery +2����̨ java +3�����ݿ� Oracle Mysql(�׹���) + +1.��װOracle + + �����û��� + sys�������û� �������Ȩ�� ����sysdba��ɫ �����ݿ����Ա�� + �д������ݿ��Ȩ�� + system����������Ա Ȩ�޽����ڳ�������Ա + + scott:��ͨ�û� һ��������ϰ scott -- tiger + �����û���alter user scott account unlock + �������룺alter user scott identified by tiger + +2.��¼��ʽ + 1��sqlplus �س� + �����û��� + �������� + + 2��sqlplus/nolog + conn scott/tiger + +3. DB��DataBase �������ݽṹ���������ݵIJֿ� + ��ϵ�����ݿ� + �Զ�ά����ʽ�洢����(�����У��ֶ��� �����У�����) + + + DBMS�����ݿ����ϵͳ + mysql��oracle��SqlServer +4.�������� + sqlplus �����ݿ� + conn �û���/���� �������ݿ� + show user; ��ʾ��ǰ�û� + exit; �Ͽ����� + quit; �Ͽ����� +5.SQL:�ṹ����ѯ���� + SQL�ķ��ࣺ***** + DDL ���ݶ������� ���ڲ������ݶ��󣨱� ��ͼ ���� ������ + DML ���ݲ������� ������ɾ�ı��е�����(ͨ����������) + TCL ����������� �������ݵ��ύ�ͳ��� + DQL ���ݲ�ѯ���� ���ڲ�ѯ���ݼ�¼ + DCL ���ݿ������� ����ִ��Ȩ�޷��� + + DDL�����ݶ������� + ---���ڲ������ݿ���󣨱� ��ͼ ���� ������ + CREATE���������������������� + ALTER���޸ı�����������Ľṹ + DROP��ɾ��������������Ľṹ + TRUNCATE����ձ�(ɾ���������ݣ��������ṹ) + DML�����ݲ������� + ---������ɾ�ı��е�����(ͨ����������) + INSERT INTO�������ݲ��뵽���� + DELETE��ɾ���������� ---- WHERE + UPDATE ���� SET�����±������� --- WHERE + + TCL������������� + COMMIT���ύ--���� + ROLLBACK���ع�---���� + + DQL�����ݲ�ѯ���� + SELECT ��ѯ***** + + DCL�����ݿ������� + ����ִ��Ȩ�޷��� GRANT REVOKE +6.DDL ���ݶ������� + 1�������� + �鿴�û�����ı��� + SELECT table_name FROM user_tables; + CREATE TABLE ���� ( + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��] + ); + CREATE TABLE users( + id number(10) primary key, + name varchar2(20) not null unique, + salary number(10,2), + create_time date default sysdate + ); + id(����) �ص㣺���������ű���ÿһ���������ֵ������ͬ���ұ�����ֵ���ǿ���Ψһ��PRIMARY KEY + �������� + ������������ + 1����������ĸ��ͷ + 2��������1-30���ַ�֮�� + 3������ֻ�ܰ���A-Z,a-z,0-9,_,$,��# + 4�����벻�ܺ��û������������������ + 5�����벻����Oracle�ı����� + ��������: + 1��NUMBER:��ֵ���� + NUMBER(X):x����λ�� + NUMBER(3) ����ܴ��3λ�� + NUMBER(X,Y):x��ʾ����λ y��ʾ�����м�λ��С�� + NUMBER(6,2) 8888.88 + 2��CHAR:�����ַ��� + CHAR(X):X��ʾ��ռ�����ֽ� ����2000�ֽ� + 3��VARCHAR2:�䳤�ַ��� + VARCHAR2(X):X��ʾ��ռ�����ֽ�(�������б�������ݳ��ȣ�ռ�õĿռ��DZ仯��) ���4000�ֽ� + 4��DATE ʱ������ + SELECT SYSDATE FROM DUAL + α����dualȷʵ��һ�ű�����һ��ֻ��һ���ֶΣ�һ�м�¼�ı���ϰ�������dz�֮Ϊα������Ϊ�����洢 + ��������� + 2��ɾ������ + DROP TABLE users; + 3��ALTER���޸ı� + �޸ı����� + RENAME ԭ���� TO �±��� + �޸ı��ṹ�� + ����������ֶΣ� + ALTER TABLE ���� ADD(�ֶ��� ��������[Ĭ��ֵ Լ��]) + ��ֻ����������󣬲��ܲ��뵽���е����� + ɾ�����������ֶΣ� + ALTER TABLE ���� DROP(�ֶ���) + �޸ı��������ֶεĸ�ʽ�� + ALTER TABLE ���� MODIFY(�ֶ��� ��������[Ĭ��ֵ Լ��]) +7��DML ���ݲ������� + 1��������������� + ȫ�ֶβ������ݣ� + INSERT INTO ���� VALUES(1,'����',8000,sysdate,'����Ա'); + ע�⣺values�е�����˳��Ҫ�Ͷ����ֶ�ʱһ�� + �����ֶβ������� + INSERT INTO ����(��Ҫ�������ݵ��ֶ�) VALUES(���ֶ�˳�����Ӧ������) + 2���޸ı����������� + ���޸ı�������ʱ��ͨ������where�Ӿ����޶�Ҫ�޸ĵļ�¼��������ֻ���޸�����where�����ļ�¼�� + ������ȫ���޸� + UPDATE ���� SET �ֶ���=����,�ֶ���=���� WHERE �ֶ���=���� + 3��ɾ���������� + ͨ����where�Ӿ����ã�����Ļ���ɾ���������� + DELETE FROM ���� WHERE �ֶ���=���� + ��DDL����е�TRUNCATE��䣬ͬ����ɾ�������ݵ����� + ��delete������ + --delete����������ɾ����truncate��������ȫ��ɾ�� + --delete��DML�����Ի��ˣ�truncate��DDL��䣬������Ч���޷����� + --�����ɾ��ȫ������¼�����������ϴ�delete���Ч�ʱ�truncate���� + ɾ��ȫ����¼�� + delete from users; + ���� + truncate table users; +8.TCL ����������� + commit �ύ + ��ʾ����� ��ť + ��ʽ�������ر����ݿ⹤�� + rollback �ع�---���� + ������ĸ����ԣ�***** + 1.ԭ���ԣ�������������в���Ҫôȫ���ɹ���Ҫôȫ��ʧ�ܻع�;�ɹ�����Ҫ��ȫӦ�õ����ݿ⣬ʧ�����ܶ����ݿ� + ����Ӱ�죻 + 2.һ���ԣ�����ִ��ǰ��ִ�к���봦��һ����״̬�� + 3.�����ԣ�������û������������ݿ�ʱ�����ݿ�Ϊÿһ���û����������񣬲�����������IJ��������ţ������ + ������֮��Ҫ�໥���룻 + 4.�־��ԣ�һ������һ�����ύ�ˣ���ô�����ݿ��е����ݵĸı���������Ե� +9.DQL ���ݲ�ѯ���� + ������ѯ�����SELECT�Ӿ��FROM�Ӿ���� + SELECT��ָ��Ҫ��ʾ���ֶ����� + FROM��ָ��������Դ + 1����ѯ���� + SELECT * FROM ����; + 2����ѯ����������Ϣ + SELECT �ֶ�����1,�ֶ�����2 FROM ���� WHERE �޶�����; + ע�⣺SELECT ������ʾ�ֶΡ�����ʽ������ + <>�Dz����� + ���������ƽϳ��������ڲ�ѯ�����������ʾ������Ը���ѯ�������ʹ���б��� + �������������ֿ�ͷ���������ֿ�ͷ������Ҫ��""���ϣ�ͬ�������Է��ſ�ͷ + +10�����ú��� + 1���ַ������� + CONCAT(c1,c2)�����ַ�������c1��c2�����ֶ�ƴ����ʾ + ||�����ַ������ţ�����ַ������ӣ���||��ֱ�ۣ� + SELECT ename || ':'|| sal as "����:����" from EMP + + LENGTH(�ֶ���)��ʾ�ַ������� + SELECT length(ename) from emp + + UPPER(�ֶ���)ת��д + LOWER(�ֶ���)תСд + INITCAP(�ֶ���)����ĸ��д + + TRIM() ȥ���ַ���ǰ��ָ������ + select trim('E' from 'EEEEEHELLO WORLDE') from dual + + ȥ��Ա���������ֵ�����ĸA + select ename,trim('A' FROM ename) from emp + + LTRIM('�ַ���','')ȥ���ַ������ָ������ + RTRIM('�ַ���','')ȥ���ַ����Ҳ�ָ������ + + LPAD(ch1,m,ch2) + RPAD(ch1,m,ch2) + ��λ���� + ch1Ҫ��ѯ��ʾ������ mָ��λ�� + ch2����λ������ʱ��ʲô���в�λ + ��emp����ʹ����λ����sql��0����6λ + select ename,rpad(sal,6,'$') from emp + SUBSTR(ch,m,n)����ȡ�ַ���--��1��ʼ + ��ȡch����ַ�������m��ʼ��ȡ������ȡn�� + ��mΪ���������ǴӺ���ǰ�����±��ѯ�Ӷ����н�ȡ + INSTR(ch1,ch2,m,n)������ch1��ch2���ֵ�λ�ã�m�������ĸ�λ�ÿ�ʼ������n�����ڼ��γ��� + ��ָ��m��n��Ĭ��ֵ����1 + 2�����ֺ��� + ROUND(n,m)�������������� + �����е�n�������κ����֣�ָҪ������������ + m������������mȡ�������������뵽С������mλ + mȡ0���������뵽����λ + mȡ���������������뵽С����ǰmλ + mȱʡ��Ĭ��ֵ��0 + TRUNC(n,m)����ȡ���֣�û���������� + select trunc(45.678,2),trunc(45.678,0),trunc(45.678,-1) from dual + MOD(m,n)������m����n���������nΪ0��ֱ�ӷ���M + нˮֵ��1000ȡ���� + select ename,sal,mod(sal,1000) from emp + CEIL(n)������ȡ�� ���ڸ�������С���� + FLOOR(n)������ȡ�� С�ڸ������������ + 3�����ں��� + sysdate�����ص�ǰ��ϵͳʱ�䣬��ȷ���� select sysdate from dual + systimestamp�����ص�ǰϵͳ���ں�ʱ�䣬��ȷ�����루ʱ����� + 1��TO_DATE()���൱����SimpleDateFormat��parse()���� + ���ã����������ַ������ո��������ڸ�ʽת��ΪDate + select to_date('2018-12-01 11:30','YYYY-MM-DD HH24:MI:SS') from dual + 2��TO_CHAR()�����ڽ�DATE���ո��������ڸ�ʽת��Ϊ�ַ��� + select ename,to_char(hiredate,'YYYY"��"MM"��"DD"��"') from emp + ���ڳ��ú����� + 1��LAST_DAY(date)������ָ�����������µ����һ�� + select last_day(sysdate) from dual + 2��ADD_MONTHS(date,i)����������date����i���º������ֵ������i�Ǹ��������õ��Ǽ�ȥi���º�� + ����ֵ + ��ѯԱ����ְ20��������� + select ename,add_months(hiredate,20*12) "20����" from emp + 3��MONTHS_BETWEEN(date1,date2)��������������֮�������ٸ��� + ʵ���������date1-date2�����date2ʱ���date1ʱ��������õ���ֵ�������������ڼ���������£� + �����õ���С��λ�Ľ�� + ����Ա����ְ���ٸ��� + select ename,trunc(months_between(sysdate,hiredate)) from emp + date�������������ݿ��п��Խ��м���������������������� + select ename,sysdate-hiredate from emp + 4��LEAST(date1,date2)����ʾ����ʱ���е���Сֵ + GREATEST(date1,date2)����ʾ����ʱ���е����ֵ + �������ͱ���һ�� + select LEAST(sysdate,to_date('2018-12-1','YYYY-MM-DD')) from dual + 5��EXTRACT(date from datetime)���Ӳ���datetime����ȡ����dateָ�������ݣ�������ȡ�ꡢ�¡��� + select extract(day from sysdate) from dual + ��ѯ1980����ְ��Ա������Щ + select ename from emp where extract(year from hiredate) = 1980 + 4����ֵ���� + null�ĺ��壺 + ��ʱ���е�ijЩ�ֶ�ֵ������δ֪����ʱ�����ڣ�ȡֵnull + �κ��������;���ȡֵnull + null������ѯ�� + null�������κ�ֵ + �ж�һ���ֶε�ֵ�Ƿ�Ϊnull��Ҫʹ��is null��is not null + select * from emp where comm is null + �ǿ�Լ���� + �ǿգ�not null��Լ������ȷ���ֶ�ֵ��Ϊ�� + 1��NVL(arg1,arg2)����nullת��Ϊ��nullֵ + ���arg1Ϊnull������arg2�����򷵻�arg1��arg1��arg2�������κ��������ͣ��������������������ͱ� + ����һ�µ� + ����Ա�������� + select ename,sal,comm,sal+nvl(comm,0) from emp + 2��NVL2(arg,res1,res2)�����argΪnull���򷵻�res2;arg��Ϊnull���򷵻�res1 + select ename,sal,comm,nvl2(comm,sal+comm,sal) from emp +11��������ѯ��䣺 + 1��from�Ӿ䣺 + select * from ���� + select--����ָ��Ҫ��ѯ���� + from--ָ��Ҫ���ĸ����в�ѯ + 2��where�Ӿ䣺 + ��select����У�������where�Ӿ���ʹ�ñȽϲ��������Ʋ�ѯ��� + ��ѯ����10�µ�Ա����Ϣ + select * from emp where deptno=10 + ��ѯԱ������ְλ��'SALESMAN'��Ա����Ϣ + select * from emp where job='SALESMAN' --oracle���ַ������ִ�Сд +12����ѯ���� + 1��ʹ��>,<,>=,<=,!=,<>,= + ��ѯְԱ����нˮ����2000��ְԱ��Ϣ + select ename,sal from emp where sal<2000 + ��ѯְԱ���в����ڲ���10��Ա����Ϣ + select ename,sal,job from emp where deptno <> 10 + ��ѯְԱ������1981��1��1���Ժ���ְ��ְԱ��Ϣ + 2��ʹ��AND,OR�ؼ��� + AND ���� + OR ���� + ��ѯнˮ����1000����ְλ�ǡ�CLERK����ְԱ��Ϣ + select ename,sal,job from emp where sal>1000 and job='CLERK' + ��ѯнˮ����1000����ְλ�ǡ�CLERK����ְԱ��Ϣ + select ename,sal,job from emp where sal>1000 or job='CLERK' + ע�⣺AND���ȼ�����OR������ʹ��()���������ȼ�˳�� + select ename,sal,deptno from emp where deptno=20 or deptno=30 and sal>800 + 3��ģ����ѯ***** + LIKE����ģ��ƥ���ַ��� + select ename,job from emp where ename like 'A%' + _:����һ���ַ� + %:�������ַ� + 4��ʹ��IN��NOT IN + IN(list)��ȡ�������б���Χ�е����� + NOT IN(list)��ȡ�������ϴ��б��е����ݼ�¼ + ��ѯְλ��MANAGER����CLERK��Ա����Ϣ + select * from emp where job in('MANAGER','CLERK') + ��ѯ���Dz���10��20��Ա�� + 5��BETWEEN...AND... + ������ѯ����ij��ֵ��Χ���������ݣ������䣩 + �ȼ���>= AND <=��Ч�� + ��ѯнˮ��1500-3000֮���ְԱ��Ϣ + select * from emp where sal between 1500 and 3000 + 6��ʹ��ANY��ALL���� + ALL��ANY���ܵ���ʹ�ã���Ҫ��ϵ��бȽϲ�����>,>=,<,<=һ��ʹ�� + >ANY��������С + ALL��������� + ANY(select sal from emp where job='MANAGER') + 7��distinct�����ظ����� + ��ѯԱ���IJ��ű�� + select distinct deptno from emp + ��ѯÿ�����ŵ�ְλ + select distinct deptno,job from emp +13����������ȼ� + 1��() + 2�������������* / + - + 3�����ӷ���|| + 4���Ƚ��������= > >= < <= <> is null,like,between...and...,in + 5���߼��������not and or + BETWEEN...AND��AND������ + 1��BETWEEN...AND��ֻ�������Ƚ�һ���ֶ�ֵ + 2��AND�����������Ƚϲ�ͬ���ֶ� +14��ORDER BY ���� + �Խ���������򣬱��������select�����һ���Ӿ� + ���ԶԽ��������ָ���ֶν�������ASC����DESC + select ename,sal from emp order by sal asc + ע�⣺������ֶ���������NULL����NULL���������ֵ + select * from emp where deptno=10 order by mgr desc + ��������� + ���Զ�����Ϊ�����׼ʱ�����Ȱ��յ�һ�н������������һ��������ͬ�����Եڶ��������Դ����� + �������򲻹������ǵ���ÿ������Ҫ������������ʽ + ��Ա�����е�ְԱ�����Ȱ��ղ��ű���������У��ٰ���нˮ�������� + select ename,deptno,sal from emp order by deptno asc,sal desc +15���ۺϺ���(���к��������麯��) + 1��MAX��MIN������ȡ���л����ʽ�������Сֵ + ��������ͳ���κ��������ͣ��������֡��ַ������� + ��ȡ�����µ����нˮ�����нˮ������������ + select max(sal),min(sal) from emp + �������������ְʱ�䣬���������� + select max(hiredate),min(hiredate) from emp + 2��AVG��SUM������ͳ���л����ʽ��ƽ��ֵ���� + ֻ�ܲ����������ͣ�����NULLֵ + ��ȡ������ȫ��ְԱ��ƽ��нˮ��нˮ�ܺ� + select AVG(sal),SUM(sal) from emp + 3��COUNT������������еļ�¼����������NULLֵ + ��ȡְԱ����һ���ж�����ְԱ��¼ + select count(*) from emp + ���ְԱ�����ж��������н���� + select count(comm) from emp + COUNT��SUM������ + COUNT��ͳ�Ƽ�¼����SUM��ͳ�Ƽ�¼��ֵ���ܺͣ�ע���������� + 4���ۺϺ����Կ�ֵ�Ĵ��� + �ۺϺ�������NULLֵ + select avg(comm) from emp + �����ϣ������nullֵ��ʹ��NVL���� + select avg(nvl(comm,0)) from emp +16��GROUP BY ���� + �����������ָ���ֶν��з��� + ������򣺸��ֶ��µ�ֵһ���ļ�¼������һ�� + ��ϣ���õ�ÿ�����ŵ�ƽ��нˮ������������������ƽ��нˮ + select MAX(sal),MIN(sal),AVG(sal),SUM(sal) from emp group by deptno + select������������˾ۺϺ�����ͬʱ�������ijһ�ֶ�ʱ����ô���ֶα��������group by�Ӿ��� + where���治��ʹ�þۺϺ��� + select MAX(sal),deptno from emp where MAX(sal)>4000 group by deptno������ִ�У� + HAVING�Ӿ� + �����Է����Ľ����һ�����ƣ����簴���ŷ���󣬵õ�ÿ�����ŵ���߹��ʣ����Լ������������� + �������group by���棬���ܵ������� + WHERE�ǷǷ��麯���Ĺ����ж� + HAVING�Ƿ��飨�ۺϣ������Ĺ����ж� + ��ѯ����������ʳ���4000�IJ����Լ������ + select MAX(sal),deptno from emp group by deptno having MAX(sal)>4000 + �鿴���ű�ţ��Լ��ò��ŵ�ƽ�����ʣ�Ҫ���Ǹò��ŵ�ƽ������Ҫ����2000��ְλ��PRESIDENT�IJ�����ͳ�� + select deptno,AVG(sal) from emp where job <> 'PRESIDENT' group by deptno having AVG(sal)>2000 +17��SELECT���ִ�й���***** + 1��ͨ��from�Ӿ����ҵ���Ҫ��ѯ�ı� + 2��ͨ��where�Ӿ���зǷ��麯��ɸѡ�ж� + 3��ͨ��group by�Ӿ���ɷ������ + 4��ͨ��having�Ӿ�����麯��ɸѡ�ж� + 5��ͨ��select�Ӿ�ѡ��Ҫ��ʾ���л����ʽ���麯�� + 6��ͨ��order by�Ӿ����������� +18�������ܽ�***** + ���к����Ͷ��к����ķ��ಢ����˵�� + ���к����� + �ַ�������substr() trim() length()... + ��ֵ������round() trunc() mod()... + ���ں�����last_day() add_months()... + ת�������� + ��ʾת�� to_char() to_date() + ��ʽת�� �����������ַ�������ͨ���ᱻתΪ�ַ��� + ͨ�ú�����NVL() NVL2() + ע�⣺ͨ�����ڲ����������� ��������Ƕ�׶�㣬���ʱ������������ִ�� + ���к�����MAX()���ֵ MIN()��Сֵ SUM()��� AVG()ƽ��ֵ COUNT()ͳ�� +19��������ѯ + �����ĸ�� + ʵ��Ӧ��������Ҫ�����ݣ���������Ҫ��ѯ�������������ϵı������ֲ�ѯ�������Ӳ�ѯ�����Ӳ�ѯͨ���������� + �໥�� + ϵ�ĸ��ӱ�֮�� + �����������һ�ű��������г�Ϊ��� + ��������ı����ڹ�����ϵ�����ڶ��һ�� + �����֮��Ĺ�ϵ��һ�Զࣨͨ�����������ϵ������Զࣨͨ�������ű�������ϵ����һ��һ + N�ű��Ĺ�����ѯ��������ҪN-1����������������ָ����������������ֵѿ�������������Ľ������ + �ѿ������� + �ѿ�����ָ������������ÿ������ÿһ�к���������ÿһ������ϣ������������ļ�¼�����ֱ���X��Y���ѿ������� + ����X*Y����¼ + select count(*) from emp--14����¼ + select count(*) from dept--4����¼ + select * from emp,dept--56����¼ + ��ֵ���� �÷������=���� + �鿴Ա�����֡����ʡ��������� + select ename,sal,dname from emp e,dept d where e.deptno=d.deptno + select ename,sal,dname + from emp e,dept d + where e.deptno=d.deptno and d.loc='CHICAGO' + KING�����﹤���Լ����IJ��ű���Ƕ��� + select e.ename,e.deptno,d.loc + from emp e,dept d + where e.deptno=d.deptno and e.ename='KING' + 1.���Ը����������ͨ������ָ���ֶ������ĸ��������������ֶ�������ͬ�������ָ��������Դ + 2.ֻ���������������Ľ�����Żᱻ��ѯ���� + 3.���й�����������ͬʱ�������������͹������������ݲŻᱻ��ѯ���� + �����ӣ� + ���������������������ļ�¼�������Ϻ͵�ֵ����û������ֻ��д����ͬ + �﷨����1 INNER JOIN ��2 ON ����������INNER����ʡ�ԣ� + select ename,sal,dname from emp e INNER JOIN dept d ON e.deptno=d.deptno + ������ + �����ӷ��������������������ݼ�¼����Щ�������Ҫ������Щ���������������ļ�¼����Ҫʹ�������� + ������Ͳ��������������Ľ��ȫ����ѯ���� + ���ٴ����б��и������� + create table empc as select * from emp + ��empc���в���һ������ ���ź�Ϊ50 + insert INTO empc(empno,ename,deptno) values(1000,'JACK',50) + �������ӣ�(�����ȫ�����ұ��IJ���) + ��������ļ�¼���ڱ����ӵ��ұ����ҳ����������ļ�¼��֮ƥ�䣬����Ҳ��������ƥ��ģ���null��ʾ + �﷨����1 LEFT OUTER JOIN ��2 ON ����������OUTER����ʡ�ԣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ������������κβ��ŵ�Ա�� + select empno,ename,dname + from empc e + left join dept d + on e.deptno=d.deptno + �������ӣ�(�ұ���ȫ��������IJ���) + �����ұ��ļ�¼���ڱ����ӵ�������ҳ����������ļ�¼��֮ƥ�䣬����Ҳ���ƥ��ģ���null���� + �﷨����1 RIGHT OUTER JOIN ��2 ON ����������OUTER����ʡ�ԣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ�����û��Ա���IJ��� + select empno,ename,dname + from empc e + right join dept d + on e.deptno=d.deptno + ȫ������ + ���ط������������б��ļ�¼��û����֮ƥ��ģ���null��ʾ������������Ӻ������ӵIJ����� + �﷨����1 FULL OUTER JOIN ��2 ON ����������OUTER����ʡ�ԣ� + ������ + ���ӵ�����������ͬһ���������е�һ���ֶο��Զ�Ӧ��ǰ���������ֶ� + ��ѯÿ��Ա�����ϼ��쵼��˭ + select e.ename emp,m.ename mgr + from emp e,emp m + where e.mgr=m.empno + ��ѯSMITH����˾���ĸ����й��� + Oracle���ݿ��Դ��﷨��***** + 1���ѿ����� + 2�������ӣ�1.��ֵ���� + 2.�ǵ�ֵ���� + ���Ա����š����������ʡ����ʵȼ� + select e.empno,e.ename,e.sal,s.grade + from emp e,salgrade s + where e.sal between s.losal and s.hisal + 3�������ӣ�1.�������ӣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ������������κβ��ŵ�Ա�� + select empno,ename,dname + from emp e,dept d + where e.deptno=d.deptno(+) + 2.�������ӣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ�����û��Ա���IJ��� + select empno,ename,dname + from empc e,dept d + where e.deptno(+)=d.deptno + 4�������� + ͨ�ñ�׼��SQL99�﷨ + 1.�������� + �������Ӻ͵ѿ�����������ģʽ��ͬ�����DZ�1�е�ÿһ�ж����2�е������н���һ������ + select * from emp cross join dept + 2.��Ȼ���� + ��Ȼ���ӵ�һ��������ܹ�ƥ�䵽��ֵ��������ô���ǵ�ֵ���ӵĽ�� + select * from emp natural join dept; --����deptno���е�ֵ���� + ��Ȼ���ӵڶ�����������ܹ�ƥ�䵽��ֵ����������ô��ѯ������ǽ������� + select * from emp natural join salgrade; --û�й�ϵ�������� + 3.�������� + 4.�������� + 5.ȫ������ + +20���Ӳ�ѯ + Ϊ�˸���ѯ�ṩ���ݶ�����ִ�еIJ�ѯ�������Ӳ�ѯ + �Ӳ�ѯ��Ƕ��������SQL����е�SELECT��䣬�󲿷�ʱ�������WHERE�Ӿ��У��Ӳ�ѯǶ�������������ѯ�򸸲�ѯ + �Ӳ�ѯ��Ҫע�⣺ + 1.�Ӳ�ѯ��Ҫд�������� + 2.�Ӳ�ѯ��Ҫд����������Ҷ� + 3.�Ӳ�ѯ����д��where,having,from�Ӿ���; + 4.�Ӳ�ѯ��ͨ����дorder by�Ӿ� + 1���Ӳ�ѯ��WHERE�Ӿ��� + ���ݷ��ؽ���IJ�ͬ���Ӳ�ѯ��Ϊ�����Ӳ�ѯ�������Ӳ�ѯ�������Ӳ�ѯ + 1.�����Ӳ�ѯ + ����һ��һ�����ݣ������Ӳ�ѯҪ��ʹ�õ��в����� > >= < <= <> = + select ename,sal from emp + where sal>(select MAX(sal) + from emp where deptno=20) + ��ѯ��SCOTTͬ���ŵ�Ա����Ϣ + select ename,job + from emp + where deptno=(select deptno from emp where ename='SCOTT') + ����нˮ����������ƽ��нˮ�ߵ�Ա�� + select deptno,ename,sal + from emp + where sal>(select avg(sal) from emp) + 2.�����Ӳ�ѯ + ���ض���һ�����ݣ�ʹ�ö��в����� in all any + select * from emp where deptno + in(select deptno from emp where job='SALESMAN') + and job <> 'SALESMAN' + >ANY�����Ӳ�ѯ���ؽ���е�ij��ֵ�󡣼�������Сֵ + ��ѯ��10�Ų���ij��Ա�����ʸߵ�Ա����Ϣ + select empno,ename,sal + from emp + where sal > any(select sal from emp where deptno=10) + =ANY�����Ӳ�ѯ���ؽ���е�ij��ֵ��� + ��ѯ��10�Ų���ij��Ա��������ȵ�Ա����Ϣ + select empno,ename,sal + from emp + where sal = any(select sal from emp where deptno=10) + ALL�����Ӳ�ѯ���ؽ���е�����ֵ���� + ��ѯ��30�Ų�������Ա�����ʸߵ�Ա����Ϣ + select empno,ename,sal + from emp + where sal > ALL(select sal from emp where deptno=30) + (select min(sal) from emp where deptno=30) + 3���Ӳ�ѯ��select���� + �����ӵ���һ��д�������Ƽ� + ��ѯÿһ��Ա����ͬʱ������Ա���IJ��ű�Ų�ѯ���ŵ����ƣ�����ѯ�������ţ���Ա���IJ�����Ϣ����null + select e.ename,e.sal, + (select d.dname from dept d where d.deptno=e.deptno) dname + from empc e +20����ҳ��ѯ + �ҵ�Ա�����й�����ߵ�ǰ�������������� + select * from emp order by sal desc + select rownum,e.* from emp e order by sal desc + --������Ľ������һ�ű��������ٲ�ѯ + select rownum,t1.* from (select e.* from emp e order by sal desc) t1 + --ֻҪ��ʾǰ������¼ + select rownum,t1.* from (select e.* from emp e order by sal desc) t1 where rownum<4 + ROWNUM:α�У�ϵͳ�Զ����ɵ�һ�У�������ʾ�к� + rownum��ORACLE�����е�������ʾ�кţ�Ĭ��ֵ����ʼֵ��1���ڲ�ѯ�����֮���ټ�1 + ��ѯrownum����2�����м�¼ + select rownum,e.* from emp e where rownum>2--û���κμ�¼ + ��ѯrownum���ڵ���1�����м�¼ + select rownum,e.* from emp e where rownum>=1 + ��ѯrownumС��6�����м�¼ + select rownum,e.* from emp e where rownum<6 + ��ѯ��6~��10����¼ + ��һ�֣� + select * from (select rownum rn,e.* from emp e) t + where t.rn between 6 and 10 + �ڶ��֣� + select t1.* from (select emp.*,rownum rn from emp where rownum <=10) t1 where rn >=6; + ��oracle��ֻ��ʹ���Ӳ�ѯ������ҳ��ѯ + ҳ�룺page + ÿҳ���������pageSize 5�� + ��һҳ��1~5 + �ڶ�ҳ��6~10 + ����ҳ��11~15 + start��(page-1)*pageSize+1 + end��pageSize*page +21��DECODE���� + DECODE(����1,ֵ1,���1,ֵ2,���2...[,Ĭ��ֵ]) + ����1������ֵ����ƥ�䣬���ĸ�ƥ��ɹ�����ʾ��Ӧ�Ľ��������δƥ�䣬����ʾ���һ��Ĭ��ֵ + ��ѯְԱ��������ְԱ��ְλ���㽱������ְλ�ֱ���'MANAGER','SALESMAN','ANALYST'ʱ�� + ������ֱ���нˮ��1.2����1.1����1.05�����������������ְλ���������ȡнˮֵ + select ename,job,sal, + decode(job, + 'MANAGER',sal*1.2 + 'SALESMAN',sal*1.1 + 'ANALYST',sal*1.05 + sal + ) "����" from emp + ��������� + select decode(job, + 'MANAGER','VIP', + 'ANALYST','VIP', + 'OPERATION' + ) from emp group by + decode(job, + 'MANAGER','VIP', + 'ANALYST','VIP', + 'OPERATION' + ) + ���� + select * from emp order by decode(job, + 'MANAGER',1 + 'SALESMAN',2 + 'ANALYST',3 + 4 + ) +22�����Ϻ��� + UNION��������������ѯ����������� + ���еIJ�ѯ������ܲ�������ͬһ�ű� + + ���ʴ���1500������20�Ų����µ�Ա�� + select * from emp where sal>1500 or deptno=20 + --���ʴ���1500������20�Ų����µ�Ա�� + select * from emp where sal>1500 + --20�Ų����µ�Ա�� + select * from emp where deptno=20 + �������㣺union��union all + union��ȥ���ظ��ģ��������� + union all������ȥ���ظ��� + select * from emp where sal>1500 + union + select * from emp where deptno=20 + INTERSECT������ͬʱ����������������е����� + ���ʴ���1500������20�Ų����µ�Ա�� + select * from emp where sal>1500 + INTERSECT + select * from emp where deptno=20 + MINUS������������� + 1981����ְԱ�����������ܲú;����� + --1981����ְԱ�� + select * from emp where to_char(hiredate,'YYYY')='1981' + --�ܲú;��� + select * from emp where where job='PRESIDENT' or job='MANAGER' + + select * from emp where to_char(hiredate,'YYYY')='1981' + MINUS + select * from emp where job='PRESIDENT' or job='MANAGER + ���������е�ע����� + 1.�е�����Ҫһ�� + 2.����˳��д + 3.�е�����Ҫһ�£�������㣬�ÿ�ֵ��� + --�е����Ͳ�ƥ�� + select ename,sal from emp where sal>1500 + union + select sal,ename from emp where deptno=20 + --�е�������ƥ�� + select ename,sal,deptno from emp where sal>1500 + union + select ename,sal from emp where deptno=20 +23��DDL�������� + 1���������ռ䣺 + �߼���λ��ͨ�������½�һ����Ŀ���ͻ��´������ռ䣬�ڱ��ռ��д����û��������� + �﷨�� + create tablespace ���ռ������ + datafile '�ļ���·��' + size ��С + autoextend on �Զ���չ + next ÿ����չ�Ĵ�С + �л���system�˺��´��� + ɾ�����ռ䣺 + drop tablespace YYP + 2�������û��� + create user �û��� + identified by ���� + default tablespace ���ռ������ + ɾ���û� + drop user yuanyipeng cascade; + �����û����� + alter user �û��� identified by ����; + ��Ȩ + grant connect to yuanyipeng + �����ɫ + grant dba to yuanyipeng + select * from scott.emp + ʹ���Ӳ�ѯ�ķ�ʽ�������� + create table ���� as ��ѯ��� + create table emp as select * from scott.emp + ע�⣺ֻ�Ḵ�Ʊ��ṹ�ͱ������ݣ����Ḵ���е�Լ�� + �����ѯ����н�������Ǹ��Ʊ��ṹ������ + �����ѯ���û�н�������Ǹ��Ʊ��ṹ + ֻ���Ʊ����ݣ�������������û��values�ؼ��֣� + insert into ���� ��ѯ��䣨���Ľṹ�����б��ṹһ�£� + 3���е�Լ����Լ����Ҫ������Լ���������ݵĹ��� + ����Լ����primary key ����Ϊ�գ�����Ψһ + �ǿ�Լ����not null + ΨһԼ����unique + ���Լ����check(����)��mysql���ǿ���д�ģ�����mysqlֱ�Ӻ����˼��Լ�� + ���Լ������Ҫ������Լ���ӱ�A�еļ�¼�������Ǵ���������B��(��֤����������) + create table books( + book_id number(10) primary key, + book_name varchar2(20) not null, + price number(10,2), + cid number(10) not null references category(id)--���Լ�� + ); + create table books( + book_id number(10) primary key, + book_name varchar2(20) not null, + price number(10,2), + cid number(10) not null, + foreign key(cid) references category(id)--���Լ�� + ); + + create table category( + id number(10) primary key, + cname varchar2(10) + ); + alter table books add foreign key(cid) references category(id) +24����ͼ + ���ݿ����֮һ����ͼ��sql��������ֵĽ�ɫ�ͱ���ȫ��ͬ��������ͼ������һ����ʵ���ڵı�������Ӧ����һ + ����ѯ���Ľ��������ͼ������������һ���� + ��ͼ�����ã��򻯸��Ӳ�ѯ���������ݷ��� + 1����������ͼ�������� + ����Ȩ�ޣ�ʹ��system�û���Ȩ + ��ͼ��Ȩ��䣺 + grant create view to �û���; + ����һ������ͼv_emp_10����ʾ����10�е�Ա���ı��롢������нˮ + create view v_emp_10 + as + select empno,ename,sal,deptno + from emp where deptno=10; + 2���޸���ͼ + �޸���ͼ�����滻����ͼ���Ӳ�ѯ��ʹ��create or replace view ��ͼ�����ɣ�����ͼ�����ھʹ��������ھ��滻 + ������ͼʱ�����и������ + create or replace view v_emp_10 + as + select empno id,ename name,sal salary,deptno + from emp where deptno=10; + 3����ѯ��ͼ + select * from v_emp_10 + ��ͼ�������ʹ�����ͼʱ������һ�£���һ����ԭ����; + select id,name,salary from v_emp_10 + 4����ͼ��DML���� + ����ͼ����DML�������Ƕ���ͼ������Դ�Ļ��������в��� + �������ݣ� + 1.ע����ͼ�п��������ֶζ�������Ĭ��ֵ�����Բ���Υ������������Ӧ�ֶε�Լ�������������ǿ������� + �ֶε�not null����������ʧ�� + 2.Ϊ��ͼ���Ӽ��ѡ����Ա������ͼ����DML����ʱ��Ⱦ���������ڴ��������with check option + 3.���û������ͼ��ִ��DML�����ı�Ҫ���ڽ�����ͼʱ����Ϊֻ�������������������֤��ͼ��Ӧ�Ļ����� + ���ݲ��ᱻ�Ƿ��޸�with read only + 5����ͼ�����Ӳ�ѯ�IJ�ͬ����Ϊ����ͼ�͸�����ͼ�����Ӳ�ѯ�а������к���������ʽ����麯��ʱ������ͼ�� + һ��������ͼ��������ͼ���ܽ���DML����������Ϊ�Ӳ�ѯ�еı���ʽ����������� + ����һ����ͼv_emp_salary����ְԱ�������ݰ����ŷ��飬���ÿ�����ŵ�ƽ��нˮ��нˮ�ܺ͡����нˮ + create view v_emp_salary + as + select d.dname,avg(sal) avg_sal,sum(sal) sum_sal,min(sal) min_sal from emp e,dept d + where e.deptno=d.deptno group by d.dname + 6��ɾ����ͼ + DROP VIEW ��ͼ���� +25�����У�sequence ��������Ψһ����ֵ�����ݿ����ͨ����Ϊ��������ֵ + �����Ƕ��������ݿ�������в��������ڱ� + 1���������� + create sequence ������ + start with xx ��ʼ���� + increment by xx ������ÿ�����Ӽ��� + maxvalue xx ���ֵ + minvalue xx ��Сֵ + + create sequence users_seq + start with 100 + increment by 10 + + ������������α�� + ---nextval����ȡ���е��¸�ֵ + ---currval: ��ȡ���еĵ�ǰֵ + �����д����Ժ󣬱�����ִ��һ��nextval��֮�����ʹ��currval + ��ȡ���еĵ�һ��ֵ������ʹ������ֵΪusers�������µļ�¼ + select users_seq.nextval from dual; + insert into users(id,name) values(users_seq.nextval,'����÷') + 2��ɾ�����У�drop sequence users_seq +26��������һ����߲�ѯЧ�ʵĻ��� + ������Ϊ����������ȥ�أ�����������������ֶο���Ϊ���������� + 1������������ + create [unique] index �������� + on ����(����) + ���ӵ������� + ��users����name���Ͻ������� + create index idx_users_name on users(name); + ���Ӷ������� + create index idx_users_name_salary on users(name,salary); + ��ѯ�������Զ�Ӧ������������Ҫ�����ƶ�����߲�ѯ�ٶ� + select * from users order by name,salary + 2���޸Ļ�ɾ������ + �����������������ִ��DML��������Ҫ�����ؽ���������������Ŀռ������� + alter index �������� rebuild + ɾ��������drop index �������� \ No newline at end of file diff --git a/JDBC/bin/note/sql/SQL.sql b/JDBC/bin/note/sql/SQL.sql new file mode 100644 index 0000000..f831846 --- /dev/null +++ b/JDBC/bin/note/sql/SQL.sql @@ -0,0 +1,460 @@ +--SQL.day.02 +--������ +CREATE TABLE users( + id number(10) primary key,--���� + name varchar2(20) unique,--Ψһ��ʶ + salary number(10,2), + create_time date default sysdate +); + +--�鿴���ṹ(��command window���ã�SQL window����ʹ�ã� +desc ���� + +--�޸ı��� +RENAME users to userplus + +--����������ֶ� +ALTER TABLE userplus ADD(school varchar2(20)) +ALTER TABLE userplus ADD(job varchar2(10)) + +--ɾ�����������ֶ� +ALTER TABLE userplus DROP(school) + +--�޸ı��������ֶεĸ�ʽ +ALTER TABLE userplus MODIFY(salary number(10)) +ALTER TABLE userplus MODIFY(username varchar2(20) unique) + +--�޸ı����ֶ����� +ALTER TABLE userplus RENAME column name to username + +--������������� + --ȫ�ֶβ������� +INSERT INTO userplus VALUES(1, '����', 8000,sysdate, '����Ա'); +INSERT INTO userplus VALUES(2, '����', 8000,default, '����Ա');--��Ϊ������default sysdate�������������ֱ��дdefault +INSERT INTO userplus VALUES(3, '����', 8000,to_date('2018-12-01','YYYY-MM-DD'), '����Ա'); + --�����ֶβ��� +INSERT INTO userplus(id, username, job) VALUES(4,'����','��Ʒ����'); +INSERT INTO userplus(id, username, job, salary) VALUES(5,'����','��Ʒ����'��5000) + +--���±����������� +UPDATE userplus SET salary=salary*10, job='�ܾ���' WHERE id=1; + +--ɾ���������� +DELETE FROM userplus WHERE salary>5000; + +--�ύ���� +commit; + +--�ع� +rollback; + +--��ѯ������������ +SELECT * FROM userplus; + +--��ѯ����������Ϣ +SELECT job, salary FROM userplus; +SELECT * FROM userplus WHERE job <> '�ܹ�ʦ'; +SELECT salary*12 "��н" FROM userplus WHERE username='����';--���� +SELECT salary*12 AS "��н" FROM userplus WHERE username='����';--���� + +--10.(1)�ַ������� + --�����ַ��� +SELECT CONCAT(ename, sal) from EMP; +SELECT CONCAT(CONCAT(ename, ': ') ,sal)from EMP; +SELECT ename||': '||sal as "����������" from EMP; + --��ʾ�ַ������� +SELECT LENGTH(ename) from EMP; + --ת��д +SELECT UPPER(ename) from EMP; + --ȥ���ַ���ǰ��ָ������ +SELECT TRIM('E' from 'EEHELLO WORLDEEE') from dual; +SELECT ename, trim('A' FROM ename) from emp; --ȥ��Ա������ǰ�������'A' +SELECT LTRIM('EEHELLO WORLDEEE', 'E') from dual;--ȥ��EEHELLO��ǰ���E +SELECT ename, ltrim(ename,'A') from emp;--ȥ��Ա������ǰ�������'A' + --��λ���� +select * from emp +select ename, LPAD(sal, 6, 0) AS salary from emp + --substr��ȡ�ַ��� +select substr('helloworldqqqqq',6,10) from dual; + --INSTR�ַ������� +select instr('thinking in java','in',4,2) from dual; + +--10.(2)���ֺ��� + --��ȡ�������������� +select round(321.123,1) from dual; + --��ȡ������������ +select trunc(45.678,2), trunc(45.678,0), trunc(45.678), trunc(45.678,-1) from dual; + --ȡ���� +select ename, sal, mod(sal,1000) from emp; + --����ȡ�� +select ceil(45.678) from dual; + --����ȡ�� +select floor(45.678) from dual; +--10.(3)���ں��� + --ϵͳʱ�䣬��ȷ���� +select sysdate from dual; + --ʱ�������ȷ������ +select systimestamp from dual; + --TO_DATE(),����ת������ +select to_date('2018-12-01 11:30','YYYY-MM-DD HH:MI:SS') from dual; +select to_date('2018-12-01 13:30','YYYY-MM-DD HH24:MI:SS') from dual; + --TO_CHAR(),��DATE���ո��������ڸ�ʽת��Ϊ�ַ��� +select ename, to_char(hiredate,'YYYY-MM-DD HH:MI:SS') from emp; +select ename, to_char(hiredate,'YYYY"��"MM"��"DD"��"') from emp; + --LAST_DAY(date),����ָ�����������µ����һ�� +select last_day(sysdate) from dual; +select last_day(to_date('2018-12-01 11:30','YYYY-MM-DD HH:MI:SS')) from dual; + --ADD_MONTHS(date,i),����+-i����֮������� +select ename, hiredate, add_months(hiredate, 12*20) AS "20����" from emp; + --MONTHS_BETWEEN(date1,date2):������������֮�������ٸ��� +select ename,trunc(months_between(sysdate, hiredate)) from emp; + --date�������������ݿ��п��Խ��м������������������������ +select ename, sysdate-hiredate from emp; + --LEAST(date1,date2):��ʾ����ʱ���е���Сֵ���������ͱ���һ�¡� +select least(sysdate,to_date('2018/12/09','YYYY-MM-DD')) from dual; + --GREATEST(date1,date2):��ʾ����ʱ���е����ֵ���������ͱ���һ�¡� +select greatest(sysdate,to_date('2018/12/09','YYYY-MM-DD')) from dual; + --EXTRACT(date from datetime):�Ӳ���datetime����ȡ����dateָ�������ݣ�������ȡ���ꡢ�¡��ա� +select extract(year from sysdate),extract(month from sysdate),extract(day from sysdate) from dual; +select extract(hour from systimestamp),extract(minute from systimestamp),extract(second from systimestamp) from dual; +select ename from emp where extract(year from hiredate) = 1980; + +--10.(4)��ֵ������ +select * from emp where comm is not null and comm != 0; + --NVL(arg1,arg2):��nullת��Ϊ��nullֵ�����arg1Ϊnull������arg2�����򷵻ر����� +select ename,nvl(comm,0) from emp; +select * from emp where nvl(comm,0)>0; + --NVL(arg1,arg2),���arg1Ϊnull������arg2�����򷵻ر���������Ա�������루�¹��ʼӽ��𣨽������Ϊnull���� +select ename, sal, comm, sal+nvl(comm,0) as "������" from emp; + --NVL2(arg,res1,res2):���argΪnull���򷵻�res2,arg��Ϊnull�򷵻�res1. +select ename, sal, comm, nvl2(comm,comm+sal,sal) as"������" from emp; + +--11.������ѯ���: + --��ѯ����10�µ�Ա����Ϣ�� +select * from emp where deptno = 10; + --��ѯԱ������ְλ��'SALESMAN'��Ա����Ϣ +select * from emp where job = 'SALESMAN'; + +--12.��ѯ����: + --��ѯְԱ����нˮ����2000��ְԱ��Ϣ +select ename, sal from emp where sal < 2000; + --��ѯְԱ���в����ڲ���10��Ա����Ϣ +select * from emp where deptno <>10; + --��ѯְԱ������1981��1��1���Ժ���ְ��ְԱ��Ϣ +select * from emp where hiredate > to_date('1981/01/01','YYYY/MM/DD'); + --��ѯнˮ����1000������ְλ��'CLERK'��ְԱ��Ϣ +select * from emp where sal>1000 and job='CLERK'; + --��ѯнˮ����1000������ְλ��'CLERK'��ְԱ��Ϣ +select * from emp where sal>1000 or job='CLERK'; + --NVL(arg1,arg2)��NVL2(arg,res1,res2) + --��ѯнˮ����1000�����ҽ���Ϊnull��ְԱ��Ϣ +select * from emp where sal>1000 and comm>0; +select * from emp where sal>1000 and nvl(comm,0)>0; +select * from emp where (deptno=20 or deptno=30) and sal>800; + --ģ����ѯ��LIKE + --��ѯԱ������������'A'��ͷ��Ա����Ϣ +select * from emp where ename like 'ALLEN'; + --��ѯԱ�����������ڶ�����ĸ��'A'��Ա����Ϣ +select * from emp where ename like '_A%'; + --IN��NOT IN�� + --��ѯְλ�Ǿ�������ְԱ��Ա����Ϣ +select * from emp where job in ('MANAGER','CLERK'); + --��ѯ���Ų���10��20��Ա�� +select * from emp where deptno not in (10,20); +select * from emp where deptno <> all(10,20); + --BETWEEN...AND...:��������ѯ����ij��ֵ��Χ���������ݡ� + --��ѯнˮ��1500~3000֮���ְԱ��Ϣ +select * from emp where sal between 1500 and 3000;--[1500,3000] + --��ѯ�Ⱦ�����͹��ʸߵ�Ա����Ϣ�� +select * from emp where sal > any(select sal from emp where job='MANAGER'); + --��ѯԱ���IJ��ű�� +select distinct(deptno) from emp; +select distinct deptno,job from emp; + +--14.ORDER BY ����(д�������Ӿ����) +select ename, sal from emp order by sal; +select ename, sal from emp order by sal asc; +select * from emp where sal>2000 order by mgr desc; + --�������򣬶�Ա�����е�ְԱ�����Ȱ��ղ��ű���������У��ٰ���нˮ�������С� +select ename, deptno, sal from emp order by deptno asc, sal desc; + +--15.�ۺϺ��� + --MAX��MIN +select deptno,max(sal),min(sal) from emp group by deptno; +select max(hiredate),min(hiredate) from emp; +select ename from emp a where hiredate = (select max(hiredate) from emp); + --ERROR: +select ename from emp a where hiredate = (select max(hiredate),min(hiredate) from emp); + --RIGHT: +select ename from emp where hiredate in ((select min(hiredate) from emp), (select max(hiredate) from emp)); + --AVG��SUM +select avg(sal),sum(sal) from emp; +select avg(comm),sum(comm) from emp;--����nullֵ + --COUNT +select count(*) from emp; +select count(comm) from emp;--����nullֵ +select count(*) from emp where comm is not null; + --��ϣ���ۺϺ�������NULLֵʱ�� +select avg(nvl(comm,0)) from emp; + +--16.GROUP BY +select max(sal),min(sal),avg(sal) from emp group by deptno; +select deptno,max(sal),min(sal),avg(sal) from emp group by deptno; +select deptno,ename,max(sal),min(sal),avg(sal) from emp group by deptno,ename; + --where���治��ʹ�þۺϺ���,����ִ��. +select max(sal),deptno from emp where max(sal)>4000 group by deptno; + --��ѯ����������ʳ���4000�IJ����Լ�����ʡ� +select max(sal),deptno from emp group by deptno having max(sal)>4000; + --�鿴���ű�ţ��Լ��ò��ŵ�ƽ�����ʣ�Ҫ���Ǹñ��ŵ�ƽ�����ʳ���2000��ְλ��PRESIDENT�IJ��ܲ���ͳ��) +select deptno, avg(sal) from emp where job<>'PRESIDENT' group by deptno having avg(sal)>2000; + +--19.������ѯ + --��ѯÿ��Ա�����ڲ������� +select ename, dname from emp, dept where emp.deptno=dept.deptno; + --�鿴Ա�����֡����ʡ��������� +select ename, sal, dname from emp e, dept d where e.deptno=d.deptno and loc='CHICAGO'; + --KING�����﹤���Լ����IJ��ű���Ƕ��٣� +select ename, e.deptno, loc from emp e, dept d where e.deptno=d.deptno and ename='KING'; + +--20.�������� + --������ +select ename, dname from emp inner join dept on emp.deptno=dept.deptno; + --������ + --���ٴ�һ�����и������� +create table empc as select * from emp; + --��empc���в���һ�����ݣ����ź�Ϊ50 +insert into empc(empno, ename, deptno)values(1000,'JACK',50); +select * from empc; + --�������ӣ���ѯԱ���ı�š��������Լ��������ƣ������������κβ��ŵ�Ա���� +select empno, ename, dname, empc.deptno from empc left outer join dept on empc.deptno=dept.deptno; + --��������,��ѯԱ���ı�š��������Լ��������ƣ������κ�û��Ա���IJ��š� +select empno, ename, dname, c.deptno from empc c right outer join dept d on c.deptno=d.deptno; + --ȫ�����ӣ���ѯԱ���ı�š��������Լ��������ƣ������������κβ��ŵ�Ա����Ҳ�����κ�û��Ա���IJ��š� +select empno, ename, dname, c.deptno from empc c full outer join dept d on c.deptno=d.deptno; + --�����ӣ���ѯÿ��Ա�����ϼ��쵼 +select e.ename, m.ename from emp e, emp m where e.mgr=m.empno; +select e.ename emp, m.ename mgr from emp e left outer join emp m on e.mgr=m.empno; + --��ѯSMITH����˾���ĸ����й��� + --��ֵ���� +select e.ename emp, m.ename mgr, d.loc mgrloc +from emp e, emp m, dept d +where e.mgr=m.empno and m.deptno=d.deptno and e.ename='SMITH'; + --������ +select e.ename emp, m.ename mgr, d.loc mgrloc +from emp e inner join emp m +on e.mgr=m.empno and e.ename='SMITH' +inner join dept d +on m.deptno=d.deptno; + +--(�����ӵ���һ��д��):��ѯÿһ��Ա����ͬʱ������Ա���IJ��ű�Ų�ѯ���ŵ���Ϣ������ѯ�������ţ���Ա���IJ�����Ϣ����null +select e.ename, e.sal, e.deptno from empc e;--ERROR + --Solution 1. +select e.ename, e.sal, +(select d.deptno from dept d where d.deptno=e.deptno) deptno +from empc e;--RIGHT + --Solution 2.������������д����Ϊ����Ҫ��Ҫд��d.deptno +select e.ename, e.sal,d.deptno +from empc e left outer join dept d +on e.deptno=d.deptno; + --Solution 3.�����ű�ʱ(���Ӳ������ű�)�����ַ����Ƚϼ�һЩ +select e.ename, e.sal, +(select d.dname from dept d where d.deptno=e.deptno) dname, +(select d.loc from dept d where d.deptno=e.deptno) loc +from empc e; + +--21.�Ӳ�ѯ + --WHERE: + --�����Ӳ�ѯ����ѯ���ڲ��ź�20����߹��ʵ�Ա����Ϣ +select ename, sal from emp where sal>(select max(sal) from emp where deptno=20); + --�����Ӳ�ѯ����ѯ��SCOTTͬ���ŵ�Ա����Ϣ +select ename, job from emp where deptno=(select deptno from emp where ename='SCOTT') + --�����Ӳ�ѯ������нˮ����������ƽ��нˮ�ߵ�Ա�� +select ename, job, sal from emp where sal>(select avg(sal) from emp); + --�����Ӳ�ѯ����ѯ������SALESMAN��ְλ����SALESMAN��Ա������Ϣ�� +select * from emp where deptno in (select deptno from emp where job='SALESMAN') and job<>'SALESMAN'; + --�����Ӳ�ѯ����ѯ��10�Ų���ij��Ա��������ȵ�Ա����Ϣ +select * from emp where sal = any (select sal from emp where deptno = 10); + --�����Ӳ�ѯ��?????????????????????????????????????????????????????????????????????????? + --EXISTS�ؼ��֣��г�����Щ��Ա���IJ�����Ϣ +select deptno, dname from dept d where EXISTS (select * from emp e where e.deptno=d.deptno); + --HAVING: + --��ѯ��͹��ʸ��ڲ���30����͹��ʵIJ�����Ϣ +select min(sal) from emp group by deptno having min(sal)<(select min(sal) from emp where deptno=30); + +--22.��ҳ��ѯ + --��ѯrownum����2�����м�¼ +select rownum, e.* from emp e where rownum>2;--ERROR����Ϊû���κμ�¼�� +select rownum, e.* from emp e where rownum>=1; +select rownum, e.* from emp e where rownum<6; + --�ҵ�Ա�����й�����ߵ�ǰ�������������򣩣� + --ERROR +select rownum, e.* from emp e order by sal desc where rownum<=3; + --RIGHT +select * from emp order by sal desc;--�Ƚ����ٰ�������һ�ű�����ȥѡrownumС�ڵ���3���С� +select rownum, t1.* from (select * from emp order by sal desc) t1 where rownum <=3; + --��ѯ��6~10����¼(���ַ����� +select * from (select rownum rn, e.* from emp e) t where t.rn between 6 and 10; +select t1.* from (select rownum rn, emp.* from emp where rownum<=10) t1 where rn between 6 and 10; + --���ʽ����ѡ6~10����¼��ԭ���Ƚ����ټ�α�У��ٵ��±�ѡ��Χ�� +select * from (select rownum rn, e.* from (select * from emp order by sal desc) e) t where t.rn between 6 and 10; + +--23.DECODE(����1��ֵ1�����1��ֵ2�����2...[,Ĭ��ֵ]); + --��ѯְԱ��������ְԱ��ְλ���㽱������ְλ�ֱ���'MANAGER','SALESMAN','ANALYST'ʱ��������ֱ���нˮ��1.2,1.1,1.05��������Ϊԭ����нˮֵ�� +select ename, job, sal, decode(job,'MANAGER',sal*1.2,'SALESMAN',sal*1.1,'ANALYST',sal*1.05,sal) "����" from emp; +select decode(job,'MANAGER','VIP','ANALYST','VIP','OPERATION') from emp; + --������������������ +select decode(job,'MANAGER','VIP','ANALYST','VIP','OPERATION') job, count(*) from emp group by decode(job,'MANAGER','VIP','ANALYST','VIP','OPERATION'); + --�Զ������� +select * from emp order by decode(job,'MANAGER','1','SALESMAN','2','ANALYST','3'); + +--24 + --union + --���ʴ���1500��20�Ų����µ�Ա�� +select * from emp where sal>1500 or deptno=20; + --���ʴ���1500�� +select * from emp where sal>1500;--7����¼ + --20�Ų����µ�Ա�� +select * from emp where deptno=20;--5����¼ + --�������㣺union(�����ظ����ݣ�9����¼��������), union all(�������ظ����ݣ�12����¼��������) +select * from emp where sal>1500 union select * from emp where deptno=20;--����֮���ܹ�9����¼ + --INTERSECT������ +select * from emp where sal>1500 INTERSECT select * from emp where deptno=20; + --MINUS��������������� + --1981����ְԱ��(�������ܲú;���) +select * from emp where to_char(hiredate,'YYYY')='1981'; +select * from emp where job in ('PRESIDENT','MANAGER'); +select * from emp where to_char(hiredate,'YYYY')='1981' MINUS select * from emp where job in ('PRESIDENT','MANAGER'); + --ע����� +select empno,ename from emp where to_char(hiredate,'YYYY')='1981' MINUS select null,ename from emp where job in ('PRESIDENT','MANAGER'); + +--25.DDL���������� +CREATE tablespace YYP datafile 'e:/YYP.dbf' size 100m autoextend on next 10m;--���� +drop tablespace YYP;--ɾ����ȡ�����ļ��Ĺ���������ɾ���ļ�����ȡ������֮�󣬿����ֶ�ɾ������ +create user zicheng identified by qqq default tablespace YYP;--�����û� +drop user zicheng cascade;--ɾ���û� +alter user zicheng identified by eee;--�޸����� +grant connect to zicheng;--��Ȩ +grant dba to zicheng;--�����ɫ + --ʹ���Ӳ�ѯ�ķ�ʽ�������� +create table emp as select * from scott.emp;--ֻ���Ʊ��ṹ�ͱ����ݣ����Ḵ���е�Լ���� +create table dept as select * from scott.dept where 1=2;--ֻ���Ʊ��ṹ��������,��Ϊ1=2Ϊfalse�� +insert into dept select * from scott.dept;--ֻ���Ʊ�����,��ǰ���DZ��Ľṹ�����б��ṹһ�¡� +select * from dept; + +--26.�е�Լ�� + --���Լ�� +CREATE TABLE users( + id number(10) primary key,--���� + name varchar2(20) unique,--Ψһ��ʶ + sex varchar2(2) check(sex in ('��' ,'Ů')),--���Լ�� + salary number(10,2) check(salary>3000), + create_time date default sysdate +); +insert into users values(1,'admin','��',3500,default); + --���Լ�� +create table category( + id number(10) primary key, + cname varchar2(10) +); +create table books( + book_id number(10) primary key, + book_name varchar2(20) not null, + price number(10,2) check(price>100),--���Լ�� + cid number(10) not null references category(id)--���Լ�� + --foreign key(cid) references category(id)--��һ�����Լ���ķ�ʽ +); + --���Բ��ڶ�����н������Լ���������ô����Ҳ��ʵ�����Լ���� +alter table books add foreign key(cid) references category(id); + --���ӣ� +insert into category values(1,'С˵'); +insert into category values(2,'��ѧ'); +insert into category values(3,'����'); +select * from category; +insert into books values(1,'���μ�',101,1); +select * from books; + --����ɾ���������Ϊ�ӱ�������ص��ѽ������������Լ�� +delete from category where id=1; +delete from books where cid=1;--ɾ��������������ַ��� +update books set cid=null where cid=1;--Ȼ����ɾ�������е����ݾ�ok�� + +--27.��ͼ + --����һ������ͼv_emp_10����ʾ����10�е�Ա���ı��롢������нˮ�� +create view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10; + --�޸���ͼ�����滻����ͼ���Ӳ�ѯ��ʹ��create or replace view ��ͼ�� ���ɣ�����ͼ�����ھʹ��������ھ��滻��������ͼʱ�����Ը��и�������� +create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10; + --��ѯ��ͼ +select * from v_emp_10; + --��ͼ��DML����,���޸�ԭ�������� +insert into v_emp_10 values(1,'Tony',8888,50) +select * from emp; + --with check option +create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10 with check option + --with read onlyֻ�������ܶ���ͼ����DML������ +create or replace view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10 with read only; + --����һ����ͼv_emp_salary,��ְԱ���е����ݰ����ŷ��飬���ÿ�����ŵ�ƽ��нˮ��нˮ�ܺ͡����нˮ�� +create view v_emp_salary as select d.dname, avg(sal) avg_sal,sum(sal) sum_sal,min(sal) min_sal,max(sal) max_sal from emp e, dept d where e.deptno=d.deptno group by d.deptno, d.dname; +select * from v_emp_salary; + --drop view ��ͼ���ƣ�ɾ����ͼ +drop view v_emp_slary; + +--28.���� + --�������������С����������ɾ������ +CREATE TABLE users( + id number(10) primary key,--���� + name varchar2(20), --unique,--Ψһ��ʶ + salary number(10,2), + create_time date default sysdate +); + --�������У�create sequence ������ start with **(��ʼ����) increment by **(����,ÿ�����Ӽ��������ɸ�) maxvalue **(���ֵ) / minvalue **(��Сֵ); +create sequence users_seq start with 100 increment by 10;--��������,��һ��һ��Ҫ��nextval��������currval +insert into users(id,name)values(users_seq.nextval,'����÷');--������� +select users_seq.currval from dual;--�鿴��ǰ����ֵ�� +drop sequence users_seq;--ɾ������ + --ѭ������ +begin + for i in 1..10000 + loop + insert into users(id,name,salary) values( users_seq.nextval,'˲�䱬ը',8888); + end loop; + commit; +end; + +--29.���� + --����������create [unique] index �������� on ����(����) +create index idx_users_name on users(name);--��users����ename���Ͻ������� + --���Ӷ��������� +create index idx_users_salary on users(name,salary); + --��ѯ�������Զ�Ӧ������������Ҫ����ȥ�ƶ�����߲�ѯ�ٶȡ� +select users.* from users order by name, salary; + --�ع�������������ƽ����������޸ı�������ع�һ��������ƽ��������� +alter index �������� rebuild;--�����޸����ݵı��������齨�������� + --ɾ�������� +drop index idx_users_salary; + + + + + + + + + +--��ϰ +create table student( + id number(10) not null, + name varchar2(20), + hobby varchar2(100), + school varchar2(20), + create_time date +); +--�޸ı���student_t +rename student to student_t; +--�����ֶΣ���Ŀproject���ɼ�score +alter table student_t add(project varchar2(20), score number(3)); +--ɾ��ѧУ�ֶ� +alter table student_t drop(school); +--�޸��ֶθ�ʽ +alter table student_t modify(create_time varchar2(20) default sysdate); +--�޸ı����ֶ����ƣ�name��Ϊstu_name +alter table student_t rename column name to stu_name; + diff --git a/JDBC/bin/note/sql/SQLNote.sql b/JDBC/bin/note/sql/SQLNote.sql new file mode 100644 index 0000000..a18543f --- /dev/null +++ b/JDBC/bin/note/sql/SQLNote.sql @@ -0,0 +1,422 @@ +���ݿ�//297��308,415 +һ�� ʹ��sqlplus�������ݿ� + 1. ��ʽ�� + 1�� sqlplus���س�������û��������� + 2�� sqlplus/nolog ------conn scott/tiger + 3�� ʹ�ÿ��ӻ����棬developer��ע�⣺ʹ�ô������ӿ��ӻ�����ȴ�������ӣ�������޷�����޼���������Ҫ�������ü�������a.�رշ��� b.�������ü����ļ��޸�HOSTΪ��ǰIP�����޸�Ϊlocalhost����127.0.0.1������������ c.�ٿ��������������ӵ�½���ɡ��� + 2. �����ע����� + 1�� ����sql����ԡ�;����β���س�ִ�С� + 2�� sql��䲻���ִ�Сд���ַ������⡣ + 3. Oracle�û���oracle��װ����Զ�����4���˻����� + 1�� sys�û��������û����������Ȩ�ޣ�����sysdba�����ݿ����Ա����ɫ���������ֵ����ͼȨ�ޣ��ܴ������ݿ⣬Ĭ������manager�� + 2�� system�û�������Ա�������ڳ����û�������sysoper(ϵͳ����Ա)��ɫ�����ܴ����⡣ + 4. DB��Database���ݿ⣬��ϵ�����ݿ⣬�������ݽṹ���������ݵIJֿ⣬�Զ�ά��(�У��ֶΣ� �У�����)����ʽ�洢���ݡ� + DBMS�����ݿ����ϵͳ��Oracle, mysql, sqlserver���� +���� sql��� + 1. SQL ��Structed Query Language,�ṹ����ѯ���ԣ� + 2. SQL���ࣺ + DDL�����ݶ������ԣ����ڲ������ݶ��󣨱�����ͼ�����У��������� + CREATE������������������ + ALTER���޸ı�����������ṹ�� + DROP��ɾ�������������� + TRUNCATE��ɾ�������ݣ��������ṹ����ձ��IJ����� + DML�����ݲ������ԣ�ͨ����Ҫ����TCL����������ɾ�ı��е����ݡ� + INSERT INTO�������ݲ�����С� + DELETE��ɾ���������ݡ� + UPDATE SET�����±������ݡ� + TCL������������ԡ� + COMMIT���ύ�� + ROLLBACK���ع��� + DQL�����ݲ�ѯ���� + SELECT����ѯ���ݡ� + DCL�����ݿ������ԡ� + GRANT������Ȩ�ޡ� + grant��create��view��to��scott; + 3. �������ͣ� + char(n)�������ַ�����n���ֽ��������2000�ֽڡ� + varchar2(n):�䳤�ַ�����n���ֽ��������4000�ֽڡ� + number(n,m):n������λ���֣�m�����м�λС���� + date:ʱ�����ڣ�select sysdate from dual; +����SQL��DDL�� DML��TCL��DQL�� +�����Ĺ��ɣ� + 1.ǰ�ˣ�HTML��CSS��JS��JQuery + 2.��̨��Java + 3.���ݿ⣺Oracle�� Mysql�����׹����չ��� + +1.��װOracle + + �����û��� + sys�������û� �������Ȩ�� ����sysdba��ɫ �����ݿ����Ա�� + �д������ݿ��Ȩ�� + system����������Ա Ȩ�޽����ڳ�������Ա + + scott:��ͨ�û� һ��������ϰ scott -- tiger + �����û���alter user scott account unlock + �������룺alter user scott identified by tiger + +2.��¼��ʽ + 1��sqlplus �س� + �����û��� + �������� + + 2��sqlplus/nolog + conn scott/tiger + +3. DB��DataBase �������ݽṹ���������ݵIJֿ�(��ϵ�����ݿ�),�Զ�ά����ʽ�洢����(�����У��ֶ��� �����У�����) + DBMS�����ݿ����ϵͳ(mysql��oracle��SqlServer) + +4.�������� + sqlplus �����ݿ� + conn �û���/���� �������ݿ� + show user; ��ʾ��ǰ�û� + exit; �Ͽ����� + quit; �Ͽ����� + +5.SQL:�ṹ����ѯ���� + SQL�ķ��ࣺ + DDL ���ݶ������� ���ڲ������ݶ��󣨱� ��ͼ ���� ������ + DML ���ݲ������� ������ɾ�ı��е�����(ͨ����������) + TCL ����������� �������ݵ��ύ�ͳ��� + DQL ���ݲ�ѯ���� ���ڲ�ѯ���ݼ�¼ + DCL ���ݿ������� ����ִ��Ȩ�޷��� + + DDL�����ݶ������� + ---���ڲ������ݿ���󣨱� ��ͼ ���� ������ + CREATE���������������������� + ALTER���޸ı�����������Ľṹ + DROP��ɾ��������������Ľṹ + TRUNCATE����ձ�(ɾ���������ݣ��������ṹ) + DML�����ݲ������� + ---������ɾ�ı��е�����(ͨ�����������������) + INSERT INTO�������ݲ��뵽���� + DELETE��ɾ���������� ---- WHERE + UPDATE ���� SET�����±������� --- WHERE + + TCL������������� + COMMIT���ύ--���� + ROLLBACK���ع�---���� + + DQL�����ݲ�ѯ���� + SELECT ��ѯ***** + + DCL�����ݿ������� + ����ִ��Ȩ�޷��� GRANT REVOKE +6.DDL ���ݶ������� + �鿴�û�����ı��� + SELECT table_name FROM user_tables;--�û���������б����ᱻ�г��� + (1)������ + CREATE TABLE ���� ( + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��] + ); + CREATE TABLE users( + id number(10) primary key, + name varchar2(20) not null unique, + salary number(10,2), + create_time date default sysdate + ); + id(����) �ص㣺���������ű���ÿһ���������ֵ������ͬ���ұ�����ֵ���ǿ���Ψһ�� + �������� + ������������ + 1����������ĸ��ͷ + 2��������1-30���ַ�֮�� + 3������ֻ�ܰ���A-Z,a-z,0-9,_,$,��# + 4�����벻�ܺ��û������������������ + 5�����벻����Oracle�ı����� + ��������: + 1��NUMBER:��ֵ���� + NUMBER(X):x����λ�� + NUMBER(3) ����ܴ��3λ�� + NUMBER(X,Y):x��ʾ����λ y��ʾ�����м�λ��С�� + NUMBER(6,2) 8888.88 + 2��CHAR:�����ַ��� + CHAR(X):X��ʾ��ռ�����ֽ� ����2000�ֽ� + 3��VARCHAR2:�䳤�ַ��� + VARCHAR2(X):X��ʾ��ռ�����ֽ�(�������б�������ݳ��ȣ�ռ�õĿռ��DZ仯��) ���4000�ֽ� + 4��DATE ʱ������ SELECT SYSDATE FROM DUAL; + α����dualȷʵ��һ�ű�����һ��ֻ��һ��һ�еı���ϰ���ϳ�Ϊα������Ϊ�����洢��������ݡ� + �鿴���ṹ(��command window)��DESC table_name; + (2)ɾ������ + DROP TABLE users;--ɾ�������ݺͽṹ + TRUNCATE TABLE users;--ɾ�������ݣ��������ͱ��ṹ + (3)�޸ı�(ALTER): + �޸ı�����RENAME old_name TO new_name + �޸ı��ṹ�� + ����������ֶΣ�ֻ�����ӵ���󣬲��ܲ��뵽���е������У���ALTER TABLE table_name ADD(�ֶ�������������[Ĭ��ֵ Լ��]), ���磺ALTER TABLE usersplus ADD(school varchar2(20)) + ɾ�����������ֶΣ� ALTER TABLE biaoming DROP(�ֶ����������磺ALTER TABLE userplus DROP(school�� + �޸ı��������ֶεĸ�ʽ��ALTER TABLE ���� MODIFY(�ֶ�������������[Ĭ��ֵ Լ��])�����磺ALTER TABLE userplus MODIFY(salary number(10)) + �޸ı����ֶ����ƣ�ALTER TABLE userplus RENAME column name to username +7.DML ���ݲ������� + (1)������������ݣ� + ȫ�ֶβ������ݣ�INSERT INTO ���� VALUES(1, '����', 8000,sysdate, '����Ա'); ע�⣺values�е�����˳��Ҫ�Ͷ����ֶ�ʱһ�¡� + �����ֶβ������ݣ�INSERT INTO ����(��Ҫ�������ݵ��ֶ�)VALUES(���ֶ�˳�����Ӧ������)�� + (2)�޸ı����������ݣ� + ���޸ı�������ʱ��ͨ������where�Ӿ����޶�Ҫ�޸ĵļ�¼��������ֻ���޸�����where�����ļ�¼�������޸�ȫ���� + UPDATE ���� SET �ֶ���=���� WHERE �ֶ���=���� + (3)ɾ���������ݣ� + DELETE FROM ���� WHERE �ֶ���=���ݡ�ͨ����where�Ӿ����ã�����ɾ�������������ݡ� + ��DDL����е�TRUNCATE���ͬ����ɾ�������ݵ����á� + TRUNCATE��DELETE������DELETE������������ɾ�����ݣ���TRUNCATE�����е�����ȫ��ɾ����DELETE��DML���ԣ�����rollback��TRUNCATE������������ԣ�������Ч���޷�rollback. + ɾ��ȫ����¼��DELETE FROM userplus(Ч�ʵ�); TRUNCATE TABLE userplus(Ч�ʸ�). +8.TCL������������� + (1)commit:�ύ�� ����ͨ������ťȥ�ύ�����������ر����ݿ�ʱ��Ĭ���ύ�� + (2)rollback���ع��� + ������ĸ����ԣ� + ԭ����(Atomicity): +�� �� ԭ������ָ������������в���Ҫôȫ���ɹ�ȫ��Ӧ�ã�Ҫôȫ��ʧ�ܻع����������IJ�������ɹ��ͱ���Ҫ��ȫӦ�õ����ݿ⣬�������ʧ�����ܶ����ݿ����κ�Ӱ�졣 + һ����(Consistency): + һ������ָһ������ִ��֮ǰ��ִ��֮�󶼱��봦��һ����״̬����ת����˵�������û�A���û�B���ߵ�Ǯ������һ����5000����ô����A��B֮�����ת�ˣ�ת�����ˣ���������������û���Ǯ�������Ӧ�û�����5000������������һ���ԡ� + ������(Isolation): +���� ��������ָ�����������֮��Ҫ�໥���롣������û������������ݿ�ʱ���������ͬһ�ű�ʱ�����ݿ�Ϊÿһ���û����������񣬲��ܱ���������IJ��������š� + �־���(Durability): +���� �־�����ָһ������һ�����ύ�ˣ���ô�����ݿ��е����ݵĸı���������Եģ������������ݿ�ϵͳ�������ϵ������Ҳ���ᶪʧ�ύ����IJ����� +9.DQL�����ݲ�ѯ���� + ������ѯ�����SELECT�Ӿ��FROM�Ӿ���ɡ� + SELECT��ָ����ʾ���ֶ����ƣ� FROM��ָ��������Դ�� + (1)��ѯ���У�SELECT * FROM ����; + (2)��ѯ����������Ϣ��SELECT field1, field2 FROM table_name where �޶�����; + ע�⣺SELECT������ʾ�ֶΣ�����ʽ�������� <>�Dz����ڡ����������ƽϳ��������ڲ�ѯ�����������ʾ���������б����� + SELECT salary*12 "��н" FROM userplus WHERE username='����';--���� + SELECT salary*12 AS "��н" FROM userplus WHERE username='����';--���� + �������������ֿ�ͷ���������ֻ���ſ�ͷ��������Ҫ��""���ϡ� +10.���ú���(���к���):4 + (1)�ַ���������13 + CONCAT(c1,c2),�����ַ�������c1,c2�����ֶ�ƴ����ʾ�����磺SELECT CONCAT(CONCAT(ename, ': ') ,sal)from EMP; + ||���Ӷ���ַ������š����磺SELECT ename||': '||sal as "����������" from EMP; + LENGTH(ch):��ʾ�ַ������ȣ� ���磺SELECT LENGTH(ename) from EMP; + UPPER(�ֶ���)��ת��д + LOWER(�ֶ���): תСд + INITCAP(�ֶ���): ֻ������ĸ��д + TRIM():ȥ���ַ���ǰ��ָ�����ݡ����磨�Ҳ�д�ַ�������SELECT TRIM('E' from 'EEHELLO WORLDEEE') from dual; SELECT ename, trim('A' FROM ename) from emp; --ȥ��Ա������ǰ���'A' + LTRIM('',''):ȥ���ַ��ش����ָ�����ݡ����磨���д�ַ�������SELECT LTRIM('EEHELLO WORLDEEE', 'E') from dual; + RTRIM('',''):ȥ���ַ����Ҳ�ָ�����ݡ� + LPAD(ch1, m, ch2): ��λ������ch1��Ҫ��ѯ��ʾ�����ݣ�mָ��λ��������λ������ʱ��ch2���в�λ����emp����ʹ����λ����sql��0����6λ�����磺select ename, LPAD(sal, 6, 0) AS salary from emp + RPAD(ch1, m, ch2): �Ҳ�λ������ + SUBSTR��ch,m,n):��ȡ�ַ�������ȡch����ַ�������m��ʼ��ȡ������ȡn������mΪ���������ǴӺ���ǰ�����±��ѯ���Ӷ����н�ȡ�� + INSTR(ch1,ch2,m,n):����ch1��ch2���ֵ�λ�ã�m�������ĸ�λ�ÿ�ʼ������n�����ڼ��γ��֣���ָ��m��n��Ĭ��ֵ����1���±��1��ʼ����ƥ�����ݵ�ʱ�򷵻�0. + (2)���ֺ�����5 + ROUND(n,m):�����������룬�����е�n�������κ�����(Ҫ������������);m������������mȡ�������������뵽С������mλ��mȡ0���������뵽����λ(mȱʡ��Ĭ����0)��mȡ�������������뵽С����ǰmλ�� + TRUNC(n,m):��ȡ����û���������� + MOD(m,n):����m����n���������nΪ0��ֱ�ӷ���m�� + CEIL(n):����ȡ�������ڸ�������С������ + FLOOR(n):����ȡ����С�ڸ�������������� + (3)���ں���:11 + sysdate:���ص�ǰ��ϵͳʱ�䣬��ȷ���롣 + systimestamp:ʱ��������ص�ǰϵͳ���ں�ʱ�䣬��ȷ�����롣 + (1)TO_DATE():����ת���������൱��Java�е�SimpleDateFormat��parse()���������������ַ������ո��������ڸ�ʽת��ΪDate�� + select to_date('2018-12-01 11:30','YYYY-MM-DD HH:MI:SS') from dual; + select to_date('2018-12-01 13:30','YYYY-MM-DD HH24:MI:SS') from dual; + (2)TO_CHAR():���ڽ�DATE���ո��������ڸ�ʽת��Ϊ�ַ����� + select ename, to_char(hiredate,'YYYY-MM-DD HH:MI:SS') from emp; + select ename, to_char(hiredate,'YYYY"��"MM"��"DD"��"') from emp; + (3)LAST_DAY(date):����ָ�����������µ����һ�졣 + (4)ADD_MONTHS(date,i):��������date����i���º������ֵ����i�Ǹ��������õ��Ǽ�ȥi���º������ֵ�� + (5)MONTHS_BETWEEN(date1,date2):������������֮�������ٸ���;��date2ʱ���date1ʱ��������õ���ֵ�������������ڼ���������£������õ���С��λ�Ľ���� + (6)date�������������ݿ��п��Խ��м������������������������select ename, sysdate-hiredate from emp; + (7)LEAST(date1,date2):��ʾ����ʱ���е���Сֵ���������ͱ���һ�¡� + (8)GREATEST(date1,date2):��ʾ����ʱ���е����ֵ���������ͱ���һ�¡� + (9)EXTRACT(date from datetime):�Ӳ���datetime����ȡ����dateָ�������ݣ�������ȡ���ꡢ�¡��ա� + (4)��ֵ������2 + null�ĺ��壺��ʱ���е�ijЩ�ֶ�ֵ����δ֪����ʱ�����ڣ�ȡֵnull����Java���������͵�Ĭ��ֵΪnull����sql�У��κ��������;���ȡֵΪnull�� + null������ѯ:�������κ�ֵ�� is null �� is not null. + NVL(arg1,arg2):��nullת��Ϊ��nullֵ�����arg1Ϊnull������arg2�����򷵻ر�����arg1��arg2�������κβ������ͣ��������������������ͱ�����һ�µġ� + NVL2(arg,res1,res2):���argΪnull���򷵻�res2,arg��Ϊnull�򷵻�res1. +11.������ѯ��� + (1)from�Ӿ�:select * from table_name;select����ָ��Ҫ��ѯ���У�fromָ��Ҫ���ĸ����в�ѯ�� + (2)where�Ӿ�:��select����п�����where�Ӿ���ʹ�ñȽϲ��������Ʋ�ѯ����� +12.��ѯ���� + (1)�Ƚ��������>,<,>=,<=,<>,= + (2)�ؼ��֣�AND(����),OR(��)��AND���ȼ�����OR������ʹ��()�����OR�����ȼ�˳�� + (3)ģ����ѯ + LIKE������ģ��ƥ���ַ���; + _��������һ���ַ�,%��������0~n���ַ��� + (4)IN��NOT IN�� + IN(list):ȡ���б���Χ�е����ݡ� + NOT IN(list):ȡ�������ϴ��б��е����ݼ�¼�� + (5)BETWEEN...AND...:��������ѯ����ij��ֵ��Χ���������ݡ� + (6)ALL��ANY: ALL��ANY���ܵ���ʹ�ã���Ҫ��ϵ��бȽϲ�����(>,<,>=,<=,<>,=)һ��ʹ�á� + >ANY:������С + ALL:������� + , <, >=, <=, <>, !=, is null(is not null), between...and..., in; + �߼������not, and, or + BETWEEN...AND...��AND������ + BETWEEN...AND...:ֻ�������Ƚ�һ���ֶ�ֵ + AND:���������Ƚϲ�ͬ���ֶ� +14.ORDER BY�����򣬶Խ���������򣬱��������select�����һ���Ӿ䡣 + asc��д������desc��������������ֶ��а���null�����������ֵ�� + ���Զ�����Ϊ�����׼ʱ�����Ȱ��յ�һ�н������������һ��������ͬ�����Եڶ��������Դ����ơ� +15.�ۺϺ���(���к��������麯��) + (1)MAX��MIN:����ȡ���л����ʽ�������Сֵ����������ͳ���κ��������ͣ��������֡��ַ������ڡ�����NULLֵ�� + (2)AVG��SUM:����ͳ���л����ʽ��ƽ��ֵ���ܺͣ�ֻ�ܲ����������ͣ�����NULLֵ�� + (3)COUNT:����������еļ�¼����������NULLֵ�� + (4)�ۺϺ�����NULLֵ�Ĵ������ۺϺ�������NULLֵ��select count(comm)��avg(comm) from emp;--�Զ�����commΪnull�ļ�¼; + �����ϣ������NULLֵ������Ҫʹ��NVL��NVL2:select count(nvl(comm,0)), avg(nvl(comm,0)) from emp; +16.GROUP BY:���顣�����������ָ���ֶν��з��顣������򣺸��ֶ��µ�ֵһ���ļ�¼������һ�顣 + ���select��������˾ۺϺ�����ͬʱ�ֳ�����ijһ�ֶ�ʱ����ô���ֶα��������group by�Ӿ��С� + where���治��ʹ�þۺϺ���,��Ϊwhere�ǷǷ��麯���Ĺ����жϡ�select max(sal),deptno from emp where max(sal)>4000 group by deptno;--����ִ�� + HAVING:�Ƿ��飨�ۺϣ������Ĺ����ж�;�������GROUP BY֮�󣬲��ܵ�������;�����Է����Ľ����һ������;���簴���ŷ���󣬵õ�ÿ�����ŵ���߹��ʣ����Լ��������������� + ��ѯ����������ʳ���4000�IJ����Լ������:select max(sal),deptno from emp group by deptno having max(sal)>4000; +17.SELECT���ִ�й��̣� + (1)ͨ��FROM�Ӿ����ҵ���Ҫ��ѯ�ı��� + (2)ͨ��WHERE���зǷ��麯����ɸѡ�жϡ� + (3)ͨ��GROUP BY�Ӿ���ɷ�������� + (4)ͨ��HAVING�Ӿ�����麯��ɸѡ�жϡ� + (5)ͨ��SELECT�Ӿ�ѡ��Ҫ��ʾ���л����ʽ���麯���� + (6)ͨ��ORDER BY�Ӿ������������� +18.�����ܽ� + ���к����Ͷ��к����ķ��ಢ����˵���� + ���к����� + �ַ�����13��CONCAT(c1,c2), LENGTH(ch), UPPER(�ֶ���), LOWER(�ֶ���), INITCAP(�ֶ���), TRIM('e'FROM'ee'), LTRIM('ee','e'), RTRIM('ee','e'), LPAD(ch1, m, ch2), RPAD(ch1, m, ch2), SUBSTR��ch,m,n), INSTR(ch1,ch2,m,n) + ��ֵ����5��ROUND(n,m), TRUNC(n,m), MOD(m,n), CEIL(n), FLOOR(n) + ���ں���9��sysdate, systimestamp, LAST_DATE(date), ADD_MONTHS(date,i), MONTHS_BETWEEN(date1,date2), LEAST(date1,date2), GREATEST(date1,date2), EXTRACT(date from datetime) + ת�������� + ��ʾת����TO_DATE(), TO_CHAR() + ��ʽת���������������ַ�������ͨ����ת��Ϊ�ַ����� + ͨ�ú���2��NVL(), NVL2() + ע�⣺ͨ�����ڲ����������ݣ���������Ƕ�׶�㣬���ʱ������������ִ�С� + ���к���(�ۺϺ���)5��MAX()���ֵ, MIN()��Сֵ, SUM()���, AVG()ƽ��ֵ, COUNT()ͳ�� +19.������ѯ�� + (1)���ʵ��Ӧ��������Ҫ�����ݣ���������Ҫ��ѯ�������������ϵı������ֲ�ѯ�������Ӳ�ѯ�����Ӳ�ѯͨ�����������໥��ϵ�ĸ��ӹ�ϵ�� + (2)�����������һ�ű��������г�Ϊ�������������ı����ڹ�����ϵ�����ڶ��һ���� + (3)�����֮��Ĺ�ϵ��һ�Զ�(���)����Զ�(ͨ�������ű�������ϵ)��һ��һ�� + (4)N�ű��Ĺ�����ѯ������Ҫ(N-1)����������;��ֵ���ӣ����=����;����ָ����������������ֵѿ�����(ָ������������ÿ������ÿһ�к���������ÿһ������ϣ���������Ľ����)�� + (5)�ܽ᣺ + ���Ը����������ͨ������ָ���ֶ������ĸ��������������ֶ�������ͬ�����ָ��������Դ�� + ֻ���������������Ľ�����Żᱻ��ѯ������(����deptno40��û���ˣ���˲��ᱻ��ʾ����)�� + ���й�����������ͬʱ�������������͹������������ݲŻᱻ��ѯ������ +20.�������ࣺ + ?�����ӣ����������������������ļ�¼�������Ϻ͵�ֵ����û������ֻ��д����ͬ�� + �﷨����1 INNER JOIN ��2 ON ��������;(INNER����ʡ��) + �����ӣ����������������������ݼ�¼����Щ�������Ҫ������Щ���������������ļ�¼����Ҫʹ�������ӡ�������Ͳ��������������Ľ��ȫ����ѯ������ + create table empc as select * from emp;--���ٴ�һ�����и������� + insert into empc(empno, ename, deptno)values(1000,'JACK',50); + �������ӣ�(�����ȫ�����ұ��IJ���)��������ļ�¼���ڱ����ӵ��ұ����ҳ����������ļ�¼��֮ƥ�䣬����Ҳ��������ƥ�����null��ʾ�� + �﷨����1 LEFT OUTER JOIN ��2 ON ��������;(OUTER����ʡ��) + �������ӣ�(�ұ���ȫ��������IJ���)�����ұ��ļ�¼���ڱ����ӵ�������ҳ����������ļ�¼��֮ƥ�䣬����Ҳ������ұ�ƥ�����null��ʾ�� + �﷨����1 RIGHT OUTER JOIN ��2 ON ��������;(OUTER����ʡ��) + ȫ�����ӣ����ط������������б��ļ�¼��û����֮ƥ��ģ���null��ʾ(����������Ӻ������ӵIJ���)�� + �﷨����1 FULL OUTER JOIN ��2 ON ��������;(OUTER����ʡ��) + ?�����ӣ����ӵ�����������ͬһ���������е�һ���ֶο��Զ�Ӧ��ǰ���������ֶΡ� + Oracle���ݿ��Դ��﷨�� + (1)�ѿ������� + (2)�����ӣ�a.��ֵ���� + b.�ǵ�ֵ����:���Ա����š����������ʡ����ʵȼ���select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal; + c.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno=d.deptno(+);--��ѯԱ���ı�ţ��������������ƣ������������κβ��ŵ�Ա���� + d.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno(+)=d.deptno;--��ѯԱ���ı�ţ��������������ƣ�����û��Ա���IJ��š� + e.�����ӣ� + ͨ�ñ�׼��SQL99�﷨�� + (1)�������ӣ��������Ӻ͵ѿ�����������ģʽ��ͬ�����DZ�1�е�ÿһ�ж����2�е������н���һ�����ӡ�select * from emp cross join dept; + (2)��Ȼ���ӣ�select * from emp natural join dept;--���ܹ�ƥ�䵽��ֵ���������Լ������ͬ���У�����ô���ǵ�ֵ���ӵĽ��.����dept���е�ֵ���ӡ� + select * from emp natural join salgrade;--������ƥ�䵽��ֵ��������ô������ǽ������ӡ� + (3)�������ӣ�left outer join + (4)�������ӣ�right outer join + (5)ȫ�����ӣ�full outer join +21.�Ӳ�ѯ�� + Ϊ�˸���ѯ�ṩ���ݶ�����ִ�еIJ�ѯ�������Ӳ�ѯ���Ӳ�ѯ��Ƕ��������SQL����е�SELECT��䡣�Ӳ�ѯǶ�������������ѯ�򸸲�ѯ�� + ע����� + (1)�Ӳ�ѯ��Ҫд�������С� + (2)�Ӳ�ѯ��Ҫд����������Ҷˡ� + (3)�Ӳ�ѯ����д��WHERE��HAVING��FROM�Ӿ��С� + (4)�Ӳ�ѯ��ͨ����дORDER BY�� + �Ӳ�ѯ��WHERE�Ӿ��У����ݷ��ؽ���IJ�ͬ���Ӳ�ѯ��Ϊ�����Ӳ�ѯ�������Ӳ�ѯ�������Ӳ�ѯ�� + (1)�����Ӳ�ѯ������һ��һ�����ݣ�Ҫ��ʹ�õ��в�����(>,>=,<,<=,<>,=) + (2)�����Ӳ�ѯ�����ض������ݣ�ʹ�ö��в�����(in,all,any) + (3)�����Ӳ�ѯ: ͨ�����ڽ����ڶ��β�ѯ����������FROM�Ӿ��С� + ͳ�Ƶ����Լ����ʵ������ڲ���ƽ�����ʵ�Ա����Ϣ�� + a.����ͳ�Ʋ���ƽ�����ʣ�select avg(sal) aa, deptno from emp group by deptno + b.Ա������С��<����ƽ�����ʣ�select sal, ename, e.deptno from emp e, (select avg(sal) aa, deptno from emp group by deptno) t where e.deptno=t.deptno and e.sal=6; + ҳ�룺page��ÿҳ���pageSizeΪ5����һҳ1~5���ڶ�ҳ6~10������ҳ11~15. start=(page-1)pageSize+1; end=pageSize*page; +23.DECODE���� + DECODE(����1��ֵ1�����1��ֵ2�����2...[,Ĭ��ֵ]);�ò�������ȥ��ֵƥ�䣬ƥ�䵽������ʾ��Ӧ�Ľ��;��ƥ������ʾĬ��ֵ��Ĭ��ֵ����������ʾnull(�൱��Java�е�switch)�� + --��ѯְԱ��������ְԱ��ְλ���㽱������ְλ�ֱ���'MANAGER','SALESMAN','ANALYST'ʱ��������ֱ���нˮ��1.2,1.1,1.05��������Ϊԭ����нˮֵ�� + select ename, job, sal, decode(job,'MANAGER',sal*1.2,'SALESMAN',sal*1.1,'ANALYST',sal*1.05,sal) "����" from emp; + select * from emp order by decode(job,'MANAGER','1','SALESMAN','2','ANALYST','3');--�Զ������� +24.���Ϻ��� + (1)UNION��������������ѯ�����������union(�����ظ����ݣ�������), union all(�������ظ����ݣ�������); + select * from emp where sal>1500 union select * from emp where deptno=20;--����֮���ܹ�9����¼ + (2)INTERSECT������ͬʱ����������������е����ݡ� + (3)MINUS��������������� + (4)ʹ�ü��Ϻ�����ԭ�����еIJ�ѯ������ܲ�������ͬһ�ű������ұ����֮���޹����� + (5)ע�����a.��Ӧ���е�������Ҫƥ�䡣 + b.�е�����Ҫ����˳��д�� + c.�е�����Ҫһ�£������һ��ʱ����nullȥ���䲻ƥ����С� +25.DDL���������� + (1)�������ռ䣺�߼���λ��ͨ�������½�һ����Ŀ���ͻ��´������ռ䣬�ڱ��ռ��д����û����������� + ���л���system���﷨��CREATE tablespace ���ռ����� datafile '�ļ���·��' size ��С autoextend on �Զ���չ next ÿ����չ�Ĵ�С; + (2)ɾ�����ռ䣺drop tablespace ���ռ����� + (3)�����û���create user �û��� identified by ���� default tablespace ���ռ������; + (4)ɾ���û���drop user �û��� cascade; + (5)�����û����룺alter user �û��� identified by ����; + (6)��Ȩ��grant connect to �û���; + (7)�����ɫ��grant dba to �û���; + (8)ʹ���Ӳ�ѯ�ķ�ʽ������(�л������´������û�֮�󣩣� + ֻ���Ʊ��ṹ�ͱ����ݣ����Ḵ���е�Լ����create table emp as select * from scott.emp;-- + �����ѯ����н�������Ǹ��Ʊ��ṹ�����ݣ����û�н�������Ǹ��Ʊ��ṹ��create table dept as select * from scott.dept where 1=2;--ֻ���Ʊ��ṹ��������,��Ϊ1=2Ϊfalse�� + ֻ���Ʊ�����(������������û��values�ؼ���):insert into ���� ��ѯ��� (���Ľṹ�����б��ṹһ��):insert into dept select * from scott.dept; +26.�е�Լ����Լ����Ҫ������Լ���������ݵĹ��� + ����Լ����primary key ����Ϊ�գ�����Ψһ�� + �ǿ�Լ����not null + ΨһԼ����unique + ���Լ����check(����) ��MySql���ǿ���д�ģ���MySql��ֱ�Ӻ��Լ�顣price number(10,2) check(price>100),--���Լ�����ڴ�������ʱ��ֱ��д(��primary key, not null, uniqueһ��λ�û�֮��) + ���Լ������Ҫ������Լ���ӱ�A�еļ�¼�����Ǵ���������B��(��֤����������)�� + (1)references �����(���������), --�ڴ�������ʱ��ֱ��д(��primary key, not null, unique, checkһ��λ�û�֮��) + (2)foreign key(�Լ�������) references �����(���������)--��һ�����Լ���ķ�ʽ��д�����һ�����ŵ�ǰ�� + (3)alter table books add foreign key(cid) references category(id);--���Բ��ڶ�����н������Լ���������ô����Ҳ��ʵ�����Լ���� +27.��ͼ�����ݿ����֮һ����ͼ��sql��������ֵĽ�ɫ�ͱ���ȫ��ͬ������ͼ������һ����ʵ���ڵı�������Ӧ����һ����ѯ���Ľ��������ͼ���ͱ�������һ������ + ���ã��򻯸��Ӳ�ѯ�������������ݷ��ʡ� + (1)��������ͼ��������������Ȩ�ޣ�ʹ��system�û���Ȩ����ͼ��Ȩ��䣺grant create view to �û����� + create view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10; + (2)�޸���ͼ�����滻����ͼ���Ӳ�ѯ��ʹ��create or replace view ��ͼ�� ���ɣ�����ͼ�����ھʹ��������ھ��滻��������ͼʱ�����Ը��и�������� + create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10; + (3)��ѯ��ͼ���������б������Ա���Ϊ׼������ԭ������select * from v_emp_10; + (4)��ͼ��DML����(������)������ͼ����DML�������Ƕ���ͼ������Դ�Ļ��������в����� + a.�������ݣ���ͼ�п��������ֶζ�������Ĭ��ֵ�����Բ���Υ������������Ӧ�ֶε�Լ�������翴�������ֶε�not null��Լ������������ʧ�ܣ��� + b.Ϊ��ͼ���Ӽ��ѡ����Ա������ͼ����DML����ʱ��Ⱦ��������������ڴ��������with check option + create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10 with check option;--���ܶԻ�������deptno10��������ݽ���DML������ + c.with read only��ֻ�������ܶ���ͼ����DML���������û������ͼ��ִ��DML�����ı�Ҫ���ڽ�����ͼʱ����Ϊֻ�������������������֤��ͼ��Ӧ�Ļ��������ݲ��ᱻ�Ƿ��޸ġ� + create or replace view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10 with read only;--with read onlyֻ�������ܶ���ͼ����DML������ + (5)��ͼ�����Ӳ�ѯ�IJ�ͬ����Ϊ����ͼ�͸�����ͼ�����Ӳ�ѯ�а������к���������ʽ����麯��ʱ������ͼ��һ��������ͼ��������ͼ���ܽ���DML���������ұ��뱻�Ӳ�ѯ�еı���ʽ������������� + (6)ɾ����ͼ��DROP VIEW ��ͼ���ơ� +28.���У�sequence����������Ψһ����ֵ�����ݿ����ͨ����Ϊ��������ֵ�������Ƕ��������ݿ�������в��������ڱ��� + (1)�������У�create sequence ������ [start with **(��ʼ����) increment by **(����,ÿ�����Ӽ��������ɸ�) maxvalue **(���ֵ) / minvalue **(��Сֵ)]; + (2)������������α��:nextval/*��ȡ���е��¸�ֵ*/ ,currval/*��ȡ���еĵ�ǰֵ*/ + �����д����Ժ������ִ��nextval��֮�����ʹ��currval����ȡ���еĵ�һ��ֵ����ʹ������ֵΪusers�������µļ�¼�� + select users_seq.currval from dual;--�鿴��ǰ����ֵ�� + (3)ɾ������:drop sequence users_seq; + (4)���ӣ� + create sequence users_seq start with 100 increment by 10;--��������,��һ��һ��Ҫ��nextval��������currval + insert into users(id,name)values(users_seq.nextval,'Tony');--������� + select users_seq.currval from dual;--�鿴��ǰ����ֵ�� + drop sequence users_seq;--ɾ������ + +29.������һ����߲�ѯЧ�ʵĻ���(�������ֵ��Ŀ¼)��������Ϊ����������ȥ�ء�����������������ֶΣ�����Ϊ������������ + ?(1)����������create [unique] index �������� on ����(����) + create index idx_users_name on users(ename);--��users����ename���Ͻ������� + (2)���Ӷ���������create index idx_users_salary on users(name,salary); + (3)��ѯ�������Զ�Ӧ������������Ҫ����ȥ�ƶ�����߲�ѯ�ٶȡ� + select users.* from users order by name, salary; + (4)�ع�������������ƽ����������޸ı�������ع�һ��������ƽ��������� + alter index �������� rebuild;--�����޸����ݵı��������齨�������� + (5)ɾ��������drop index idx_users_salary; \ No newline at end of file diff --git a/JDBC/bin/note/sql/SQLPractice.sql b/JDBC/bin/note/sql/SQLPractice.sql new file mode 100644 index 0000000..ad16146 --- /dev/null +++ b/JDBC/bin/note/sql/SQLPractice.sql @@ -0,0 +1,233 @@ +1~10 homework: +--1����USERS���в����Ա��ֶΣ���ָ��Ĭ��ֵΪ�� +alter table userplus add(sex char(2) default '��'); +--2��ɾ���������������˵���Ϣ +delete from userplus where username='����'; +--3����ѯUSERS�����û������� +select username from userplus; +--4����ѯÿ���û��Ĺ����Ƕ��� +select username, salary from userplus; +--5���鿴�����Ĺ��� +select salary from userplus where username='����'; +--6����ѯÿ���û�����н +select salary*12 as "��н" from userplus; +--7����ѯ�û����ʴ���2000���û���Ϣ +select * from userplus where salary > 2000; +--8���� ���� ���� ������ʾ +select username||': '||salary as "����������" from userplus; +--9����ѯUSERS����ÿ���û������ֳ��� +select username,length(username) from userplus; +--10����ѯ����3λ���ϵ��û���š����������ʡ����� +select id, username, salary, job from userplus where length(username)>=3; + +10~12 homework: +--1�����ѯ��DEPT�����в��ŵ������ +select * from dept; +--2����ѯ��DEPT�еIJ��źš��������������ֶε�������Ϣ�� +select deptno, dname from dept; +--3����ӱ�EMP�в�ѯ10�Ų��Ź����Ĺ�Ա�����͹��ʡ� +select ename, sal from emp where deptno=10; +--4����ӱ�EMP�в��ҹ�����ְԱCLERK����MANAGER�Ĺ�Ա���������ʡ� +select ename, sal from emp where job in ('CLERK','MANAGER'); +--5������EMP���в��Ҳ��ź���10��30֮��Ĺ�Ա�����������źš����ʡ������� +select ename, deptno, sal, job from emp where deptno between 10 and 30; +--6����ӱ�EMP�в���������J��ͷ���й�Ա�����������ʡ�ְλ�� +select ename, sal, job from emp where ename like 'J%'; +--7����ӱ�EMP�в��ҹ��ʵ���2000�Ĺ�Ա�����������������ʣ��������ʽ������С� +select ename, job, sal from emp where sal<2000 order by sal desc; +--8���ڱ�EMP�в�ѯ���й��ʸ���JONES�����й�Ա�����������͹��ʡ� +select ename, job, sal from emp where sal>(select sal from emp where ename='JONES'); +--9���г�û�ж�Ӧ���ű���Ϣ�����й�Ա�������������Լ����źš� +select ename, job, deptno from emp where deptno not in(select deptno from dept); +--10�����ҹ�����1000��3000֮��Ĺ�Ա���ڲ��ŵ�������Ա��Ϣ +select * from emp where deptno in (select distinct deptno from emp where sal between 1000 and 3000); +--11����ѯ���й�Ա��������SAL��COMM֮�͡� +select ename, sal, comm, NVL2(comm,comm+sal,sal) from emp; +--12����ѯ������CHICAGO�����ľ���MANAGER������ԱSALESMAN������������ +select ename, sal from emp where deptno=(select deptno from dept where loc='CHICAGO') and job in('MANAGER','SALESMAN'); +select a.ename, a.sal from emp a, dept b where a.deptno=b.deptno and b.loc='CHICAGO' and a.job in('MANAGER','SALESMAN'); +--13����ѯ��ʾÿ����Ա���빫˾��׼ȷʱ�䣬����������������¡����� ʱ������ʾ�� +select ename, to_char(hiredate,'YYYY"��"MM"��"DD"��" HH:MI:SS') from emp; + +13~18 homework: +select * from dept +select * from emp +--1.�г�������һ��Ա�������в��š� +select deptno from emp group by deptno having count(empno)>=1; +--2.�г�н��ȡ�SMITH���������Ա���� +select * from emp where sal>(select sal from emp where ename='SMITH'); +--3.�г����н�����1500�ĸ��ֹ����� +select distinct(job) from emp group by job having min(sal)>1500; +--4.��ѯ����20,30 �еĸ�λ����"CLERK"��"SALESMAN"������Ա����Ϣ +select * from emp where deptno in (20,30) and job not in('CLERK','SALESMAN'); +--5.��ѯ��ƽ��Ա�����ʸߵ�Ա����Ϣ +select * from emp where sal > (select avg(sal) from emp); +--6.�ҳ�����10�����о����Ͳ���20�����а���Ա����ϸ���ϡ� +select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK'); +--7.��ʾ�����С�R���Ĺ�Ա������ +select ename from emp where ename not like '%R%'; +--8.��ʾ��Ա������������������ޣ������ϵĹ�Ա������ǰ�档 +select ename, hiredate from emp order by hiredate asc; +--9.����ÿ����Ա����ʾ����빫˾������ +select ename, trunc(sysdate-hiredate+1) as "����" from emp; +--10.��ѯ����ְλ��ƽ�������Ƕ��٣� +select job, avg(sal) from emp group by job; +--11.�鿴ÿ�������ж����� +select deptno, count(*) from emp group by deptno; +--12.�鿴ÿ��ְλ��ƽ�������빤���ܺ� +select job, avg(sal),sum(sal) from emp group by job; +--13.���emp�������в��ŵ����нˮ�����нˮ�����ű��Ϊ10�IJ��Ų���ʾ�� +select deptno, max(sal), min(sal) from emp where deptno<>10 group by deptno; +--14.��ѯ���������С�A���ַ�������нˮ��1000���ϣ�������1000��������Ա����Ϣ�� +select * from emp where ename like '%A%' and sal>1000; +--15.��ʾнˮ����˵�ְλ�� +select job from emp where sal=(select max(sal) from emp); +select job from emp where sal>=all(select sal from emp); +--16.�г�����Ա�����깤��,����н�ӵ͵������� +select ename, sal*12 as "��н" from emp order by sal; + +19~21 homework: +select * from emp; +select * from salgrade; +--1.��ʾԱ�����еIJ��ظ��ĸ�λjob +select distinct(job) from emp; +--2.��ѯ10���Ź��ʴ���3000��Ա����Ϣ��Ҫ��Ա������ְʱ����ǰ�������� +select * from emp where deptno=10 and sal>3000 order by hiredate; +--3.�г��������ŵ�MANAGER �����н�� +select deptno,min(sal) from emp where job='MANAGER' group by deptno; +--4.��ѯ����Ա���Ĺ����ص㣬����û��Ա���ĵص�ҲҪ��ʾ +select ename,loc from dept d left outer join emp e on d.deptno=e.deptno; +--5.��ѯ���е�Ա����Ϣ�Ͳ������ƣ������������κβ��ŵ�Ա�� +select empno, ename, dname, e.deptno from emp e left outer join dept d on e.deptno=d.deptno; +--6.�г���"SCOTT"������ͬ����������Ա������������ +select empno, ename, job, dname from emp e,dept d where e.deptno=d.deptno and job=(select job from emp where ename='SCOTT'); +--7.�г��ܹ���������ֱ���ϼ�������Ա���ı�ţ��������������� +select empno, ename, e.deptno, dname from emp e,dept d where e.deptno=d.deptno and hiredate<(select hiredate from emp m where e.mgr=m.empno); +--8.�г�ְλΪ��CLERK����Ա�������������ڲ������ƣ��������� +select ename, dname, t.b from emp e, dept d, (select deptno,count(*) b from emp group by deptno) t where e.deptno=d.deptno and e.deptno=t.deptno and job='CLERK'; +--9.��ѯ������4��Ա���IJ��ŵIJ������� +select e.deptno, count(*),dname from emp e, dept d where e.deptno=d.deptno group by e.deptno,dname having count(*)>4; +select d.dname from dept d where deptno in (select deptno from emp group by deptno having count(*)>4); + --�Լ�д�� +select d.deptno, d.dname, t.count from emp e, dept d, (select count(*) count from emp a group by deptno) t where e.deptno=d.deptno and t.count>4; +--10.��ѯ���ʸ���30�Ų��ŵ�����Ա���Ĺ��ʵ�Ա�����������ʼ��������� +select ename, sal, e.deptno, dname from emp e,dept d where sal>(select max(sal) from emp where deptno=30); +--11.��ѯ����KING�����ڲ��ŵIJ��ű�š����������Լ��ò������Ա������ +select e.deptno, dname, count(*) from emp e,dept d where e.deptno=d.deptno and e.deptno=( select deptno from emp where ename='KING') group by e.deptno,dname; + --�Լ�д�ģ���̫�ã� +select e.deptno, dname, e.deptno,(select count(*) count from emp where deptno= (select deptno from emp where ename='KING')) from emp e, dept d where e.deptno=d.deptno and ename='KING'; +--12.��ѯƽ��������ߵIJ��ŵIJ��ű�š��������ƺ͸ò��ŵ�ƽ������ +select e.deptno, d.dname, avg(sal) from emp e, dept d where e.deptno=d.deptno group by e.deptno,d.dname having avg(sal)>=all(select avg(sal) from emp group by deptno); + --�Լ�д�� +select deptno from emp group by deptno having avg(sal)>=all(select avg(sal) from emp group by deptno); +--13.���ʵȼ�Ϊ2�ȼ��� Ա����Ϣ�������ڲ��ŵ���Ϣ +select * from emp e,dept d, salgrade s where e.deptno=d.deptno and s.grade=2 and e.sal between s.losal and s.hisal; + --�Լ�д�� +select e.ename, e.sal from emp e,dept d, (select losal, hisal from salgrade where grade=2) t where e.deptno=d.deptno and e.sal between t.losal and t.hisal; +--14.10�Ų��ŵIJ�����Ϣ ��Ա��Ϣ ���ʵȼ� +select d.*,e.*,s.grade from emp e,dept d, salgrade s where e.deptno=d.deptno and e.deptno=10 and e.sal between s.losal and s.hisal; + --�Լ�д�� +select ename, sal, (select t.grade from salgrade t where e.sal between t.losal and t.hisal) grade from emp e where deptno=10; +--15.��ѯ����쵼�ߵ�нˮ�ȼ� +select grade from salgrade where (select sal from emp where mgr is null) between losal and hisal + --�Լ�д�� +select ename, sal, (select t.grade from salgrade t where e.sal between t.losal and t.hisal) grade from emp e where mgr is null; +--16.SQL����ִ�й��̼���ÿ���Ӿ�����ã� +FROM,WHERE,GROUP BY,HAVING,SELECT,ORDER BY +--17.���ݿ����������ͼ����ȼ��� +();���������*,/,+,-;���ӷ�||;�Ƚ������=, >, <, >=, <=, <>, !=, is null(is not null), between...and..., in;�߼������not, and, or +--18.���ݿ�ĺ������ࣿ +DDL,DML,TCL,DQL,DCL +�ַ�,��ֵ,����,ת�� +--19.������ĸ����� +ԭ���� +һ���� +������ +�־��� + +--С�ܽ᣺ +--SQL����ִ�й��̼���ÿ���Ӿ�����ã� +(1)ͨ��FROM�Ӿ����ҵ���Ҫ��ѯ�ı��� +(2)ͨ��WHERE���зǷ��麯����ɸѡ�жϡ� +(3)ͨ��GROUP BY�Ӿ���ɷ�������� +(4)ͨ��HAVING�Ӿ�����麯��ɸѡ�жϡ� +(5)ͨ��SELECT�Ӿ�ѡ��Ҫ��ʾ���л����ʽ���麯���� +(6)ͨ��ORDER BY�Ӿ������������� + +--���ݿ����������ͼ����ȼ��� +(); +���������*,/,+,-; +���ӷ�||; +�Ƚ������=, >, <, >=, <=, <>, !=, is null(is not null), between...and..., in; +�߼������not, and, or + +--���ݿ�ĺ������ࣿ +���к�����CONCAT(c1,c2), LENGTH(ch), UPPER(�ֶ���), LOWER(�ֶ���), INITCAP(�ֶ���), TRIM('e'FROM'ee'), LTRIM('ee','e'), RTRIM('ee','e'), LPAD(ch1, m, ch2), RPAD(ch1, m, ch2), SUBSTR��ch,m,n), INSTR(ch1,ch2,m,n) +��ֵ������ROUND(n,m), TRUNC(n,m), MOD(m,n), CEIL(n), FLOOR(n) +���ں�����sysdate, systimestamp, LAST_DATE(date), ADD_MONTHS(date,i), MONTHS_BETWEEN(date1,date2), LEAST(date1,date2), GREATEST(date1,date2), EXTRACT(date from datetime)�ۺϺ�����count(),avg(),sum(),max(),min(), +ת�������� + ��ʾת����TO_DATE(), TO_CHAR() + ��ʽת���������������ַ�������ͨ����ת��Ϊ�ַ����� +ͨ�ú�����NVL(), NVL2() +���к���(�ۺϺ���)��MAX()���ֵ, MIN()��Сֵ, SUM()���, AVG()ƽ��ֵ, COUNT()ͳ�� + +--�ṹ����ѯ���Է�������ã� +SQL���ࣺ + DDL�����ݶ������ԣ����ڲ������ݶ��󣨱�����ͼ�����У��������� + CREATE������������������ + ALTER���޸ı�����������ṹ�� + DROP��ɾ�������������� + TRUNCATE��ɾ�������ݣ��������ṹ����ձ��IJ����� + DML�����ݲ������ԣ�ͨ����Ҫ����TCL����������ɾ�ı��е����ݡ� + INSERT INTO�������ݲ�����С� + DELETE��ɾ���������ݡ� + UPDATE SET�����±������ݡ� + TCL������������ԡ� + COMMIT���ύ�� + ROLLBACK���ع��� + DQL�����ݲ�ѯ���� + SELECT����ѯ���ݡ� + DCL�����ݿ������ԡ� + GRANT������Ȩ�ޡ� + grant��create��view��to��scott; + +--��������ԣ� +ԭ����(Atomicity): +�� ԭ������ָ������������в���Ҫôȫ���ɹ���Ҫôȫ��ʧ�ܻع����������IJ�������ɹ��ͱ���Ҫ��ȫӦ�õ����ݿ⣬�������ʧ�����ܶ����ݿ����κ�Ӱ�졣 +һ����(Consistency): + һ������ָһ������ִ��֮ǰ��ִ��֮�󶼱��봦��һ����״̬����ת����˵�������û�A���û�B���ߵ�Ǯ������һ����5000����ô����A��B֮�����ת�ˣ�ת�����ˣ���������������û���Ǯ�������Ӧ�û�����5000������������һ���ԡ� +������(Isolation): +�����������ǵ�����û������������ݿ�ʱ���������ͬһ�ű�ʱ�����ݿ�Ϊÿһ���û����������񣬲��ܱ���������IJ��������ţ������������֮��Ҫ�໥���롣 +�־���(Durability): +�����־�����ָһ������һ�����ύ�ˣ���ô�����ݿ��е����ݵĸı���������Եģ������������ݿ�ϵͳ�������ϵ������Ҳ���ᶪʧ�ύ����IJ����� + +--Oracle���ݿ��Դ��﷨��ͨ�ñ�׼��SQL99�﷨�� +Oracle���ݿ��Դ��﷨�� + (1)�ѿ������� + (2)�����ӣ�a.��ֵ���� + b.�ǵ�ֵ����:���Ա����š����������ʡ����ʵȼ���select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal; + c.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno=d.deptno(+);--��ѯԱ���ı�ţ��������������ƣ������������κβ��ŵ�Ա���� + d.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno(+)=d.deptno;--��ѯԱ���ı�ţ��������������ƣ�����û��Ա���IJ��š� + e.�����ӣ� +ͨ�ñ�׼��SQL99�﷨�� + (1)�������ӣ��������Ӻ͵ѿ�����������ģʽ��ͬ�����DZ�1�е�ÿһ�ж����2�е������н���һ�����ӡ�select * from emp cross join dept; + (2)��Ȼ���ӣ�select * from emp natural join dept;--���ܹ�ƥ�䵽��ֵ���������Լ������ͬ���У�����ô���ǵ�ֵ���ӵĽ��.����dept���е�ֵ���ӡ� + select * from emp natural join salgrade;--������ƥ�䵽��ֵ��������ô������ǽ������ӡ� + (3)�������ӣ�left outer join + (4)�������ӣ�right outer join + (5)ȫ�����ӣ�full outer join + +--�����е�Լ���� +����Լ����primary key ����Ϊ�գ�����Ψһ�� +�ǿ�Լ����not null +ΨһԼ����unique +���Լ����check(����) ��MySql���ǿ���д�ģ���MySql��ֱ�Ӻ��Լ�顣price number(10,2) check(price>100),--���Լ�����ڴ�������ʱ��ֱ��д(��primary key, not null, uniqueһ��λ�û�֮��) +���Լ������Ҫ������Լ���ӱ�A�еļ�¼�����Ǵ���������B��(��֤����������)�� + (1)references �����(���������), --�ڴ�������ʱ��ֱ��д(��primary key, not null, unique, checkһ��λ�û�֮��) + (2)foreign key(�Լ�������) references �����(���������)--��һ�����Լ���ķ�ʽ��д�����һ�����ŵ�ǰ�� + (3)alter table books add foreign key(cid) references category(id);--���Բ��ڶ�����н������Լ���������ô����Ҳ��ʵ�����Լ���� + +--��һ��sql�����emp���й��ʴӴ�С����4��5��6λ���ʣ� +select * from (select rownum rn, e.* +from (select ename, sal from emp order by sal desc) e where rownum<=6)t +where t.rn >=4; \ No newline at end of file diff --git "a/JDBC/bin/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\347\255\224\346\241\210\357\274\211.txt" "b/JDBC/bin/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\347\255\224\346\241\210\357\274\211.txt" new file mode 100644 index 0000000..ca083df --- /dev/null +++ "b/JDBC/bin/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\347\255\224\346\241\210\357\274\211.txt" @@ -0,0 +1,97 @@ +1.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门 +select dept.dname,emp.* from dept +left outer join emp +on dept.deptno=emp.deptno; + +2.列出在每个部门工作的员工数量、平均工资。 +select dept.deptno,count(emp.empno),avg(sal) +from dept +left outer join emp +on dept.deptno=emp.deptno +group by dept.deptno; + +3.列出所有员工的姓名、部门名称和工资。 +select emp.ename, dept.dname,emp.sal +from emp, dept +where emp.deptno=dept.deptno; + +4.查询每个部门的部门编号,部门名称,部门人数,最高工资,最低工资,工资总和,平均工资 +select d.deptno,d.dname,count(e.ename), +max(e.sal),min(e.sal),sum(sal),avg(sal) +from dept d join emp e on e.deptno=d.deptno +group by d.deptno,d.dname + +5.查询每个经理所管理的人数,经理编号,经理姓名,要求包括没有经理的人员信息。 +select count(e.empno),m.empno,m.ename +from emp e +left join emp m +on e.mgr = m.empno +group by m.empno,m.ename; + +6.查询部门人数大于2的部门编号,部门名称,部门人数 +select d.deptno,d.dname,count(e.ename) +from dept d +join emp e on d.deptno=e.deptno +group by d.dname,d.deptno +having(count(e.ename)>2); + +7.查询部门平均工资在2500元以上的部门名称及平均工资。 +select d.dname ,avg(e.sal) +from dept d +join emp e on d.deptno=e.deptno +group by d.dname +having(avg(e.sal)>2500); + +8.查询员工岗位中不是以"SA"开头并且平均工资在 2500元以上的岗位及平均工资,并按平均工资降序排序。 +select e.job,avg(e.sal) +from emp e +where e.job not like 'SA%' +group by e.job +having avg(e.sal)>2500 +order by avg(e.sal) desc + +9.显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于 +3000的,按最低工资由高到低排序。 +select m.empno,m.ename,min(e.sal) +from emp e +left join emp m +on e.mgr = m.empno +group by m.empno,m.ename +having min(e.sal)>=3000 +order by min(e.sal) desc; + +10.查询部门平均工资大于2000,且人数大于2的部门编号,部门名称,部门人数,部门平均工资,并按照部门人数升序排序。 +select d.deptno,d.dname,count(e.ename),avg(e.sal) +from dept d +join emp e on d.deptno=e.deptno +group by d.dname,d.deptno +having(count(e.ename)>2) and (avg(e.sal)>2000) +order by count(e.ename) ; + +11.查询部门最低工资 高于10号部门 最低工资的部门的编号、名称及部门最低工资。 +SELECT e.deptno,d.dname,MIN(e.sal) +FROM emp e,dept d +WHERE e.deptno=d.deptno +GROUP BY deptno,dname +HAVING MIN(e.sal)>(SELECT MIN(sal) from emp WHERE deptno=10) + +12.显示30号部门的雇员所从事的不重复的工作名称,并显示30部门的所在地。 +SELECT DISTINCT e.job,d.loc +FROM emp e,dept d +WHERE e.deptno=d.deptno AND e.deptno=30; + +13.查询工作在广州的员工人数,最高工资及最低工资。 +select count(*) 员工人数, +max(e.sal) 最高工资, +min(e.sal) 最低工资 +from emp e, dept d +where e.deptno = d.deptno +and d.loc = '广州'; + +14.统计各个部门不同职务的员工薪水的总和,平均工资,部门编号,部门名称,部门位置,职务 +SELECT dept.deptno,dname,loc,JOB,sum(sal),avg(sal) +FROM emp,dept +WHERE emp.deptno=dept.deptno +GROUP BY dept.deptno,dname,loc,JOB; + + diff --git "a/JDBC/bin/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\350\207\252\345\267\261\357\274\211.sql" "b/JDBC/bin/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\350\207\252\345\267\261\357\274\211.sql" new file mode 100644 index 0000000..2c215cd --- /dev/null +++ "b/JDBC/bin/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\350\207\252\345\267\261\357\274\211.sql" @@ -0,0 +1,33 @@ +--1.�г��������ƺ���Щ���ŵ�Ա����Ϣ��ͬʱ�г���Щû��Ա���IJ��� +select dname, e.* from dept d left outer join emp e on d.deptno=e.deptno; +--2.�г���ÿ�����Ź�����Ա��������ƽ�����ʡ� +select count(ename), avg(sal) from emp e right outer join dept d on d.deptno=e.deptno group by d.deptno; +--3.�г�����Ա�����������������ƺ͹��ʡ� +select ename, dname, sal from emp e, dept d where e.deptno=d.deptno; +--4.��ѯÿ�����ŵIJ��ű�ţ��������ƣ�������������߹��ʣ���͹��ʣ������ܺͣ�ƽ������ +select d.deptno, dname, count(*), max(sal), min(sal), sum(sal), avg(sal) from emp e, dept d where d.deptno=e.deptno group by d.deptno, dname; +--5.��ѯÿ��������������������������ţ�����������Ҫ�����û�о�������Ա��Ϣ�� +select count(e.ename), m.empno,m.ename from emp e left join emp m on e.mgr=m.empno group by m.empno,m.ename +--6.��ѯ������������2�IJ��ű�ţ��������ƣ��������� +select d.deptno, d.dname, count(*) from emp e, dept d where e.deptno=d.deptno group by d.deptno, d.dname having count(*)>2; +--7.��ѯ����ƽ��������2500Ԫ���ϵIJ������Ƽ�ƽ�����ʡ� +select d.dname, avg(sal) from emp e, dept d where e.deptno=d.deptno group by d.dname having avg(sal)>2500; +--8.��ѯԱ����λ�в�����"SA"��ͷ����ƽ�������� 2500Ԫ���ϵĸ�λ��ƽ�����ʣ�����ƽ�����ʽ������� +select job, avg(sal) from emp where job not like 'SA%' group by job having avg(sal)>2500 order by avg(sal) desc +--9.��ʾ��������;����������������������Ա������͹��ʣ�û�о�����KINGҲҪ��ʾ�� +--��������͹���С��3000�ģ�����͹����ɸߵ������� +select m.empno, m.ename, min(e.sal) from emp e left outer join emp m +on e.mgr=m.empno group by m.empno, m.ename having min(e.sal)>=3000 order by min(e.sal) desc; +--10.��ѯ����ƽ�����ʴ���2000������������2�IJ��ű�ţ��������ƣ���������������ƽ�����ʣ������ղ��������������� +select d.deptno, d.dname, count(*), avg(sal) from emp e, dept d +where e.deptno=d.deptno group by d.deptno, d.dname +having count(*)>2 and avg(sal)>2000 order by count(*); +--11.��ѯ������͹��� ����10�Ų��� ��͹��ʵIJ��ŵı�š����Ƽ�������͹��ʡ� +select d.deptno, dname, min(sal) from emp e, dept d where d.deptno=e.deptno group by d.deptno, dname +having min(sal)>(select min(sal) from emp where deptno = 10); +--12.��ʾ30�Ų��ŵĹ�Ա�����µIJ��ظ��Ĺ������ƣ�����ʾ30���ŵ����ڵء� +select distinct job, d.loc from emp e, dept d where d.deptno=e.deptno and e.deptno=30; +--13.��ѯ������'CHICAGO'��Ա����������߹��ʼ���͹��ʡ� +select count(empno),max(sal),min(sal) from emp e, dept d where d.deptno=e.deptno and loc='CHICAGO'; +--14.ͳ�Ƹ������Ų�ְͬ���Ա��нˮ���ܺͣ�ƽ������,���ű�ţ���������,����λ�ã�ְ�� +select sum(sal),avg(sal),d.deptno,dname,loc, job from emp e, dept d where d.deptno=e.deptno group by d.deptno,dname,loc, job; diff --git a/JDBC/bin/project/EmpManage_JDBC/bean/Dept.class b/JDBC/bin/project/EmpManage_JDBC/bean/Dept.class new file mode 100644 index 0000000..3194607 Binary files /dev/null and b/JDBC/bin/project/EmpManage_JDBC/bean/Dept.class differ diff --git a/JDBC/bin/project/EmpManage_JDBC/bean/Emp.class b/JDBC/bin/project/EmpManage_JDBC/bean/Emp.class new file mode 100644 index 0000000..b54c3ca Binary files /dev/null and b/JDBC/bin/project/EmpManage_JDBC/bean/Emp.class differ diff --git a/JDBC/bin/project/EmpManage_JDBC/dao/EmpDao.class b/JDBC/bin/project/EmpManage_JDBC/dao/EmpDao.class new file mode 100644 index 0000000..098774f Binary files /dev/null and b/JDBC/bin/project/EmpManage_JDBC/dao/EmpDao.class differ diff --git a/JDBC/bin/project/EmpManage_JDBC/dao/impl/EmpDaoImpl.class b/JDBC/bin/project/EmpManage_JDBC/dao/impl/EmpDaoImpl.class new file mode 100644 index 0000000..39c5605 Binary files /dev/null and b/JDBC/bin/project/EmpManage_JDBC/dao/impl/EmpDaoImpl.class differ diff --git a/JDBC/bin/project/EmpManage_JDBC/test/Test.class b/JDBC/bin/project/EmpManage_JDBC/test/Test.class new file mode 100644 index 0000000..c121b49 Binary files /dev/null and b/JDBC/bin/project/EmpManage_JDBC/test/Test.class differ diff --git a/JDBC/bin/project/EmpManage_JDBC/util/DBUtil.class b/JDBC/bin/project/EmpManage_JDBC/util/DBUtil.class new file mode 100644 index 0000000..322350c Binary files /dev/null and b/JDBC/bin/project/EmpManage_JDBC/util/DBUtil.class differ diff --git a/JDBC/bin/project/carRent_JDBC/bean/Bus.class b/JDBC/bin/project/carRent_JDBC/bean/Bus.class new file mode 100644 index 0000000..cd62ddd Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/bean/Bus.class differ diff --git a/JDBC/bin/project/carRent_JDBC/bean/Car.class b/JDBC/bin/project/carRent_JDBC/bean/Car.class new file mode 100644 index 0000000..008bc86 Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/bean/Car.class differ diff --git a/JDBC/bin/project/carRent_JDBC/bean/Truck.class b/JDBC/bin/project/carRent_JDBC/bean/Truck.class new file mode 100644 index 0000000..d374fa0 Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/bean/Truck.class differ diff --git a/JDBC/bin/project/carRent_JDBC/bean/Vehicle.class b/JDBC/bin/project/carRent_JDBC/bean/Vehicle.class new file mode 100644 index 0000000..26cf782 Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/bean/Vehicle.class differ diff --git a/JDBC/bin/project/carRent_JDBC/dao/Operation.class b/JDBC/bin/project/carRent_JDBC/dao/Operation.class new file mode 100644 index 0000000..75c0afc Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/dao/Operation.class differ diff --git a/JDBC/bin/project/carRent_JDBC/dao/impl/OperationImpl.class b/JDBC/bin/project/carRent_JDBC/dao/impl/OperationImpl.class new file mode 100644 index 0000000..3c5b181 Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/dao/impl/OperationImpl.class differ diff --git a/JDBC/bin/project/carRent_JDBC/test/Test.class b/JDBC/bin/project/carRent_JDBC/test/Test.class new file mode 100644 index 0000000..ca752aa Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/test/Test.class differ diff --git a/JDBC/bin/project/carRent_JDBC/util/DBUtil.class b/JDBC/bin/project/carRent_JDBC/util/DBUtil.class new file mode 100644 index 0000000..8d7efef Binary files /dev/null and b/JDBC/bin/project/carRent_JDBC/util/DBUtil.class differ diff --git a/JDBC/src/Demo/DEMO_EmpManage_JDBC.rar b/JDBC/src/Demo/DEMO_EmpManage_JDBC.rar new file mode 100644 index 0000000..91d61df Binary files /dev/null and b/JDBC/src/Demo/DEMO_EmpManage_JDBC.rar differ diff --git a/JDBC/src/JDBC_day01/demo/JDBCDemo01.java b/JDBC/src/JDBC_day01/demo/JDBCDemo01.java new file mode 100644 index 0000000..ae3caa0 --- /dev/null +++ b/JDBC/src/JDBC_day01/demo/JDBCDemo01.java @@ -0,0 +1,149 @@ +package JDBC_day01.demo; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import org.junit.Test; + +import oracle.jdbc.driver.OracleDriver; + +/** + * 1.ʲô��JDBC�� + * JDBC(Java DataBase Connection)��һ������ִ��SQL����Java��API������Ϊ���ֹ�ϵ�����ݿ��ṩͳһ���ʣ�����һ����Java���Ա�д����ͽӿ���ɣ���Java�������ݿ�ı�׼�淶�� + * 2.JDBC���ţ� + * (1)����jar�����������ݿ�������� + * (2)����������:Class.forName("����"); + * (3)�������ӣ�DriverManager(����������)���ṩһ����̬���������������ݿ������,getConnection(url(�������ݿ��ַ),username,password) + */ +public class JDBCDemo01 { + /** + * JDBC�����ĸ������� + * 1.driverClassName:oracle.jdbc.driver.OracleDriver + * 2.url:�������ݿ��ַ,jdbc:oracle:thin:@localhost:1521:orcl + * 3.username: zicheng + * 4.password: qqq + */ + @Test + public void fun() throws ClassNotFoundException, SQLException{ + //1.����������(ע������) + Class.forName("oracle.jdbc.driver.OracleDriver");//�����ȼ��ؾ�̬������е����ݣ���ΪOracleDriver�ľ�̬���������DriverManager.registerDriver(new OracleDriver()); + //DriverManager.registerDriver(new OracleDriver());//Ҳ���Ե��������д���Class.forName("oracle.jdbc.driver.OracleDriver"); + String url="jdbc:oracle:thin:@localhost:1521:orcl"; + String user="zicheng"; + String pwd="qqq"; + //2.�������Ӷ��� + Connection conn = DriverManager.getConnection(url, user, pwd); + //System.out.println(conn); + /* + * 3.ͨ��Connection���󴴽�Statement���� + * (1)ͨ��COnnection���󴴽���Statement�����Ĺ��ܾ��������ݿⷢ��sql��䡣 + * (2)����int executeUpdate(String sql)//�����ݿ������ɾ�IJ���,ִ�и�����SQL��䣬������� INSERT �� UPDATE ���� DELETE��䣬���߲������κ����ݣ���SQL DDL����SQL��䡣 + */ + Statement statement = conn.createStatement(); + //4.ִ��sql��䣨DML�� + String sql="insert into emp(empno,ename,job)values(888,'Tony','boss')";//������� + //int rows = statement.executeUpdate(sql);//Ӱ���˶����� + String sql1="update emp set job='big boss' where empno=888"; + //int rows1 = statement.executeUpdate(sql1); + String sql2 = "delete from emp where job='big boss'"; + //int rows2 = statement.executeUpdate(sql2); + //5.�ͷ���Դ + statement.close(); + conn.close();//����Ҫ�صģ��������������ޣ��������ݿ������� + } + + /** + * ��ѯ + * 1.�õ�Connection + * 2.�õ�Statement���󣬷���SQL��� + * 3.�Բ�ѯ���صĽ�������н��� + * @throws ClassNotFoundException + * @throws SQLException + */ + @Test + public void query() throws ClassNotFoundException, SQLException{ + //1.����������(ע������) + Class.forName("oracle.jdbc.driver.OracleDriver");//�����ȼ��ؾ�̬������е����ݣ���ΪOracleDriver�ľ�̬���������DriverManager.registerDriver(new OracleDriver()); + //DriverManager.registerDriver(new OracleDriver());//Ҳ���Ե��������д���Class.forName("oracle.jdbc.driver.OracleDriver"); + String url="jdbc:oracle:thin:@localhost:1521:orcl"; + String user="zicheng"; + String pwd="qqq"; + //2.�������Ӷ��� + Connection conn = DriverManager.getConnection(url, user, pwd); + //3.ͨ��Connection���󴴽�Statement���� + Statement statement = conn.createStatement(); + /* + * 4.ִ��sql��� + * ResultSet executeQuery(String sql)//ִ�и�����SQL��䣬����䷵�ص��� ResultSet���� + */ + String sql ="select * from emp"; + ResultSet rs = statement.executeQuery(sql); + /* + * 5.����ResultSet + * ���Ե���ResultSet��boolean next()���й���ƶ�������һ��. + * ��ResultSet���������һ���й�ֻ꣨�������ƶ������������ƶ������Ա����������ݽ��б�������ʼ��λ�ڵ�һ��֮ǰ����ֹ��λ�����һ��֮�� + * ResultSet�ṩһϵ�е�getXXX()������getInt(),getString(),getDouble(),getDate(),getObject(). + * getInt(1),getInt(empno);//��ȡ��һ�У���ֱ��д������ + */ + while(rs.next()){//����������ƶ�һ�в��ж��Ƿ���ڡ��������򷵻�true�����򷵻�false�� + int empno = rs.getInt(1);//ͨ���б������ȡ���е�ֵ + String ename = rs.getString("ename");//ͨ����������ȡ���е�ֵ�� + double sal = rs.getDouble("sal"); + String job = rs.getString("job"); + System.out.println(empno+"\t"+ename+"\t"+sal+"\t"+job); + } + //6.�ͷ���Դ + rs.close(); + statement.close(); + conn.close();//����Ҫ�صģ��������������ޣ��������ݿ������� + } + + /** + * �淶д�� + */ + public static void main(String[] args) { + Connection conn = null; + Statement statement = null; + ResultSet rs = null; + try { + Class.forName("oracle.jdbc.driver.OracleDriver");//�����ȼ��ؾ�̬������е����ݣ���ΪOracleDriver�ľ�̬���������DriverManager.registerDriver(new OracleDriver()); + String url="jdbc:oracle:thin:@localhost:1521:orcl"; + String user="zicheng"; + String pwd="qqq"; + conn = DriverManager.getConnection(url, user, pwd); + System.out.println("���ӳɹ���"); + String sql = "select * from emp where ename like '%A%'"; + statement = conn.createStatement(); + rs = statement.executeQuery(sql); + while(rs.next()){//����������ƶ�һ�в��ж��Ƿ���ڡ��������򷵻�true�����򷵻�false�� + int empno = rs.getInt(1);//ͨ���б������ȡ���е�ֵ + String ename = rs.getString("ename");//ͨ����������ȡ���е�ֵ�� + double sal = rs.getDouble("sal"); + String job = rs.getString("job"); + System.out.println(empno+"\t"+ename+"\t"+sal+"\t"+job); + } + } catch (Exception e) { + // TODO: handle exception + }finally{ + try { + if(rs!=null){ + rs.close(); + } + if(statement!=null){ + statement.close(); + } + if(conn!=null){ + conn.close(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } +} + + diff --git a/JDBC/src/JDBC_day01/demo/JDBCDemo02.java b/JDBC/src/JDBC_day01/demo/JDBCDemo02.java new file mode 100644 index 0000000..438f229 --- /dev/null +++ b/JDBC/src/JDBC_day01/demo/JDBCDemo02.java @@ -0,0 +1,73 @@ +package JDBC_day01.demo; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Scanner; + +import org.junit.Test; + +/** + * Java����ʵ���û���¼����֤�û��������롣 + * ��ʾSQLע�빥���� + */ +public class JDBCDemo02 { + + @Test + public void login() { + Connection conn = null; + Statement statement = null; + ResultSet rs = null; + try { + //1.�������ݿ� + Class.forName("oracle.jdbc.driver.OracleDriver");// �����ȼ��ؾ�̬������е����ݣ���ΪOracleDriver�ľ�̬���������DriverManager.registerDriver(new OracleDriver()); + String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String user = "zicheng"; + String pwd = "qqq"; + conn = DriverManager.getConnection(url, user, pwd); + System.out.println("���ӳɹ���"); + + //2.�����û��������뼰sql���ƴ�� + Scanner scanner = new Scanner(System.in); + System.out.print("�������û����� "); + String name = scanner.next(); + System.out.print("���������룺 "); + String password = scanner.next();// SQLע�룺select * from users where username='����'and password='123456'or'a'='a';��ȡ(����������֮��)123456'or'a'='a + // String sql = "select * from users where username='������' and + // password='123456' or 1=1";//SQLע�� + String sql = "select * from users where username='" + name + "'and password='" + password + "'"; + System.out.println(sql); + + //3.ͨ��Connection���󴴽�Statement���� + statement = conn.createStatement(); + //4.ִ��sql��䣨�����Dz�ѯ�� + rs = statement.executeQuery(sql); + //5.��ȡ�����������next()���������ж��û��Ƿ��½�ɹ��� + if (rs.next()) { + System.out.println("��½�ɹ���"); + } else { + System.out.println("�û������������"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally {//6.�ͷ���Դ + try { + if (rs != null) { + rs.close(); + } + if (statement != null) { + statement.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } +} diff --git a/JDBC/src/JDBC_day01/demo/JDBCDemo03.java b/JDBC/src/JDBC_day01/demo/JDBCDemo03.java new file mode 100644 index 0000000..c17dcc0 --- /dev/null +++ b/JDBC/src/JDBC_day01/demo/JDBCDemo03.java @@ -0,0 +1,83 @@ +package JDBC_day01.demo; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Scanner; + +import org.junit.Test; + +/** + * PreparedStatement:Statement���ӽӿڡ� + * Ԥ���루sql�Ƿ�Ϸ��淶������Statement�ӿڵ��ӽӿڣ���ֹSQL������������Ҫ���ܣ�����ߴ���Ŀɶ��ԣ���ά���ԣ����Ч�ʡ� + */ +public class JDBCDemo03 { + @Test + public void login() { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + try { + //1.����������(ע������) + Class.forName("oracle.jdbc.driver.OracleDriver");//�����ȼ��ؾ�̬������е����ݣ���ΪOracleDriver�ľ�̬���������DriverManager.registerDriver(new OracleDriver()); + //DriverManager.registerDriver(new OracleDriver());//Ҳ���Ե��������д���Class.forName("oracle.jdbc.driver.OracleDriver"); + String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String user = "zicheng"; + String pwd = "qqq"; + conn = DriverManager.getConnection(url, user, pwd); + System.out.println("���ӳɹ���"); + + //2.��ȡ���� ���������û��������� + Scanner scanner = new Scanner(System.in); + System.out.print("�������û����� "); + String name = scanner.next(); + System.out.print("���������룺 "); + String password = scanner.next();// SQLע�룺select * from users where username='����'and password='123456'or'a'='a';��ȡ(����������֮��)123456'or'a'='a + // String sql = "select * from users where username='������' and + // password='123456' or 1=1";//SQLע�� + String sql = "select * from users where username=? and password=?"; + + /* + * 3.����PrepareStatementԤ������� + * ����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + * SQL����еIJ���ȫ�������ʺ�ռλ���� + * ����PrepareStatement����setXXX()���������ʺŸ�ֵ�� + */ + ps = conn.prepareStatement(sql);//��Ԥ����sql + ps.setString(1, name);//1��2��ռλ��������ʾ�ڼ����ʺš� + ps.setString(2, password); + //4.���÷�����ִ��sql��䣬��ȡ������� + rs = ps.executeQuery();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeQuery()����Ҫ��sql�����ˡ� + //5.��������� + if (rs.next()) { + System.out.println("��½�ɹ���"); + } else { + System.out.println("�û������������"); + } + } catch (Exception e) { + e.printStackTrace(); + } finally {//6.�ͷ���Դ + try { + if (rs != null) { + rs.close(); + } + if (ps != null) { + ps.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + + + +} diff --git a/JDBC/src/JDBC_day01/demo/JDBCDemo04.java b/JDBC/src/JDBC_day01/demo/JDBCDemo04.java new file mode 100644 index 0000000..9f0873a --- /dev/null +++ b/JDBC/src/JDBC_day01/demo/JDBCDemo04.java @@ -0,0 +1,72 @@ +package JDBC_day01.demo; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Scanner; + +import org.junit.Test; + +/** + * ʹ��PrepareStatement��ʵ�����ݱ��ĸ��²����� + */ +public class JDBCDemo04 { + @Test + public void login() { + Connection conn = null; + PreparedStatement ps = null; + try { + //1.����������(ע������) + Class.forName("oracle.jdbc.driver.OracleDriver");//�����ȼ��ؾ�̬������е����ݣ���ΪOracleDriver�ľ�̬���������DriverManager.registerDriver(new OracleDriver()); + //DriverManager.registerDriver(new OracleDriver());//Ҳ���Ե��������д���Class.forName("oracle.jdbc.driver.OracleDriver"); + String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String user = "zicheng"; + String pwd = "qqq"; + conn = DriverManager.getConnection(url, user, pwd); + System.out.println("���ӳɹ���"); + + //2.��ȡ���Ӷ��������û��������� + Scanner scanner = new Scanner(System.in); + System.out.print("������Ҫ�޸ĵ��û���ţ� "); + int id = scanner.nextInt(); + System.out.print("���������û����� "); + String name = scanner.next(); + String sql = "update users set username=? where id=?"; + + /* + * 3.����PrepareStatementԤ������� + * ����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + * SQL����еIJ���ȫ�������ʺ�ռλ���� + * ����PrepareStatement����setXXX()���������ʺŸ�ֵ�� + */ + ps = conn.prepareStatement(sql);//��Ԥ����sql + ps.setString(1, name);//1��2��ռλ��������ʾ�ڼ����ʺš� + ps.setInt(2, id); + + int rows = ps.executeUpdate();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeUpdate()����Ҫ��sql�����ˡ� + System.out.println(rows); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if (ps != null) { + ps.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + } + + + +} diff --git a/JDBC/src/JDBC_day01/lib/ojdbc5.jar b/JDBC/src/JDBC_day01/lib/ojdbc5.jar new file mode 100644 index 0000000..4c02ee3 Binary files /dev/null and b/JDBC/src/JDBC_day01/lib/ojdbc5.jar differ diff --git "a/JDBC/src/JDBC_day01/note/JDBC\345\216\237\347\220\206.jpg" "b/JDBC/src/JDBC_day01/note/JDBC\345\216\237\347\220\206.jpg" new file mode 100644 index 0000000..131028f Binary files /dev/null and "b/JDBC/src/JDBC_day01/note/JDBC\345\216\237\347\220\206.jpg" differ diff --git a/JDBC/src/JDBC_day01/note/summary.txt b/JDBC/src/JDBC_day01/note/summary.txt new file mode 100644 index 0000000..12daaeb --- /dev/null +++ b/JDBC/src/JDBC_day01/note/summary.txt @@ -0,0 +1,49 @@ +JDBCDemo01�� + 1.�������� + (1)�������ݿ�/����������(ע������): + 1)Class.forName("oracle.jdbc.driver.OracleDriver");//����������(ע������) + ��DriverManager.registerDriver(new OracleDriver()); + 2)Connection conn = DriverManager.getConnection(url, user, pwd);//�������Ӷ��� + (2)Statement statement = conn.createStatement();//ͨ��Connection���󴴽�Statement���� + (3)��ɾ��/��ѯ����Ҫ���Ρ� + (4)�ͷ���Դ + 2.DML��ɾ��:int rows = statement.executeUpdate(sql);//Ӱ���˶�����//ִ��sql��䣨DML��,Ҫ���Ρ� + 3.DQL��ѯ: + (1)ResultSet rs = statement.executeQuery(sql);//ִ��sql��䣨DQL������ȡ�����,Ҫ���Ρ� + (2)ResultSet��boolean next(); + (3)getInt(),getString(),getDouble(),getDate(),getObject();//getInt(1),getInt(empno);//��ȡ��һ�У���ֱ��д������ + 4.�淶д��; + +JDBCDemo02: + Java����ʵ���û���¼����֤�û���������. + ��ʾSQLע�빥���� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ�� + 3.ͨ��Connection���󴴽�Statement���� + 4.ִ��sql��䣨�����Dz�ѯ�� + 5.��ȡ�����������next()���������ж��û��Ƿ��½�ɹ��� + 6.�ͷ���Դ + +JDBCDemo03: + ʹ��PrepareStatement��ʵ�����ݱ��IJ�ѯ������ + PreparedStatement:Ԥ���루sql�Ƿ�Ϸ��淶������Statement�ӿڵ��ӽӿڣ���ֹSQL������������Ҫ���ܣ�����ߴ���Ŀɶ��ԣ���ά���ԣ����Ч�ʡ� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ��(�������ã�����ƴ��)//String sql = "select * from users where username=? and password=?"; + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.���÷�����ִ��sql��䣬��ȡ����� + rs = ps.executeQuery();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeQuery()����Ҫ��sql�����ˡ� + 5.��������� + 6.�ͷ���Դ + +JDBCDemo04: + ʹ��PrepareStatement��ʵ�����ݱ��ĸ��²����� + 1.�������ݿ� + 2.����Ҫ�޸ĵ��ñ�ź����û�����sql���ƴ��(�������ã�����ƴ��) + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.ִ��sql��� + int rows = ps.executeUpdate();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeUpdate()����Ҫ��sql�����ˡ� + 5.�ͷ���Դ \ No newline at end of file diff --git a/JDBC/src/JDBC_day02/demo/JDBCDemo01.java b/JDBC/src/JDBC_day02/demo/JDBCDemo01.java new file mode 100644 index 0000000..906b63a --- /dev/null +++ b/JDBC/src/JDBC_day02/demo/JDBCDemo01.java @@ -0,0 +1,62 @@ +package JDBC_day02.demo; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; +import java.util.Scanner; + +import org.junit.Test; + +import JDBC_day02.demo.util.DBUtil; + +/** + * ʹ��PrepareStatement��ʵ��ģ����ѯ�� + */ +public class JDBCDemo01 { + @Test + public void fuzzyQuery() { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + try { + //1.�������ݿ� + conn = DBUtil.getConnection(); + + //2.��ȡ���Ӷ��������û��������� + Scanner scanner = new Scanner(System.in); + System.out.print("�������ѯ������ "); + String condition = scanner.next(); + String sql = "select * from emp where ename like ?"; + + /* + * 3.����PrepareStatementԤ������� + * ����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + * SQL����еIJ���ȫ�������ʺ�ռλ���� + * ����PrepareStatement����setXXX()���������ʺŸ�ֵ�� + */ + ps = conn.prepareStatement(sql);//��Ԥ����sql + ps.setString(1, "%"+condition+"%"); + + rs = ps.executeQuery(); + while(rs.next()){ + int empno = rs.getInt("empno"); + String ename = rs.getString("ename"); + String job = rs.getString("job"); + System.out.println(empno+"\t"+ename+"\t"+job); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + DBUtil.close(conn, ps, rs); + } + + } + + + +} diff --git a/JDBC/src/JDBC_day02/demo/JDBCDemo02.java b/JDBC/src/JDBC_day02/demo/JDBCDemo02.java new file mode 100644 index 0000000..3c5a164 --- /dev/null +++ b/JDBC/src/JDBC_day02/demo/JDBCDemo02.java @@ -0,0 +1,51 @@ +package JDBC_day02.demo; + +import java.sql.*; +import java.util.*; +import org.junit.Test; + +import JDBC_day02.demo.bean.Users; +import JDBC_day02.demo.util.DBUtil; + +/** + * JDBC��ȡ���ݱ�users��ÿ�����ݷ�װ��USers��Ķ����С� + */ +public class JDBCDemo02 { + @Test + public void fuzzyQuery() { + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + try { + conn = DBUtil.getConnection(); + + String sql = "select * from users"; + + ps = conn.prepareStatement(sql);//��Ԥ����sql + + rs = ps.executeQuery(); + + List list = new ArrayList();//�������϶��� + + while(rs.next()){//��ȡ�������ÿ�е����ݣ���װ��Users������ + int id = rs.getInt("id"); + String username = rs.getString("username"); + String password = rs.getString("password"); + Users user = new Users(id,username,password); + list.add(user); + } + for(Users user: list){ + System.out.println(user.toString()); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + DBUtil.close(conn, ps, rs); + } + + } + + + +} diff --git a/JDBC/src/JDBC_day02/demo/JDBCDemo03.java b/JDBC/src/JDBC_day02/demo/JDBCDemo03.java new file mode 100644 index 0000000..8e4e3fb --- /dev/null +++ b/JDBC/src/JDBC_day02/demo/JDBCDemo03.java @@ -0,0 +1,42 @@ +package JDBC_day02.demo; + +import java.sql.*; + +import org.junit.Test; + +import JDBC_day02.demo.util.DBUtil; + +/* + * ����ת�� + */ +public class JDBCDemo03 { + @Test + public void account(){ + Connection conn = null; + Statement s = null; + try{ + conn = DBUtil.getConnection(); + s = conn.createStatement(); + + String sql1 = "update t_account set balance=balance+20000 where id=1"; + String sql2 = "update t_account set balance=balance-20000 where id=2"; + + conn.setAutoCommit(false);//��������Զ��ύȡ���� + s.executeUpdate(sql1); + s.executeUpdate(sql2); + conn.commit();//�ֶ��ύ���񣬿ɽ�������߼�ȫ��������һ�������С� + + System.out.println("ת�˳ɹ�!"); + }catch(Exception e){ + try { + System.out.println("������!"); + conn.rollback();//�ع����� + } catch (SQLException e1) { + e1.printStackTrace(); + } + e.printStackTrace(); + }finally { + DBUtil.close(conn, s, null); + } + } +} diff --git a/JDBC/src/JDBC_day02/demo/bean/Users.java b/JDBC/src/JDBC_day02/demo/bean/Users.java new file mode 100644 index 0000000..f8902ca --- /dev/null +++ b/JDBC/src/JDBC_day02/demo/bean/Users.java @@ -0,0 +1,53 @@ +package JDBC_day02.demo.bean; + +public class Users { + + private int id; + private String username; + private String password; + + public Users() { + + } + + public Users(int id, String username, String password) { + this.id = id; + this.username = username; + this.password = password; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "Users [id=" + id + ", username=" + username + ", password=" + password + "]"; + } + + + + + + +} diff --git a/JDBC/src/JDBC_day02/demo/util/DBUtil.java b/JDBC/src/JDBC_day02/demo/util/DBUtil.java new file mode 100644 index 0000000..63ca751 --- /dev/null +++ b/JDBC/src/JDBC_day02/demo/util/DBUtil.java @@ -0,0 +1,46 @@ +package JDBC_day02.demo.util; + +import java.sql.*; + +/* + * ʵ��JDBC�Ĺ����� + * ���巽����ֱ�ӷ������ݿ�����Ӷ��� + * �ر�������Դ + */ +public class DBUtil { + //�������ݿ�ķ�װ���� + public static Connection getConnection(){ + Connection conn = null; + try { + //�������� + Class.forName("oracle.jdbc.driver.OracleDriver"); + String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String user = "zicheng"; + String password = "qqq"; + //��ȡ���Ӷ��� + conn = DriverManager.getConnection(url, user, password); + System.out.println("ʹ��DBUtil����Oracle�ɹ�!"); + } catch (Exception e) { + e.printStackTrace(); + } + return conn; + } + + //�ر��������� + public static void close(Connection conn, Statement statement, ResultSet rs){//Statement + try { + if(rs != null){ + rs.close(); + } + if (statement != null) { + statement.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/JDBC/src/JDBC_day02/note/summary.txt b/JDBC/src/JDBC_day02/note/summary.txt new file mode 100644 index 0000000..8d8a551 --- /dev/null +++ b/JDBC/src/JDBC_day02/note/summary.txt @@ -0,0 +1,62 @@ +DBUtil: + 实现JDBC的工具类 + 定义静态方法,直接返回数据库的连接对象 + 定义静态方法,关闭所有资源 + +JDBCDemo01: + 使用了DBUtil工具类 + 使用PrepareStatement,实现模糊查询:ps.setString(1, "%"+condition+"%"); + +JDBCDemo02: + JDBC读取数据表users,每行数据封装到USers类的对象中。 + +JDBCDemo03: + 银行转账 + conn.setAutoCommit(false);//将事务的自动提交取消。 + conn.commit();//手动提交事务,可将上面的逻辑全部控制在一个事务中。 + conn.rollback();//回滚事务。 + +EmpManage_JDBC: + 1.bean:实体类 + 2.dao:实现类(包)daoimpl,接口 + 3.test:测试类 + 4.DBUtil: + 静态--连接数据库的方法 + public static Connection getConnection (){ + Connection conn = null; + try{ + conn = Class.forName("jdbc.oracle.driver.OracleDriver"); + String url = ? + String username = ? + String password = ? + conn = DriverManager.getConnection(url, username, password); + }catch(Exception e){ + e.printStackTrace(); + } + return conn; + }; + 静态--关闭资源的方法 + public static void close(Connection conn,Statement ps,ResultSet rs) { + try { + if(rs != null) { + rs.close(); + } + if(ps != null) { + ps.close(); + } + if(conn != null) { + con.close(); + } + }catch(SQLException e) { + e.printStackTrace(); + } + } + +补充: + 1.sql.date转util.date可以自动转换。 + util.date--->sql.date: java.sql.Date date = new java.sql.Date(emp.getHiredate().getTime()); + //sql.Date的构造可以传1970的毫秒值,util.Date的getTime()可以得到1970的毫秒值。 + 2.String--->util.Date + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + emp.setHiredate(sdf.parse(sc.next()));//将String--->util.date,会抛出ParseException异常。 + diff --git a/JDBC/src/note/JDBC_day01/JDBC_day01_summary.txt b/JDBC/src/note/JDBC_day01/JDBC_day01_summary.txt new file mode 100644 index 0000000..12daaeb --- /dev/null +++ b/JDBC/src/note/JDBC_day01/JDBC_day01_summary.txt @@ -0,0 +1,49 @@ +JDBCDemo01�� + 1.�������� + (1)�������ݿ�/����������(ע������): + 1)Class.forName("oracle.jdbc.driver.OracleDriver");//����������(ע������) + ��DriverManager.registerDriver(new OracleDriver()); + 2)Connection conn = DriverManager.getConnection(url, user, pwd);//�������Ӷ��� + (2)Statement statement = conn.createStatement();//ͨ��Connection���󴴽�Statement���� + (3)��ɾ��/��ѯ����Ҫ���Ρ� + (4)�ͷ���Դ + 2.DML��ɾ��:int rows = statement.executeUpdate(sql);//Ӱ���˶�����//ִ��sql��䣨DML��,Ҫ���Ρ� + 3.DQL��ѯ: + (1)ResultSet rs = statement.executeQuery(sql);//ִ��sql��䣨DQL������ȡ�����,Ҫ���Ρ� + (2)ResultSet��boolean next(); + (3)getInt(),getString(),getDouble(),getDate(),getObject();//getInt(1),getInt(empno);//��ȡ��һ�У���ֱ��д������ + 4.�淶д��; + +JDBCDemo02: + Java����ʵ���û���¼����֤�û���������. + ��ʾSQLע�빥���� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ�� + 3.ͨ��Connection���󴴽�Statement���� + 4.ִ��sql��䣨�����Dz�ѯ�� + 5.��ȡ�����������next()���������ж��û��Ƿ��½�ɹ��� + 6.�ͷ���Դ + +JDBCDemo03: + ʹ��PrepareStatement��ʵ�����ݱ��IJ�ѯ������ + PreparedStatement:Ԥ���루sql�Ƿ�Ϸ��淶������Statement�ӿڵ��ӽӿڣ���ֹSQL������������Ҫ���ܣ�����ߴ���Ŀɶ��ԣ���ά���ԣ����Ч�ʡ� + 1.�������ݿ� + 2.�����û��������뼰sql���ƴ��(�������ã�����ƴ��)//String sql = "select * from users where username=? and password=?"; + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.���÷�����ִ��sql��䣬��ȡ����� + rs = ps.executeQuery();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeQuery()����Ҫ��sql�����ˡ� + 5.��������� + 6.�ͷ���Դ + +JDBCDemo04: + ʹ��PrepareStatement��ʵ�����ݱ��ĸ��²����� + 1.�������ݿ� + 2.����Ҫ�޸ĵ��ñ�ź����û�����sql���ƴ��(�������ã�����ƴ��) + 3.����PrepareStatementԤ�������,��ΪPrepareStatement�ǽӿڣ���ʵ����ΪStatement���� + 1)ps = conn.prepareStatement(sql);//��Ԥ����sql.//����Connection�ӿڵķ���prepareStatement����ȡPrepareStatement�ӿڵ�ʵ���ࡣ + 2)ps.setString(1, name);//����PrepareStatement����setXXX()���������ʺŸ�ֵ��1��2��ռλ��������ʾ�ڼ����ʺš� + 4.ִ��sql��� + int rows = ps.executeUpdate();//��ΪprepareStatement�Ѿ�Ԥ�����sql������statement.executeUpdate()����Ҫ��sql�����ˡ� + 5.�ͷ���Դ \ No newline at end of file diff --git "a/JDBC/src/note/JDBC_day01/JDBC\345\216\237\347\220\206.jpg" "b/JDBC/src/note/JDBC_day01/JDBC\345\216\237\347\220\206.jpg" new file mode 100644 index 0000000..131028f Binary files /dev/null and "b/JDBC/src/note/JDBC_day01/JDBC\345\216\237\347\220\206.jpg" differ diff --git a/JDBC/src/note/JDBC_day02/JDBC_day02_summary.txt b/JDBC/src/note/JDBC_day02/JDBC_day02_summary.txt new file mode 100644 index 0000000..9a00174 --- /dev/null +++ b/JDBC/src/note/JDBC_day02/JDBC_day02_summary.txt @@ -0,0 +1,17 @@ +DBUtil: + 实现JDBC的工具类 + 定义静态方法,直接返回数据库的连接对象 + 定义静态方法,关闭所有资源 + +JDBCDemo01: + 使用了DBUtil工具类 + 使用PrepareStatement,实现模糊查询:ps.setString(1, "%"+condition+"%"); + +JDBCDemo02: + JDBC读取数据表users,每行数据封装到USers类的对象中。 + +JDBCDemo03: + 银行转账 + conn.setAutoCommit(false);//将事务的自动提交取消。 + conn.commit();//手动提交事务,可将上面的逻辑全部控制在一个事务中。 + conn.rollback();//回滚事务。 diff --git a/JDBC/src/note/project/one/project_EmpManage_summary b/JDBC/src/note/project/one/project_EmpManage_summary new file mode 100644 index 0000000..8a69bae --- /dev/null +++ b/JDBC/src/note/project/one/project_EmpManage_summary @@ -0,0 +1,43 @@ +EmpManage_JDBC�� + 1.bean:ʵ���� + 2.dao:ʵ����(��)daoimpl���ӿ� + 3.test:������ + 4.DBUtil: + ��̬--�������ݿ�ķ��� + public static Connection getConnection (){ + Connection conn = null; + try{ + conn = Class.forName("jdbc.oracle.driver.OracleDriver"); + String url = �� + String username = �� + String password = �� + conn = DriverManager.getConnection(url, username, password); + }catch(Exception e){ + e.printStackTrace(); + } + return conn; + }; + ��̬--�ر���Դ�ķ��� + public static void close(Connection conn,Statement ps,ResultSet rs) { + try { + if(rs != null) { + rs.close(); + } + if(ps != null) { + ps.close(); + } + if(conn != null) { + con.close(); + } + }catch(SQLException e) { + e.printStackTrace(); + } + } + +���䣺 + 1.sql.dateתutil.date�����Զ�ת���� + util.date--->sql.date�� java.sql.Date date = new java.sql.Date(emp.getHiredate().getTime()); + //sql.Date�Ĺ�����Դ�1970�ĺ���ֵ��util.Date��getTime()���Եõ�1970�ĺ���ֵ�� + 2.String--->util.Date + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + emp.setHiredate(sdf.parse(sc.next()));//��String--->util.date,���׳�ParseException�쳣�� diff --git "a/JDBC/src/note/project/two/JAVA\345\237\272\347\241\200\345\256\236\350\256\255\347\273\203\344\271\240.docx" "b/JDBC/src/note/project/two/JAVA\345\237\272\347\241\200\345\256\236\350\256\255\347\273\203\344\271\240.docx" new file mode 100644 index 0000000..1b1ea81 Binary files /dev/null and "b/JDBC/src/note/project/two/JAVA\345\237\272\347\241\200\345\256\236\350\256\255\347\273\203\344\271\240.docx" differ diff --git a/JDBC/src/note/project/two/carRent_JDBC.sql b/JDBC/src/note/project/two/carRent_JDBC.sql new file mode 100644 index 0000000..f96570d --- /dev/null +++ b/JDBC/src/note/project/two/carRent_JDBC.sql @@ -0,0 +1,52 @@ +--���� +create table car( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + type varchar2(20) not null +); +create table bus( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + seat number(2) not null +); +create table truck( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + tonnage number(2) not null +); +create table rented( + brand varchar2(10) not null, + vehicleId varchar2(20) not null unique, + perRent number(4) not null, + type varchar2(20), + seat number(2), + tonnage number(2) +); +--�������� +insert into car values('����','��NY28588',800,'X6'); +insert into car values('����','��CNY3284',600,'550i'); +insert into car values('���','��NT37465',300,'������'); +insert into car values('���','��NT96968',600,'GL8'); +insert into bus values('��','��6566754',800,16); +insert into bus values('����','��8696997',800,16); +insert into bus values('��','��9696996',1500,34); +insert into bus values('����','��8696998',1500,34); +insert into truck values('һ�����','��MH98725',50,5); +insert into truck values('�������','��L593216',50,5); +insert into truck values('һ�����','��NU98631',50,10); +insert into truck values('�������','��CY56312',50,10); +--�鿴�� +select * from car; +select * from bus; +select * from truck; +select * from rented; +--ɾ���� +drop table car; +drop table bus; +drop table truck; +drop table rented; + + diff --git "a/JDBC/src/note/sql/MySQL \345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.docx" "b/JDBC/src/note/sql/MySQL \345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.docx" new file mode 100644 index 0000000..a4cf19e Binary files /dev/null and "b/JDBC/src/note/sql/MySQL \345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240.docx" differ diff --git a/JDBC/src/note/sql/MySQL.sql b/JDBC/src/note/sql/MySQL.sql new file mode 100644 index 0000000..363ef91 --- /dev/null +++ b/JDBC/src/note/sql/MySQL.sql @@ -0,0 +1,374 @@ +SQL(结构化查询语言),此笔记主要针对于MySQL version 5.5; +1.SQL分类: + DDL:数据定义语言,用于操作数据对象(表,视图,序列,索引)。 + CREATE:创建表或其它对象。 + ALTER:修改表或其它对象结构。 + DROP:删除表或其它对象。 + TRUNCATE:删除表数据,保留表结构。清空表的操作。 + DML:数据操作语言(通常需要伴随TCL),用于增删改表中的数据。 + INSERT INTO:将数据插入表中。 + DELETE:删除表中数据。 + UPDATE SET:更新表中数据。 + TCL:事务控制语言。 + COMMIT:提交。 + ROLLBACK:回滚。 + DQL:数据查询语言 + SELECT:查询数据。 + DCL:数据控制语言。 + GRANT:授予权限。 + REVOKE: 撤销权限。 + grant…create…view…to…scott; +2.Mysql基本介绍: + 启动和停止Mysql服务:mysql是一种C/S结构的软件(客户端和服务器),服务端对应的软件:mysqld.exe, 客户端: mysql.exe; + cmd命令行方式: 开启服务: net start mysql; 关闭服务: net stop mysql; + 系统服务方式: 此电脑,管理,服务,MySQL; + cmd登陆mysql: mysql -uroot -proot, 若未配置环境变量,则需进入到MySQL里的bin目录下再用cmd登陆mysql。 + -uroot:表示用户是root,是默认的用户; + -proot:表示用户的密码是root; + 退出mysql: exit或quit; +3.Mysql服务端架构: + (1)数据库管理系统(最外层):DBMS,专门管理服务器端的所有内容。 + (2)数据库(第二层):DB,专门用于存储数据的仓库(可以有多个)。 + (3)二维数据表(第三层):TABLE,专门用于存储实体的数据。 + (4)字段(第四层):Field,具体存储某种类型的数据(实际的存储单元)。 +4.数据库基本操作: + (1)创建数据库: create database [if not exists] 数据库名 [charset 字符集];--if not exits如果不存在则创建,[可写可不写].字符集若不指定则按默认字符集编码; + create database if not exists t_user charset utf8; + (2)显示数据库: + 显示全部数据库: show databases; + 显示部分数据库: show databases like '匹配模式'; _:匹配当前位置的单个字符;%:匹配指定位置的多个字符; + 获取以my开头的数据库: show databases like 'my%'; + (3)选择数据库: use 数据库名;--use t_user; + (4)修改数据库: + 1)修改数据库字符集: alter database 数据库名 charset 字符集; --alter database t_user charset gbk + 2)删除数据库: drop database 数据库名; --drop database t_user; + (5)数据表操作: + 1)创建数据表: create table 表名(字段名 字段类型[字段属性], 字段名 字段类型[字段属性], ...); + 例子:create table user( + username varchar(10), + password varchar(10) + ); + 2)显示数据表: + 显示所有表: show tables; + 显示部分表: show tables like '匹配模式'; + 3)显示表结构: desc 表名; + 4)修改表结构: 5.5版本之后的数据库名不能修改 + 修改表名: rename table 旧表名 to 新表名; --rename table user to t_user; + 修改表选项: alter table 表名 charset utf8; + 新增字段: alter table 表名 add 新字段名 字段类型[字段属性] [位置first/after字段名]; --alter table t_user add id int first; alter table t_user add age int; + 修改字段名: alter table 表名 change 旧字段名 新字段名 字段类型[字段属性] [新位置]; --alter table t_user change age nianling int; + 修改字段类型: alter table 表名 modify 字段名 新类型[新属性] [新位置]; --alter table t_user modify username varchar(20); + 删除字段: alter table 表名 drop 字段名; --alter table t_user drop nianling; + 5)删除表结构: drop table 表名[,表名2];可以同时删除多个数据表。 + 6)清空表中数据: truncate table 表名;不可以同时清空多个数据表。 +5.数据基础操作: + 插入操作: insert into 表名[(字段列表)] values(对应字段列表); + insert into 表名 values(对应全部字段); + 查询操作: select * from 表名; --*表示匹配所有的字段. + select 字段列表 from 表名;--字段列表使用","分隔. + 删除操作: delete from 表名 where 条件; --通常删除部分数据,所以加where条件。 + truncate table 表名; + 更新操作: update 表名 set 字段名=新值 where 条件; --通常修改部分字段的值,所以加where条件。 +6.列类型(字段类型): + 1)整数类型: + Tinyint: 迷你整形,系统采用一个字节来保存的整形,1个字节等于8位,最大能表示的数值是0~255. + mysql默认的为整形增加负数,实际表示的区间为-128~127. + Smallint: 小整形,系统采用两个字节来保存的整形,能表示0~65535. + Mediumint: 中整形,系统采用三个字节来保存数据. + int: 整形,标准整形系统采用四个字节来保存数据. + bigint: 比int范围还要大. + 例子: + 创建数据表: + create table t_int( + num1 tinyint, + num2 smallint, + num3 mediumint, + num4 int, + num5 bigint + ); + insert into t_int values(-128,255,255,255,255); + insert into t_int values(255,255,255,255,255); --会报错,因为最大范围为-128~127. + 无符号标识设定: + 无符号: 表示存储的数据再当前字段中没有负数(只有正数,区间为0~255). + 在类型之后加上一个unsigned。 + alter table t_int add num6 tinyint unsigned first; + insert into t_int values(255,-128,255,255,255,255); + 2)小数类型:浮点型和定点型; + 浮点型(精度类型): 是一种有可能丢失精度的数据类型,数据有可能不那么准确(尤其是在超出范围的时候). + float: 单精度浮点型(4个字节),大概7位左右的精确度。float(M,D)表示一共存储M个有效数字,其中小数部分占D位。 + 例子: + create table t_float( + f1 float, + f2 float(7,2), + f3 float(12,2) + ); + insert into t_float values (123.456,12345.67,12345678.90);--f3的数据会丢失,12345679.00; 注意: 如果数据精度丢失,那么浮点型是按照四舍五入的方式进行计算。 + double: 双精度浮点型(8个字节),大概15位左右的精确度,数据有可能不那么准确(尤其是在超出范围的时候). + 定点型:能够保证精确的小数(小数部分可能不精确,超出长度会四舍五入),整数部分一定精确。涉及到钱的时候有可能使用定点数。 + decimal(M,D):M表示总长度,最大值不能超过65,D表示小数部分长度,最长不能超过30。 + 例子: + create table t_decimal( + f1 float(10,2), + d2 decimal(10,2) + ); + insert into t_decimal values(12345678.90,12345678.90);--f1:12345679.00 | d2:12345678.90 + insert into t_decimal values(99999999.99,99999999.99);--f1:100000000.00 | d2:99999999.99 + insert into t_decimal values(99999999.99,99999999.999);--error + 3)时间日期类型: + Date:日期类型 YYYY-mm-dd + Time:时间类型 HH:ii:ss + Datetime:日期时间类型 YYYY-mm-dd HH:ii:ss + Timestamp:时间戳类型 mysql中的时间戳只是表示从格林威治时间开始,但是格式依然是 YYYY-mm-dd HH:ii:ss + 4)字符串类型: + char: 定长字符串,指定长度后,系统一定会分配指定的空间用于存储数据;char(L) L(0~255)代表字符数,哪怕只使用了一个字符,系统也分配L个字符长度; + varchar: 变长字符串,指定长度后,系统会根据实际存储的数据来计算长度分配合适的空间; varchar(L) L(0~65535)代表字符数; + text: 长文本; +7.列属性(字段属性): 在mysql中一共有6个属性null,默认值,列描述,主键,唯一键,自动增长; + 创建数据表: + 1)not null/default: + create table t_user( + name varchar(10) not null, + age int default 18 + ); + insert into t_user(name) values('Tony'); insert into t_user values('Tom',default); + 2)comment: + create table t_user( + name varchar(10) not null comment '用户名非空', --用户名不能为空 + age int default 18 + ); + 查看comment必须通过查看建表语句: show create table t_user; + 3)primary key: 主键,在同一张表内有且仅有一个字段,里面的值具有唯一性;主键一旦增加,那么对应的字段有数据要求:当前字段不能为、当前字段不能重复; + 创建主键: + 随表创建: + 第一种:直接在需要当作主键的字段后,增加primary key属性来确定主键; + create table t_user( + name varchar(10) primary key, + age int + ); + 第二种:在所有字段之后增加primary key选项, primary key(字段信息); + create table t_user( + name varchar(10), + age int, + primary key(name) + ); + 表后增加: alter table 表名 add primary key(字段); + 4)auto_increment:自动增长,当给定某个字段的某个属性之后,该列的数据在没有提供确定数据的时候,系统会根据之前已经存在的数据进行自动递增后,填充数据; + alter table t_user add id int auto_increment; + create table t_user( + id int auto_increment, + name varchar(10), + age int, + primary key(id) + ); + insert into t_user(name,age) values('Tony',18);--id=1; + insert into t_user values(2,'Mary',18);--id=2; + insert into t_user(name,age) values('Jack',18);--id=3; + 自动增长通常用于逻辑主键. + 5)unique key:唯一键,用来保证对应的字段中的数据是唯一的;主键也可以用来保证字段数据唯一性,但是一张表中只能有一个主键; + 要求:一张表中可以有多个;允许字段数据为null,null可以有多个(null不参与比较); + 创建唯一键: + 直接在表字段之后增加唯一键标识符: unique [key] + 在所有的字段之后使用 unique key(字段列表[,字段列表,...]) + 在创建完表后也可以增加唯一键; alter table 表名 add unique key(字段列表[,字段列表,...]); +8.数据查询: + (1)简单查询: + 查询所有列: select * from 表名; + 查询指定列: select 字段1, 字段2 from 表名; + 去重: select distinct 字段 from 表名; + 在查询列表中可以使用算术表达式: select empno as "员工编号", ename '员工姓名', sal*12 年薪 from emp;--起别名时: as可以使用,也可以不使用;别名可以使用单双引号,甚至可以不用引号; + (2)条件查询: select 字段列表 from 表名 [where 查询条件]; + 1)使用=作为查询条件或使用<>和!=作为查询条件或使用<,<=,>,>=作为查询条件,适用于数据库的三种主要类型(数值型,字符串类型,日期型);注意:在使用字符串类型和日期类型的数据时,必须要使用''或""; + select * from emp where sal = 800;--查询工资是800的员工信息; + select * from emp where ename = '武松';--查询员工姓名是"武松"的员工信息; + select * from emp where hiredate != "1981-02-22";--查询雇佣日期不是"1981-02-22"的员工信息; + select * from emp where sal <> 800;--查询工资不是800的员工信息; + 补充: + mysql在window平台上面默认不区分大小写,linux平台上区分大小写;--数据库里的名字是"SMITH"; + select ename from emp where binary ename="smith";--我们可以使用binary让mysql在window平台上也严格区分大小写; + select * from emp where binary lower(ename)="smith";--在数据库严格区分大小写的情况下,忽略字符串大小写; + 2)使用between and 作为查询条件,在特定范围之内: + select * from emp where sal between 1600 and 3000;--包括1600和3000; + select * from emp where hiredate between "1981-02-22" and "1981-05-01"; + 3)使用is null或is not null作为查询条件,对于null的判断不能使用=和!=和<>,而是要使用 is null进行判断; + 4)使用and作为查询条件,and也可以使用&&来代替; + select * from emp where sal>1200 && deptno=20;--查询工资大于1200并且部门号为20的员工的信息; + 5)使用or作为查询条件,or也可以使用||来代替; + 6)and的优先级比or高: + select * from emp where sal>1200&&(deptno=20||30);--查询工资大于1200并且部门编号为20或30的员工; + 7)使用in作为查询条件,in表示包含的意思: + select * from emp where job in("推销员","分析师"); + 8)使用not作为查询条件: + select * from emp where sal!=1600 and sal!=3000; + select * from emp where sal not in(1600,3000); + select * from emp where sal not(sal=1600 or sal=3000); + 9)使用like执行模糊查询: %表示0~多个任意的字符, _表示一个任意的字符; + select * from emp where ename like "李%";--查询名字以"李"开头的员工信息; + 10)使用order by子句给查询结果排序: 可以按照单个字段排序,也可以按照多个字段排序; + select * from emp order by sal desc;--asc升序(默认升序,可不写),desc降序; + 按照多个字段排序,每个字段可以指定不同的排序规则 + select * from emp where sal>1200 order by deptno [asc], sal desc;--查询工资大于1200的员工信息,并按照deptno升序(默认升序,可不写)和sal降序排列; + (3)数据库函数: + 单行函数: + 1)lower(str): 把字符型数据转换为小写的字符; + 2)upper(str): 把字符型数据转换为大写的字符; + 3)substr(被截取字符串的名字,起始下标,截取长度): 截取字符串; + select substr(ename,2,1) from emp;--从员工名字的第二个字符开始截取,截取一个字符长度; + 4)length(str): 返回字符串占用的字节数;gbk一个汉字占两个字节,utf8是三个字节; + 5)char_length(str): 返回字符串中有多少个字符; + select ename, length(ename)/3 from emp;--查询员工的名字以及名字的长度; + select ename, char_length(ename) from emp; + 6)trim(): 去除字符串型数据的前后空格; + select trim(" 1 2 3 ");--1 2 3,oracle中必须写某一个表,mysql中不写的话,默认从系统表中查询; + 7)round(): 对数值型数据进行四舍五入操作; + select round(176.56);--默认精确到个位; + select round(176.56,1);--精确到小数点后1位; + select round(176.56,-1);--精确到小数点前1位; + 8)ifnull(): 空值处理函数,null表示没有数据;在算术中如果出现null,结果肯定是null; + select (sal+ifnull(comm,0))*12 "年薪" from emp; + 9)case when函数: 执行分支语句的函数; + --匹配工作岗位,当为经理时,薪水上调10%,当为分析师时,薪水上调50%,其它岗位不变; + select empno,ename,job,sal as '原来的薪水', (case job when '经理' then sal*1.1 when '分析师' then sal*1.5 else sal end) as '新的薪水' from emp; + 10)str_to_date(str,format): 把字符串转换为日期的函数 + select * from emp where hiredate = "1981-02-22"; + --mysql会自动的调用str_to_date(str,format)函数,把字符型数据转换为日期型的数据,然后再去和hiredate进行比较; + --mysql默认的日期格式是"年-月-日";上面语句的实际执行过程如下: + select * from emp where hiredate = str_to_date("1981-02-22","%Y-%m-%d");--注意: 使用str_to_date(str,format)的时候,日期字符串需与format字符串的格式一致,转换才能成功; + --在mysql中提供了一个now()函数,可以获得当前时间点的日期对象(年月日); + update emp set hiredate=now() where empno=7521; + 11)date_format(date,format): 把日期型的数据转换为特定格式的字符串 + select ename, hiredate from emp; + --mysql会自动的调用date_format(date,format)函数,把日期型数据转换为字符串型的数据;实际执行过程如下: + select ename,date_format(hiredate,"%Y-%m-%d") from emp;--只要指定一个日期模板就可以把日期型的数据转换为任意格式的字符串; + 多行函数(聚合函数,分组函数): + 注意事项: + 1)多行函数会自动忽略空值(null),不需要手动用where排除空值; + 2)多行函数不能出现在where子句中; + 3)多行函数不能嵌套; + 常用多行函数: + 1)sum(): 求总和; + --查询emp表的月收入总和: + select sum(sal+comm) from emp;--错误的,将sal+comm的结果不为null的求和了; + select sum(sal+ifnull(comm,0)) from emp;--正确的; + select sum(sal)+sum(comm) from emp;--正确的; + 2)count(): 查询数据总和; + count(*): 查询所有的 记录总数; + count(字段): 查询指定字段不为null的数据总数; + 3)avg(): 查询平均值; select avg(sal) from emp; + 4)max(): 查询最大值; select max(sal),max(hiredate) from emp; + 5)min(): 查询最小值; select min(sal),min(hiredate) from emp; + (4)分组查询: 分组查询需要使用group by子句;把数据按照特定的条件划分为多组,然后分别使用组函数进行查询;把数据划分为多少组,最终就会得到多少条结果; + 语法: select 查询列表 from 表名 group by 分组字段; + 规则: 出现在查询列表中的字段,要么出现在组函数中,要么出现在group by中;或者分组字段仅仅出现在group by子句中; + 例子: + 1)按照单个字段进行分组: + select deptno,max(sal) from emp group by deptno;--把所有的员工按照部门编号分组,查询最高工资; + 按照部门编号分组,查询最高工资以及最高工资的员工姓名 + select ename,max(sal),deptno from emp group by deptno;--错误的,出现在查询列表中的ename即没有出现在组函数中,也没有出现在group by子句中,这是不符合规则的;这样的语句在Oracle中无法执行,mysql中能够执行,但结果不符合预期需求; + 2)按照多个字段进行分组: + select deptno,job,max(sal) from emp group by deptno,job;--按照部门编号和职位进行分组,查询最高工资; + 3)对分组之后的数据进行过滤,需要使用having子句: + select deptno,avg(sal) as '平均工资' from emp group by deptno having avg(sal)>2000;--按照部门编号分组,查询平均工资,并且显示平均工资大于2000的记录; + select deptno,avg(sal) avgsal from emp group by deptno having avgsal>2000;--与上一条等效; + select deptno,avg(sal) from emp where comm is null group by deptno having avg(sal)>2000 order by avg(sal);--把津贴为null的员工按照部门编号分组查询平均工资,并且显示平均工资大于2000的数据,然后按照平均工资排序(升序); + 4)查询句式及顺序: + select 查询列表 + from 表名 + where 查询条件 + group by 分组条件 + having 查询条件 + order by 排序条件 + from,where,group by,having,select,order by; + (5)limit子句(方言): 用来限定查询结果的起始行以及总行数。 + 分页查询:limit 起始行,页大小; 起始行=(页的索引-1)*页大小; 页大小=页大小; + 例如:select * from emp limit 4,3;--查询起始行为第5行,一共查询3行记录。 + 例子:EmpManageByMysql: https://github.com/ZichengQu/Java/tree/JavaWeb/JSP/EmpManageByMysql + public static Connection getConnection(){//使用JDBC连接mysql数据库 + Connection conn = null; + try { + //加载驱动 + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/empdb";//指定连接的数据库名称为empdb + String user = "root"; + String password = "root"; + //获取连接对象 + conn = DriverManager.getConnection(url, user, password); + System.out.println("使用DBUtil连接Oracle成功!"); + } catch (Exception e) { + e.printStackTrace(); + } + return conn; + } + (6)多表查询: + 按照连接的方式: + 1)内连接: 可以查询满足一一对应关系的数据: 例如这个员工有所属的部门,这个部门有所属的员工,这样的数据满足一一对应的关系,可以使用内连接查询出来; + 内连接分类: + 等值连接: 建立在父子表关系上,用等号来连接两个表; + select ename,sal,job,e.deptno,dname from emp e, dept where e.deptno=dept.deptno and e.deptno=20;--查询员工表及其部门信息; + select ename,sal,job,e.deptno,dname from emp e inner join dept on e.deptno=dept.deptno and e.deptno=20;--可以使用and或where在其后追加条件; + 非等值连接: 两个表之间没有父子关系,用非等号来连接两个表; + select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal;--CREATE TABLE salgrade(grade INT PRIMARY KEY, losal INT, hisal INT);插入数据; + select e.empno, e.ename, e.sal, s.grade from emp e INNER JOIN salgrade s on e.sal between s.losal and s.hisal;--与上一条等效; + 自连接: 使用别名将一个表虚拟成两个表(父子表),然后再这两个表上作等值连接; + select e.empno "员工编号",e.ename "员工姓名",m.empno "经理编号", m.ename "经理姓名" from emp e,emp m where e.mgr=m.empno;--查询员工的信息及其直接领导的信息 + 2)外连接: 可以查询不满足一一对应关系的数据: 例如有的员工没有所属部门,有的部门没有员工,这样的数据不满足一一对应的关系,可以使用外连接查询出来; + 外连接分类: + 左外连接(left outer join): 可以把左表中不满足对应关系的数据查询出来; + select * from emp left join dept on emp.deptno=dept.deptno; + 右外连接(right outer join): 可以把右表中不满足对应关系的数据查询出来; + select * from emp right join dept on emp.deptno=dept.deptno; + 全外连接(full outer join)(不支持): 可以把左右两个表中不满足对应关系的数据查询出来; + select * from emp full join dept on emp.deptno=dept.deptno;--mysql不支持全外连接,因此此查询语句会报错; + (7)子查询: 用来给主查询提供查询条件或查询数据而首先执行的一个查询;主查询使用子查询的结果;子查询必须要放在()里面; + 子查询分类: + 1)出现在where中的子查询,用来给主查询提供查询条件; + select * from emp where sal<(select avg(sal) from emp);--查询工资比平均工资低的员工信息; + 2)出现在from后面的子查询,用来给主查询提供数据的; + 3)出现在查询列表中的子查询,功能类似于外连接的效果(了解); + select e.empno,e.ename,e.job,e.sal,e.deptno,(select dname from dept d where e.deptno=d.deptno) dname from emp e; + (8)union: 使用union把两个结果合并成一个结果 + union(会合并相同的数据), union all(不会合并相同的数据); + 注意: 合并两个查询结果的时候,要求两个查询结果的结果必须要一致(查询字段的个数,字段的类型,字段的顺序必须要一致); + select * from emp where sal>1500 union select * from emp where deptno=20; +9.数据库事务处理: + 1)把多个相关的操作捆绑成一个整体,要么都成功,要么都失败; + 2)只有DML(数据操作语言)语句才会引起一个事务; + 3)事务的四个特性: + 原子性(Atomicity): +    原子性是指事务包含的所有操作要么全部成功全部应用,要么全部失败回滚。因此事务的操作如果成功就必须要完全应用到数据库,如果操作失败则不能对数据库有任何影响。 + 一致性(Consistency): + 一致性是指一个事务执行之前和执行之后都必须处于一致性状态。拿转账来说,假设用户A和用户B两者的钱加起来一共是5000,那么不管A和B之间如何转账,转几次账,事务结束后两个用户的钱相加起来应该还得是5000,这就是事务的一致性。 + 隔离性(Isolation): +   隔离性是指多个并发事务之间要相互隔离。当多个用户并发访问数据库时,比如操作同一张表时,数据库为每一个用户开启的事务,不能被其他事务的操作所干扰。 + 持久性(Durability): +   持久性是指一个事务一旦被提交了,那么对数据库中的数据的改变就是永久性的,即便是在数据库系统遇到故障的情况下也不会丢失提交事务的操作。 + 4)mysql管理事务的默认方式是: 自动提交; + start transaction: 关闭本次自动提交事务; + 关闭自动提交之后执行的第一个DML语句会引起一个事务;在事务开启之后,只要事务没有结束,执行的所有DML操作都是隶属于同一个事务的;事务中对数据的修改被保存在内存中;只有当前事务可以查询到这些修改; + insert into dept values(50,"",""); + insert into dept values(60,"",""); + update dept set dname="研发部",loc="沈阳" where deptno>40; + select * from dept; + 关闭事务 + commit: 提交结束事务的时候,事务中对数据库的修改被永久的保存到数据库中,所有的会话可以查询到这些修改; + rollback: 回滚结束事务的时候,事务中对数据库的修改被全部放弃,数据库恢复到事务开始之前的状态; + 5)事务的隔离级别: + 并发事务问题: + 脏读: 读到另一个事务的未提交更新数据,即读到了脏数据; + 不可重复读: 对同一记录的两次读取不一致,因为另一个事务对该记录进行了修改; + 幻读(虚读): 对同一张表的两次查询不一致,因为另一个事务插入了一条记录; + 四大隔离级别: + 串行化: serializable; + 不会出现任何并发问题,因为它对同一数据的访问是串行的(非并发访问),性能最差; + 可重复读: repeatable read --mysql默认的隔离级别 + 可防止脏读和不可重复读,性能比serializable好; + 读取已提交: read committed --oracle默认的隔离级别 + 防止脏读,性能比repeatable read好; + 读取未提交的数据: read uncommitted --会脏读 + 可能出现任何事务并发问题;性能最好; + mysql的隔离级别: + mysql的默认隔离级别为repeatable read,可以通过 select @@tx_isoliation 查看; + 也可以通过 set transaction isolationlevel [4选1] 来设置当前连接的隔离级别; + + + + \ No newline at end of file diff --git "a/JDBC/src/note/sql/Oracle\345\216\237\345\247\213\347\254\224\350\256\260.txt" "b/JDBC/src/note/sql/Oracle\345\216\237\345\247\213\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..d971d98 --- /dev/null +++ "b/JDBC/src/note/sql/Oracle\345\216\237\345\247\213\347\254\224\350\256\260.txt" @@ -0,0 +1,810 @@ +�����Ĺ��ɣ� +1��ǰ�� HTML��CSS��JS��JQuery +2����̨ java +3�����ݿ� Oracle Mysql(�׹���) + +1.��װOracle + + �����û��� + sys�������û� �������Ȩ�� ����sysdba��ɫ �����ݿ����Ա�� + �д������ݿ��Ȩ�� + system����������Ա Ȩ�޽����ڳ�������Ա + + scott:��ͨ�û� һ��������ϰ scott -- tiger + �����û���alter user scott account unlock + �������룺alter user scott identified by tiger + +2.��¼��ʽ + 1��sqlplus �س� + �����û��� + �������� + + 2��sqlplus/nolog + conn scott/tiger + +3. DB��DataBase �������ݽṹ���������ݵIJֿ� + ��ϵ�����ݿ� + �Զ�ά����ʽ�洢����(�����У��ֶ��� �����У�����) + + + DBMS�����ݿ����ϵͳ + mysql��oracle��SqlServer +4.�������� + sqlplus �����ݿ� + conn �û���/���� �������ݿ� + show user; ��ʾ��ǰ�û� + exit; �Ͽ����� + quit; �Ͽ����� +5.SQL:�ṹ����ѯ���� + SQL�ķ��ࣺ***** + DDL ���ݶ������� ���ڲ������ݶ��󣨱� ��ͼ ���� ������ + DML ���ݲ������� ������ɾ�ı��е�����(ͨ����������) + TCL ����������� �������ݵ��ύ�ͳ��� + DQL ���ݲ�ѯ���� ���ڲ�ѯ���ݼ�¼ + DCL ���ݿ������� ����ִ��Ȩ�޷��� + + DDL�����ݶ������� + ---���ڲ������ݿ���󣨱� ��ͼ ���� ������ + CREATE���������������������� + ALTER���޸ı�����������Ľṹ + DROP��ɾ��������������Ľṹ + TRUNCATE����ձ�(ɾ���������ݣ��������ṹ) + DML�����ݲ������� + ---������ɾ�ı��е�����(ͨ����������) + INSERT INTO�������ݲ��뵽���� + DELETE��ɾ���������� ---- WHERE + UPDATE ���� SET�����±������� --- WHERE + + TCL������������� + COMMIT���ύ--���� + ROLLBACK���ع�---���� + + DQL�����ݲ�ѯ���� + SELECT ��ѯ***** + + DCL�����ݿ������� + ����ִ��Ȩ�޷��� GRANT REVOKE +6.DDL ���ݶ������� + 1�������� + �鿴�û�����ı��� + SELECT table_name FROM user_tables; + CREATE TABLE ���� ( + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��] + ); + CREATE TABLE users( + id number(10) primary key, + name varchar2(20) not null unique, + salary number(10,2), + create_time date default sysdate + ); + id(����) �ص㣺���������ű���ÿһ���������ֵ������ͬ���ұ�����ֵ���ǿ���Ψһ��PRIMARY KEY + �������� + ������������ + 1����������ĸ��ͷ + 2��������1-30���ַ�֮�� + 3������ֻ�ܰ���A-Z,a-z,0-9,_,$,��# + 4�����벻�ܺ��û������������������ + 5�����벻����Oracle�ı����� + ��������: + 1��NUMBER:��ֵ���� + NUMBER(X):x����λ�� + NUMBER(3) ����ܴ��3λ�� + NUMBER(X,Y):x��ʾ����λ y��ʾ�����м�λ��С�� + NUMBER(6,2) 8888.88 + 2��CHAR:�����ַ��� + CHAR(X):X��ʾ��ռ�����ֽ� ����2000�ֽ� + 3��VARCHAR2:�䳤�ַ��� + VARCHAR2(X):X��ʾ��ռ�����ֽ�(�������б�������ݳ��ȣ�ռ�õĿռ��DZ仯��) ���4000�ֽ� + 4��DATE ʱ������ + SELECT SYSDATE FROM DUAL + α����dualȷʵ��һ�ű�����һ��ֻ��һ���ֶΣ�һ�м�¼�ı���ϰ�������dz�֮Ϊα������Ϊ�����洢 + ��������� + 2��ɾ������ + DROP TABLE users; + 3��ALTER���޸ı� + �޸ı����� + RENAME ԭ���� TO �±��� + �޸ı��ṹ�� + ����������ֶΣ� + ALTER TABLE ���� ADD(�ֶ��� ��������[Ĭ��ֵ Լ��]) + ��ֻ����������󣬲��ܲ��뵽���е����� + ɾ�����������ֶΣ� + ALTER TABLE ���� DROP(�ֶ���) + �޸ı��������ֶεĸ�ʽ�� + ALTER TABLE ���� MODIFY(�ֶ��� ��������[Ĭ��ֵ Լ��]) +7��DML ���ݲ������� + 1��������������� + ȫ�ֶβ������ݣ� + INSERT INTO ���� VALUES(1,'����',8000,sysdate,'����Ա'); + ע�⣺values�е�����˳��Ҫ�Ͷ����ֶ�ʱһ�� + �����ֶβ������� + INSERT INTO ����(��Ҫ�������ݵ��ֶ�) VALUES(���ֶ�˳�����Ӧ������) + 2���޸ı����������� + ���޸ı�������ʱ��ͨ������where�Ӿ����޶�Ҫ�޸ĵļ�¼��������ֻ���޸�����where�����ļ�¼�� + ������ȫ���޸� + UPDATE ���� SET �ֶ���=����,�ֶ���=���� WHERE �ֶ���=���� + 3��ɾ���������� + ͨ����where�Ӿ����ã�����Ļ���ɾ���������� + DELETE FROM ���� WHERE �ֶ���=���� + ��DDL����е�TRUNCATE��䣬ͬ����ɾ�������ݵ����� + ��delete������ + --delete����������ɾ����truncate��������ȫ��ɾ�� + --delete��DML�����Ի��ˣ�truncate��DDL��䣬������Ч���޷����� + --�����ɾ��ȫ������¼�����������ϴ�delete���Ч�ʱ�truncate���� + ɾ��ȫ����¼�� + delete from users; + ���� + truncate table users; +8.TCL ����������� + commit �ύ + ��ʾ����� ��ť + ��ʽ�������ر����ݿ⹤�� + rollback �ع�---���� + ������ĸ����ԣ�***** + 1.ԭ���ԣ�������������в���Ҫôȫ���ɹ���Ҫôȫ��ʧ�ܻع�;�ɹ�����Ҫ��ȫӦ�õ����ݿ⣬ʧ�����ܶ����ݿ� + ����Ӱ�죻 + 2.һ���ԣ�����ִ��ǰ��ִ�к���봦��һ����״̬�� + 3.�����ԣ�������û������������ݿ�ʱ�����ݿ�Ϊÿһ���û����������񣬲�����������IJ��������ţ������ + ������֮��Ҫ�໥���룻 + 4.�־��ԣ�һ������һ�����ύ�ˣ���ô�����ݿ��е����ݵĸı���������Ե� +9.DQL ���ݲ�ѯ���� + ������ѯ�����SELECT�Ӿ��FROM�Ӿ���� + SELECT��ָ��Ҫ��ʾ���ֶ����� + FROM��ָ��������Դ + 1����ѯ���� + SELECT * FROM ����; + 2����ѯ����������Ϣ + SELECT �ֶ�����1,�ֶ�����2 FROM ���� WHERE �޶�����; + ע�⣺SELECT ������ʾ�ֶΡ�����ʽ������ + <>�Dz����� + ���������ƽϳ��������ڲ�ѯ�����������ʾ������Ը���ѯ�������ʹ���б��� + �������������ֿ�ͷ���������ֿ�ͷ������Ҫ��""���ϣ�ͬ�������Է��ſ�ͷ + +10�����ú��� + 1���ַ������� + CONCAT(c1,c2)�����ַ�������c1��c2�����ֶ�ƴ����ʾ + ||�����ַ������ţ�����ַ������ӣ���||��ֱ�ۣ� + SELECT ename || ':'|| sal as "����:����" from EMP + + LENGTH(�ֶ���)��ʾ�ַ������� + SELECT length(ename) from emp + + UPPER(�ֶ���)ת��д + LOWER(�ֶ���)תСд + INITCAP(�ֶ���)����ĸ��д + + TRIM() ȥ���ַ���ǰ��ָ������ + select trim('E' from 'EEEEEHELLO WORLDE') from dual + + ȥ��Ա���������ֵ�����ĸA + select ename,trim('A' FROM ename) from emp + + LTRIM('�ַ���','')ȥ���ַ������ָ������ + RTRIM('�ַ���','')ȥ���ַ����Ҳ�ָ������ + + LPAD(ch1,m,ch2) + RPAD(ch1,m,ch2) + ��λ���� + ch1Ҫ��ѯ��ʾ������ mָ��λ�� + ch2����λ������ʱ��ʲô���в�λ + ��emp����ʹ����λ����sql��0����6λ + select ename,rpad(sal,6,'$') from emp + SUBSTR(ch,m,n)����ȡ�ַ���--��1��ʼ + ��ȡch����ַ�������m��ʼ��ȡ������ȡn�� + ��mΪ���������ǴӺ���ǰ�����±��ѯ�Ӷ����н�ȡ + INSTR(ch1,ch2,m,n)������ch1��ch2���ֵ�λ�ã�m�������ĸ�λ�ÿ�ʼ������n�����ڼ��γ��� + ��ָ��m��n��Ĭ��ֵ����1 + 2�����ֺ��� + ROUND(n,m)�������������� + �����е�n�������κ����֣�ָҪ������������ + m������������mȡ�������������뵽С������mλ + mȡ0���������뵽����λ + mȡ���������������뵽С����ǰmλ + mȱʡ��Ĭ��ֵ��0 + TRUNC(n,m)����ȡ���֣�û���������� + select trunc(45.678,2),trunc(45.678,0),trunc(45.678,-1) from dual + MOD(m,n)������m����n���������nΪ0��ֱ�ӷ���M + нˮֵ��1000ȡ���� + select ename,sal,mod(sal,1000) from emp + CEIL(n)������ȡ�� ���ڸ�������С���� + FLOOR(n)������ȡ�� С�ڸ������������ + 3�����ں��� + sysdate�����ص�ǰ��ϵͳʱ�䣬��ȷ���� select sysdate from dual + systimestamp�����ص�ǰϵͳ���ں�ʱ�䣬��ȷ�����루ʱ����� + 1��TO_DATE()���൱����SimpleDateFormat��parse()���� + ���ã����������ַ������ո��������ڸ�ʽת��ΪDate + select to_date('2018-12-01 11:30','YYYY-MM-DD HH24:MI:SS') from dual + 2��TO_CHAR()�����ڽ�DATE���ո��������ڸ�ʽת��Ϊ�ַ��� + select ename,to_char(hiredate,'YYYY"��"MM"��"DD"��"') from emp + ���ڳ��ú����� + 1��LAST_DAY(date)������ָ�����������µ����һ�� + select last_day(sysdate) from dual + 2��ADD_MONTHS(date,i)����������date����i���º������ֵ������i�Ǹ��������õ��Ǽ�ȥi���º�� + ����ֵ + ��ѯԱ����ְ20��������� + select ename,add_months(hiredate,20*12) "20����" from emp + 3��MONTHS_BETWEEN(date1,date2)��������������֮�������ٸ��� + ʵ���������date1-date2�����date2ʱ���date1ʱ��������õ���ֵ�������������ڼ���������£� + �����õ���С��λ�Ľ�� + ����Ա����ְ���ٸ��� + select ename,trunc(months_between(sysdate,hiredate)) from emp + date�������������ݿ��п��Խ��м���������������������� + select ename,sysdate-hiredate from emp + 4��LEAST(date1,date2)����ʾ����ʱ���е���Сֵ + GREATEST(date1,date2)����ʾ����ʱ���е����ֵ + �������ͱ���һ�� + select LEAST(sysdate,to_date('2018-12-1','YYYY-MM-DD')) from dual + 5��EXTRACT(date from datetime)���Ӳ���datetime����ȡ����dateָ�������ݣ�������ȡ�ꡢ�¡��� + select extract(day from sysdate) from dual + ��ѯ1980����ְ��Ա������Щ + select ename from emp where extract(year from hiredate) = 1980 + 4����ֵ���� + null�ĺ��壺 + ��ʱ���е�ijЩ�ֶ�ֵ������δ֪����ʱ�����ڣ�ȡֵnull + �κ��������;���ȡֵnull + null������ѯ�� + null�������κ�ֵ + �ж�һ���ֶε�ֵ�Ƿ�Ϊnull��Ҫʹ��is null��is not null + select * from emp where comm is null + �ǿ�Լ���� + �ǿգ�not null��Լ������ȷ���ֶ�ֵ��Ϊ�� + 1��NVL(arg1,arg2)����nullת��Ϊ��nullֵ + ���arg1Ϊnull������arg2�����򷵻�arg1��arg1��arg2�������κ��������ͣ��������������������ͱ� + ����һ�µ� + ����Ա�������� + select ename,sal,comm,sal+nvl(comm,0) from emp + 2��NVL2(arg,res1,res2)�����argΪnull���򷵻�res2;arg��Ϊnull���򷵻�res1 + select ename,sal,comm,nvl2(comm,sal+comm,sal) from emp +11��������ѯ��䣺 + 1��from�Ӿ䣺 + select * from ���� + select--����ָ��Ҫ��ѯ���� + from--ָ��Ҫ���ĸ����в�ѯ + 2��where�Ӿ䣺 + ��select����У�������where�Ӿ���ʹ�ñȽϲ��������Ʋ�ѯ��� + ��ѯ����10�µ�Ա����Ϣ + select * from emp where deptno=10 + ��ѯԱ������ְλ��'SALESMAN'��Ա����Ϣ + select * from emp where job='SALESMAN' --oracle���ַ������ִ�Сд +12����ѯ���� + 1��ʹ��>,<,>=,<=,!=,<>,= + ��ѯְԱ����нˮ����2000��ְԱ��Ϣ + select ename,sal from emp where sal<2000 + ��ѯְԱ���в����ڲ���10��Ա����Ϣ + select ename,sal,job from emp where deptno <> 10 + ��ѯְԱ������1981��1��1���Ժ���ְ��ְԱ��Ϣ + 2��ʹ��AND,OR�ؼ��� + AND ���� + OR ���� + ��ѯнˮ����1000����ְλ�ǡ�CLERK����ְԱ��Ϣ + select ename,sal,job from emp where sal>1000 and job='CLERK' + ��ѯнˮ����1000����ְλ�ǡ�CLERK����ְԱ��Ϣ + select ename,sal,job from emp where sal>1000 or job='CLERK' + ע�⣺AND���ȼ�����OR������ʹ��()���������ȼ�˳�� + select ename,sal,deptno from emp where deptno=20 or deptno=30 and sal>800 + 3��ģ����ѯ***** + LIKE����ģ��ƥ���ַ��� + select ename,job from emp where ename like 'A%' + _:����һ���ַ� + %:�������ַ� + 4��ʹ��IN��NOT IN + IN(list)��ȡ�������б���Χ�е����� + NOT IN(list)��ȡ�������ϴ��б��е����ݼ�¼ + ��ѯְλ��MANAGER����CLERK��Ա����Ϣ + select * from emp where job in('MANAGER','CLERK') + ��ѯ���Dz���10��20��Ա�� + 5��BETWEEN...AND... + ������ѯ����ij��ֵ��Χ���������ݣ������䣩 + �ȼ���>= AND <=��Ч�� + ��ѯнˮ��1500-3000֮���ְԱ��Ϣ + select * from emp where sal between 1500 and 3000 + 6��ʹ��ANY��ALL���� + ALL��ANY���ܵ���ʹ�ã���Ҫ��ϵ��бȽϲ�����>,>=,<,<=һ��ʹ�� + >ANY��������С + ALL��������� + ANY(select sal from emp where job='MANAGER') + 7��distinct�����ظ����� + ��ѯԱ���IJ��ű�� + select distinct deptno from emp + ��ѯÿ�����ŵ�ְλ + select distinct deptno,job from emp +13����������ȼ� + 1��() + 2�������������* / + - + 3�����ӷ���|| + 4���Ƚ��������= > >= < <= <> is null,like,between...and...,in + 5���߼��������not and or + BETWEEN...AND��AND������ + 1��BETWEEN...AND��ֻ�������Ƚ�һ���ֶ�ֵ + 2��AND�����������Ƚϲ�ͬ���ֶ� +14��ORDER BY ���� + �Խ���������򣬱��������select�����һ���Ӿ� + ���ԶԽ��������ָ���ֶν�������ASC����DESC + select ename,sal from emp order by sal asc + ע�⣺������ֶ���������NULL����NULL���������ֵ + select * from emp where deptno=10 order by mgr desc + ��������� + ���Զ�����Ϊ�����׼ʱ�����Ȱ��յ�һ�н������������һ��������ͬ�����Եڶ��������Դ����� + �������򲻹������ǵ���ÿ������Ҫ������������ʽ + ��Ա�����е�ְԱ�����Ȱ��ղ��ű���������У��ٰ���нˮ�������� + select ename,deptno,sal from emp order by deptno asc,sal desc +15���ۺϺ���(���к��������麯��) + 1��MAX��MIN������ȡ���л����ʽ�������Сֵ + ��������ͳ���κ��������ͣ��������֡��ַ������� + ��ȡ�����µ����нˮ�����нˮ������������ + select max(sal),min(sal) from emp + �������������ְʱ�䣬���������� + select max(hiredate),min(hiredate) from emp + 2��AVG��SUM������ͳ���л����ʽ��ƽ��ֵ���� + ֻ�ܲ����������ͣ�����NULLֵ + ��ȡ������ȫ��ְԱ��ƽ��нˮ��нˮ�ܺ� + select AVG(sal),SUM(sal) from emp + 3��COUNT������������еļ�¼����������NULLֵ + ��ȡְԱ����һ���ж�����ְԱ��¼ + select count(*) from emp + ���ְԱ�����ж��������н���� + select count(comm) from emp + COUNT��SUM������ + COUNT��ͳ�Ƽ�¼����SUM��ͳ�Ƽ�¼��ֵ���ܺͣ�ע���������� + 4���ۺϺ����Կ�ֵ�Ĵ��� + �ۺϺ�������NULLֵ + select avg(comm) from emp + �����ϣ������nullֵ��ʹ��NVL���� + select avg(nvl(comm,0)) from emp +16��GROUP BY ���� + �����������ָ���ֶν��з��� + ������򣺸��ֶ��µ�ֵһ���ļ�¼������һ�� + ��ϣ���õ�ÿ�����ŵ�ƽ��нˮ������������������ƽ��нˮ + select MAX(sal),MIN(sal),AVG(sal),SUM(sal) from emp group by deptno + select������������˾ۺϺ�����ͬʱ�������ijһ�ֶ�ʱ����ô���ֶα��������group by�Ӿ��� + where���治��ʹ�þۺϺ��� + select MAX(sal),deptno from emp where MAX(sal)>4000 group by deptno������ִ�У� + HAVING�Ӿ� + �����Է����Ľ����һ�����ƣ����簴���ŷ���󣬵õ�ÿ�����ŵ���߹��ʣ����Լ������������� + �������group by���棬���ܵ������� + WHERE�ǷǷ��麯���Ĺ����ж� + HAVING�Ƿ��飨�ۺϣ������Ĺ����ж� + ��ѯ����������ʳ���4000�IJ����Լ������ + select MAX(sal),deptno from emp group by deptno having MAX(sal)>4000 + �鿴���ű�ţ��Լ��ò��ŵ�ƽ�����ʣ�Ҫ���Ǹò��ŵ�ƽ������Ҫ����2000��ְλ��PRESIDENT�IJ�����ͳ�� + select deptno,AVG(sal) from emp where job <> 'PRESIDENT' group by deptno having AVG(sal)>2000 +17��SELECT���ִ�й���***** + 1��ͨ��from�Ӿ����ҵ���Ҫ��ѯ�ı� + 2��ͨ��where�Ӿ���зǷ��麯��ɸѡ�ж� + 3��ͨ��group by�Ӿ���ɷ������ + 4��ͨ��having�Ӿ�����麯��ɸѡ�ж� + 5��ͨ��select�Ӿ�ѡ��Ҫ��ʾ���л����ʽ���麯�� + 6��ͨ��order by�Ӿ����������� +18�������ܽ�***** + ���к����Ͷ��к����ķ��ಢ����˵�� + ���к����� + �ַ�������substr() trim() length()... + ��ֵ������round() trunc() mod()... + ���ں�����last_day() add_months()... + ת�������� + ��ʾת�� to_char() to_date() + ��ʽת�� �����������ַ�������ͨ���ᱻתΪ�ַ��� + ͨ�ú�����NVL() NVL2() + ע�⣺ͨ�����ڲ����������� ��������Ƕ�׶�㣬���ʱ������������ִ�� + ���к�����MAX()���ֵ MIN()��Сֵ SUM()��� AVG()ƽ��ֵ COUNT()ͳ�� +19��������ѯ + �����ĸ�� + ʵ��Ӧ��������Ҫ�����ݣ���������Ҫ��ѯ�������������ϵı������ֲ�ѯ�������Ӳ�ѯ�����Ӳ�ѯͨ���������� + �໥�� + ϵ�ĸ��ӱ�֮�� + �����������һ�ű��������г�Ϊ��� + ��������ı����ڹ�����ϵ�����ڶ��һ�� + �����֮��Ĺ�ϵ��һ�Զࣨͨ�����������ϵ������Զࣨͨ�������ű�������ϵ����һ��һ + N�ű��Ĺ�����ѯ��������ҪN-1����������������ָ����������������ֵѿ�������������Ľ������ + �ѿ������� + �ѿ�����ָ������������ÿ������ÿһ�к���������ÿһ������ϣ������������ļ�¼�����ֱ���X��Y���ѿ������� + ����X*Y����¼ + select count(*) from emp--14����¼ + select count(*) from dept--4����¼ + select * from emp,dept--56����¼ + ��ֵ���� �÷������=���� + �鿴Ա�����֡����ʡ��������� + select ename,sal,dname from emp e,dept d where e.deptno=d.deptno + select ename,sal,dname + from emp e,dept d + where e.deptno=d.deptno and d.loc='CHICAGO' + KING�����﹤���Լ����IJ��ű���Ƕ��� + select e.ename,e.deptno,d.loc + from emp e,dept d + where e.deptno=d.deptno and e.ename='KING' + 1.���Ը����������ͨ������ָ���ֶ������ĸ��������������ֶ�������ͬ�������ָ��������Դ + 2.ֻ���������������Ľ�����Żᱻ��ѯ���� + 3.���й�����������ͬʱ�������������͹������������ݲŻᱻ��ѯ���� + �����ӣ� + ���������������������ļ�¼�������Ϻ͵�ֵ����û������ֻ��д����ͬ + �﷨����1 INNER JOIN ��2 ON ����������INNER����ʡ�ԣ� + select ename,sal,dname from emp e INNER JOIN dept d ON e.deptno=d.deptno + ������ + �����ӷ��������������������ݼ�¼����Щ�������Ҫ������Щ���������������ļ�¼����Ҫʹ�������� + ������Ͳ��������������Ľ��ȫ����ѯ���� + ���ٴ����б��и������� + create table empc as select * from emp + ��empc���в���һ������ ���ź�Ϊ50 + insert INTO empc(empno,ename,deptno) values(1000,'JACK',50) + �������ӣ�(�����ȫ�����ұ��IJ���) + ��������ļ�¼���ڱ����ӵ��ұ����ҳ����������ļ�¼��֮ƥ�䣬����Ҳ��������ƥ��ģ���null��ʾ + �﷨����1 LEFT OUTER JOIN ��2 ON ����������OUTER����ʡ�ԣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ������������κβ��ŵ�Ա�� + select empno,ename,dname + from empc e + left join dept d + on e.deptno=d.deptno + �������ӣ�(�ұ���ȫ��������IJ���) + �����ұ��ļ�¼���ڱ����ӵ�������ҳ����������ļ�¼��֮ƥ�䣬����Ҳ���ƥ��ģ���null���� + �﷨����1 RIGHT OUTER JOIN ��2 ON ����������OUTER����ʡ�ԣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ�����û��Ա���IJ��� + select empno,ename,dname + from empc e + right join dept d + on e.deptno=d.deptno + ȫ������ + ���ط������������б��ļ�¼��û����֮ƥ��ģ���null��ʾ������������Ӻ������ӵIJ����� + �﷨����1 FULL OUTER JOIN ��2 ON ����������OUTER����ʡ�ԣ� + ������ + ���ӵ�����������ͬһ���������е�һ���ֶο��Զ�Ӧ��ǰ���������ֶ� + ��ѯÿ��Ա�����ϼ��쵼��˭ + select e.ename emp,m.ename mgr + from emp e,emp m + where e.mgr=m.empno + ��ѯSMITH����˾���ĸ����й��� + Oracle���ݿ��Դ��﷨��***** + 1���ѿ����� + 2�������ӣ�1.��ֵ���� + 2.�ǵ�ֵ���� + ���Ա����š����������ʡ����ʵȼ� + select e.empno,e.ename,e.sal,s.grade + from emp e,salgrade s + where e.sal between s.losal and s.hisal + 3�������ӣ�1.�������ӣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ������������κβ��ŵ�Ա�� + select empno,ename,dname + from emp e,dept d + where e.deptno=d.deptno(+) + 2.�������ӣ� + ��ѯԱ���ı�ţ��������Լ��������ƣ�����û��Ա���IJ��� + select empno,ename,dname + from empc e,dept d + where e.deptno(+)=d.deptno + 4�������� + ͨ�ñ�׼��SQL99�﷨ + 1.�������� + �������Ӻ͵ѿ�����������ģʽ��ͬ�����DZ�1�е�ÿһ�ж����2�е������н���һ������ + select * from emp cross join dept + 2.��Ȼ���� + ��Ȼ���ӵ�һ��������ܹ�ƥ�䵽��ֵ��������ô���ǵ�ֵ���ӵĽ�� + select * from emp natural join dept; --����deptno���е�ֵ���� + ��Ȼ���ӵڶ�����������ܹ�ƥ�䵽��ֵ����������ô��ѯ������ǽ������� + select * from emp natural join salgrade; --û�й�ϵ�������� + 3.�������� + 4.�������� + 5.ȫ������ + +20���Ӳ�ѯ + Ϊ�˸���ѯ�ṩ���ݶ�����ִ�еIJ�ѯ�������Ӳ�ѯ + �Ӳ�ѯ��Ƕ��������SQL����е�SELECT��䣬�󲿷�ʱ�������WHERE�Ӿ��У��Ӳ�ѯǶ�������������ѯ�򸸲�ѯ + �Ӳ�ѯ��Ҫע�⣺ + 1.�Ӳ�ѯ��Ҫд�������� + 2.�Ӳ�ѯ��Ҫд����������Ҷ� + 3.�Ӳ�ѯ����д��where,having,from�Ӿ���; + 4.�Ӳ�ѯ��ͨ����дorder by�Ӿ� + 1���Ӳ�ѯ��WHERE�Ӿ��� + ���ݷ��ؽ���IJ�ͬ���Ӳ�ѯ��Ϊ�����Ӳ�ѯ�������Ӳ�ѯ�������Ӳ�ѯ + 1.�����Ӳ�ѯ + ����һ��һ�����ݣ������Ӳ�ѯҪ��ʹ�õ��в����� > >= < <= <> = + select ename,sal from emp + where sal>(select MAX(sal) + from emp where deptno=20) + ��ѯ��SCOTTͬ���ŵ�Ա����Ϣ + select ename,job + from emp + where deptno=(select deptno from emp where ename='SCOTT') + ����нˮ����������ƽ��нˮ�ߵ�Ա�� + select deptno,ename,sal + from emp + where sal>(select avg(sal) from emp) + 2.�����Ӳ�ѯ + ���ض���һ�����ݣ�ʹ�ö��в����� in all any + select * from emp where deptno + in(select deptno from emp where job='SALESMAN') + and job <> 'SALESMAN' + >ANY�����Ӳ�ѯ���ؽ���е�ij��ֵ�󡣼�������Сֵ + ��ѯ��10�Ų���ij��Ա�����ʸߵ�Ա����Ϣ + select empno,ename,sal + from emp + where sal > any(select sal from emp where deptno=10) + =ANY�����Ӳ�ѯ���ؽ���е�ij��ֵ��� + ��ѯ��10�Ų���ij��Ա��������ȵ�Ա����Ϣ + select empno,ename,sal + from emp + where sal = any(select sal from emp where deptno=10) + ALL�����Ӳ�ѯ���ؽ���е�����ֵ���� + ��ѯ��30�Ų�������Ա�����ʸߵ�Ա����Ϣ + select empno,ename,sal + from emp + where sal > ALL(select sal from emp where deptno=30) + (select min(sal) from emp where deptno=30) + 3���Ӳ�ѯ��select���� + �����ӵ���һ��д�������Ƽ� + ��ѯÿһ��Ա����ͬʱ������Ա���IJ��ű�Ų�ѯ���ŵ����ƣ�����ѯ�������ţ���Ա���IJ�����Ϣ����null + select e.ename,e.sal, + (select d.dname from dept d where d.deptno=e.deptno) dname + from empc e +20����ҳ��ѯ + �ҵ�Ա�����й�����ߵ�ǰ�������������� + select * from emp order by sal desc + select rownum,e.* from emp e order by sal desc + --������Ľ������һ�ű��������ٲ�ѯ + select rownum,t1.* from (select e.* from emp e order by sal desc) t1 + --ֻҪ��ʾǰ������¼ + select rownum,t1.* from (select e.* from emp e order by sal desc) t1 where rownum<4 + ROWNUM:α�У�ϵͳ�Զ����ɵ�һ�У�������ʾ�к� + rownum��ORACLE�����е�������ʾ�кţ�Ĭ��ֵ����ʼֵ��1���ڲ�ѯ�����֮���ټ�1 + ��ѯrownum����2�����м�¼ + select rownum,e.* from emp e where rownum>2--û���κμ�¼ + ��ѯrownum���ڵ���1�����м�¼ + select rownum,e.* from emp e where rownum>=1 + ��ѯrownumС��6�����м�¼ + select rownum,e.* from emp e where rownum<6 + ��ѯ��6~��10����¼ + ��һ�֣� + select * from (select rownum rn,e.* from emp e) t + where t.rn between 6 and 10 + �ڶ��֣� + select t1.* from (select emp.*,rownum rn from emp where rownum <=10) t1 where rn >=6; + ��oracle��ֻ��ʹ���Ӳ�ѯ������ҳ��ѯ + ҳ�룺page + ÿҳ���������pageSize 5�� + ��һҳ��1~5 + �ڶ�ҳ��6~10 + ����ҳ��11~15 + start��(page-1)*pageSize+1 + end��pageSize*page +21��DECODE���� + DECODE(����1,ֵ1,���1,ֵ2,���2...[,Ĭ��ֵ]) + ����1������ֵ����ƥ�䣬���ĸ�ƥ��ɹ�����ʾ��Ӧ�Ľ��������δƥ�䣬����ʾ���һ��Ĭ��ֵ + ��ѯְԱ��������ְԱ��ְλ���㽱������ְλ�ֱ���'MANAGER','SALESMAN','ANALYST'ʱ�� + ������ֱ���нˮ��1.2����1.1����1.05�����������������ְλ���������ȡнˮֵ + select ename,job,sal, + decode(job, + 'MANAGER',sal*1.2 + 'SALESMAN',sal*1.1 + 'ANALYST',sal*1.05 + sal + ) "����" from emp + ��������� + select decode(job, + 'MANAGER','VIP', + 'ANALYST','VIP', + 'OPERATION' + ) from emp group by + decode(job, + 'MANAGER','VIP', + 'ANALYST','VIP', + 'OPERATION' + ) + ���� + select * from emp order by decode(job, + 'MANAGER',1 + 'SALESMAN',2 + 'ANALYST',3 + 4 + ) +22�����Ϻ��� + UNION��������������ѯ����������� + ���еIJ�ѯ������ܲ�������ͬһ�ű� + + ���ʴ���1500������20�Ų����µ�Ա�� + select * from emp where sal>1500 or deptno=20 + --���ʴ���1500������20�Ų����µ�Ա�� + select * from emp where sal>1500 + --20�Ų����µ�Ա�� + select * from emp where deptno=20 + �������㣺union��union all + union��ȥ���ظ��ģ��������� + union all������ȥ���ظ��� + select * from emp where sal>1500 + union + select * from emp where deptno=20 + INTERSECT������ͬʱ����������������е����� + ���ʴ���1500������20�Ų����µ�Ա�� + select * from emp where sal>1500 + INTERSECT + select * from emp where deptno=20 + MINUS������������� + 1981����ְԱ�����������ܲú;����� + --1981����ְԱ�� + select * from emp where to_char(hiredate,'YYYY')='1981' + --�ܲú;��� + select * from emp where where job='PRESIDENT' or job='MANAGER' + + select * from emp where to_char(hiredate,'YYYY')='1981' + MINUS + select * from emp where job='PRESIDENT' or job='MANAGER + ���������е�ע����� + 1.�е�����Ҫһ�� + 2.����˳��д + 3.�е�����Ҫһ�£�������㣬�ÿ�ֵ��� + --�е����Ͳ�ƥ�� + select ename,sal from emp where sal>1500 + union + select sal,ename from emp where deptno=20 + --�е�������ƥ�� + select ename,sal,deptno from emp where sal>1500 + union + select ename,sal from emp where deptno=20 +23��DDL�������� + 1���������ռ䣺 + �߼���λ��ͨ�������½�һ����Ŀ���ͻ��´������ռ䣬�ڱ��ռ��д����û��������� + �﷨�� + create tablespace ���ռ������ + datafile '�ļ���·��' + size ��С + autoextend on �Զ���չ + next ÿ����չ�Ĵ�С + �л���system�˺��´��� + ɾ�����ռ䣺 + drop tablespace YYP + 2�������û��� + create user �û��� + identified by ���� + default tablespace ���ռ������ + ɾ���û� + drop user yuanyipeng cascade; + �����û����� + alter user �û��� identified by ����; + ��Ȩ + grant connect to yuanyipeng + �����ɫ + grant dba to yuanyipeng + select * from scott.emp + ʹ���Ӳ�ѯ�ķ�ʽ�������� + create table ���� as ��ѯ��� + create table emp as select * from scott.emp + ע�⣺ֻ�Ḵ�Ʊ��ṹ�ͱ������ݣ����Ḵ���е�Լ�� + �����ѯ����н�������Ǹ��Ʊ��ṹ������ + �����ѯ���û�н�������Ǹ��Ʊ��ṹ + ֻ���Ʊ����ݣ�������������û��values�ؼ��֣� + insert into ���� ��ѯ��䣨���Ľṹ�����б��ṹһ�£� + 3���е�Լ����Լ����Ҫ������Լ���������ݵĹ��� + ����Լ����primary key ����Ϊ�գ�����Ψһ + �ǿ�Լ����not null + ΨһԼ����unique + ���Լ����check(����)��mysql���ǿ���д�ģ�����mysqlֱ�Ӻ����˼��Լ�� + ���Լ������Ҫ������Լ���ӱ�A�еļ�¼�������Ǵ���������B��(��֤����������) + create table books( + book_id number(10) primary key, + book_name varchar2(20) not null, + price number(10,2), + cid number(10) not null references category(id)--���Լ�� + ); + create table books( + book_id number(10) primary key, + book_name varchar2(20) not null, + price number(10,2), + cid number(10) not null, + foreign key(cid) references category(id)--���Լ�� + ); + + create table category( + id number(10) primary key, + cname varchar2(10) + ); + alter table books add foreign key(cid) references category(id) +24����ͼ + ���ݿ����֮һ����ͼ��sql��������ֵĽ�ɫ�ͱ���ȫ��ͬ��������ͼ������һ����ʵ���ڵı�������Ӧ����һ + ����ѯ���Ľ��������ͼ������������һ���� + ��ͼ�����ã��򻯸��Ӳ�ѯ���������ݷ��� + 1����������ͼ�������� + ����Ȩ�ޣ�ʹ��system�û���Ȩ + ��ͼ��Ȩ��䣺 + grant create view to �û���; + ����һ������ͼv_emp_10����ʾ����10�е�Ա���ı��롢������нˮ + create view v_emp_10 + as + select empno,ename,sal,deptno + from emp where deptno=10; + 2���޸���ͼ + �޸���ͼ�����滻����ͼ���Ӳ�ѯ��ʹ��create or replace view ��ͼ�����ɣ�����ͼ�����ھʹ��������ھ��滻 + ������ͼʱ�����и������ + create or replace view v_emp_10 + as + select empno id,ename name,sal salary,deptno + from emp where deptno=10; + 3����ѯ��ͼ + select * from v_emp_10 + ��ͼ�������ʹ�����ͼʱ������һ�£���һ����ԭ����; + select id,name,salary from v_emp_10 + 4����ͼ��DML���� + ����ͼ����DML�������Ƕ���ͼ������Դ�Ļ��������в��� + �������ݣ� + 1.ע����ͼ�п��������ֶζ�������Ĭ��ֵ�����Բ���Υ������������Ӧ�ֶε�Լ�������������ǿ������� + �ֶε�not null����������ʧ�� + 2.Ϊ��ͼ���Ӽ��ѡ����Ա������ͼ����DML����ʱ��Ⱦ���������ڴ��������with check option + 3.���û������ͼ��ִ��DML�����ı�Ҫ���ڽ�����ͼʱ����Ϊֻ�������������������֤��ͼ��Ӧ�Ļ����� + ���ݲ��ᱻ�Ƿ��޸�with read only + 5����ͼ�����Ӳ�ѯ�IJ�ͬ����Ϊ����ͼ�͸�����ͼ�����Ӳ�ѯ�а������к���������ʽ����麯��ʱ������ͼ�� + һ��������ͼ��������ͼ���ܽ���DML����������Ϊ�Ӳ�ѯ�еı���ʽ����������� + ����һ����ͼv_emp_salary����ְԱ�������ݰ����ŷ��飬���ÿ�����ŵ�ƽ��нˮ��нˮ�ܺ͡����нˮ + create view v_emp_salary + as + select d.dname,avg(sal) avg_sal,sum(sal) sum_sal,min(sal) min_sal from emp e,dept d + where e.deptno=d.deptno group by d.dname + 6��ɾ����ͼ + DROP VIEW ��ͼ���� +25�����У�sequence ��������Ψһ����ֵ�����ݿ����ͨ����Ϊ��������ֵ + �����Ƕ��������ݿ�������в��������ڱ� + 1���������� + create sequence ������ + start with xx ��ʼ���� + increment by xx ������ÿ�����Ӽ��� + maxvalue xx ���ֵ + minvalue xx ��Сֵ + + create sequence users_seq + start with 100 + increment by 10 + + ������������α�� + ---nextval����ȡ���е��¸�ֵ + ---currval: ��ȡ���еĵ�ǰֵ + �����д����Ժ󣬱�����ִ��һ��nextval��֮�����ʹ��currval + ��ȡ���еĵ�һ��ֵ������ʹ������ֵΪusers�������µļ�¼ + select users_seq.nextval from dual; + insert into users(id,name) values(users_seq.nextval,'����÷') + 2��ɾ�����У�drop sequence users_seq +26��������һ����߲�ѯЧ�ʵĻ��� + ������Ϊ����������ȥ�أ�����������������ֶο���Ϊ���������� + 1������������ + create [unique] index �������� + on ����(����) + ���ӵ������� + ��users����name���Ͻ������� + create index idx_users_name on users(name); + ���Ӷ������� + create index idx_users_name_salary on users(name,salary); + ��ѯ�������Զ�Ӧ������������Ҫ�����ƶ�����߲�ѯ�ٶ� + select * from users order by name,salary + 2���޸Ļ�ɾ������ + �����������������ִ��DML��������Ҫ�����ؽ���������������Ŀռ������� + alter index �������� rebuild + ɾ��������drop index �������� \ No newline at end of file diff --git a/JDBC/src/note/sql/SQL.sql b/JDBC/src/note/sql/SQL.sql new file mode 100644 index 0000000..f831846 --- /dev/null +++ b/JDBC/src/note/sql/SQL.sql @@ -0,0 +1,460 @@ +--SQL.day.02 +--������ +CREATE TABLE users( + id number(10) primary key,--���� + name varchar2(20) unique,--Ψһ��ʶ + salary number(10,2), + create_time date default sysdate +); + +--�鿴���ṹ(��command window���ã�SQL window����ʹ�ã� +desc ���� + +--�޸ı��� +RENAME users to userplus + +--����������ֶ� +ALTER TABLE userplus ADD(school varchar2(20)) +ALTER TABLE userplus ADD(job varchar2(10)) + +--ɾ�����������ֶ� +ALTER TABLE userplus DROP(school) + +--�޸ı��������ֶεĸ�ʽ +ALTER TABLE userplus MODIFY(salary number(10)) +ALTER TABLE userplus MODIFY(username varchar2(20) unique) + +--�޸ı����ֶ����� +ALTER TABLE userplus RENAME column name to username + +--������������� + --ȫ�ֶβ������� +INSERT INTO userplus VALUES(1, '����', 8000,sysdate, '����Ա'); +INSERT INTO userplus VALUES(2, '����', 8000,default, '����Ա');--��Ϊ������default sysdate�������������ֱ��дdefault +INSERT INTO userplus VALUES(3, '����', 8000,to_date('2018-12-01','YYYY-MM-DD'), '����Ա'); + --�����ֶβ��� +INSERT INTO userplus(id, username, job) VALUES(4,'����','��Ʒ����'); +INSERT INTO userplus(id, username, job, salary) VALUES(5,'����','��Ʒ����'��5000) + +--���±����������� +UPDATE userplus SET salary=salary*10, job='�ܾ���' WHERE id=1; + +--ɾ���������� +DELETE FROM userplus WHERE salary>5000; + +--�ύ���� +commit; + +--�ع� +rollback; + +--��ѯ������������ +SELECT * FROM userplus; + +--��ѯ����������Ϣ +SELECT job, salary FROM userplus; +SELECT * FROM userplus WHERE job <> '�ܹ�ʦ'; +SELECT salary*12 "��н" FROM userplus WHERE username='����';--���� +SELECT salary*12 AS "��н" FROM userplus WHERE username='����';--���� + +--10.(1)�ַ������� + --�����ַ��� +SELECT CONCAT(ename, sal) from EMP; +SELECT CONCAT(CONCAT(ename, ': ') ,sal)from EMP; +SELECT ename||': '||sal as "����������" from EMP; + --��ʾ�ַ������� +SELECT LENGTH(ename) from EMP; + --ת��д +SELECT UPPER(ename) from EMP; + --ȥ���ַ���ǰ��ָ������ +SELECT TRIM('E' from 'EEHELLO WORLDEEE') from dual; +SELECT ename, trim('A' FROM ename) from emp; --ȥ��Ա������ǰ�������'A' +SELECT LTRIM('EEHELLO WORLDEEE', 'E') from dual;--ȥ��EEHELLO��ǰ���E +SELECT ename, ltrim(ename,'A') from emp;--ȥ��Ա������ǰ�������'A' + --��λ���� +select * from emp +select ename, LPAD(sal, 6, 0) AS salary from emp + --substr��ȡ�ַ��� +select substr('helloworldqqqqq',6,10) from dual; + --INSTR�ַ������� +select instr('thinking in java','in',4,2) from dual; + +--10.(2)���ֺ��� + --��ȡ�������������� +select round(321.123,1) from dual; + --��ȡ������������ +select trunc(45.678,2), trunc(45.678,0), trunc(45.678), trunc(45.678,-1) from dual; + --ȡ���� +select ename, sal, mod(sal,1000) from emp; + --����ȡ�� +select ceil(45.678) from dual; + --����ȡ�� +select floor(45.678) from dual; +--10.(3)���ں��� + --ϵͳʱ�䣬��ȷ���� +select sysdate from dual; + --ʱ�������ȷ������ +select systimestamp from dual; + --TO_DATE(),����ת������ +select to_date('2018-12-01 11:30','YYYY-MM-DD HH:MI:SS') from dual; +select to_date('2018-12-01 13:30','YYYY-MM-DD HH24:MI:SS') from dual; + --TO_CHAR(),��DATE���ո��������ڸ�ʽת��Ϊ�ַ��� +select ename, to_char(hiredate,'YYYY-MM-DD HH:MI:SS') from emp; +select ename, to_char(hiredate,'YYYY"��"MM"��"DD"��"') from emp; + --LAST_DAY(date),����ָ�����������µ����һ�� +select last_day(sysdate) from dual; +select last_day(to_date('2018-12-01 11:30','YYYY-MM-DD HH:MI:SS')) from dual; + --ADD_MONTHS(date,i),����+-i����֮������� +select ename, hiredate, add_months(hiredate, 12*20) AS "20����" from emp; + --MONTHS_BETWEEN(date1,date2):������������֮�������ٸ��� +select ename,trunc(months_between(sysdate, hiredate)) from emp; + --date�������������ݿ��п��Խ��м������������������������ +select ename, sysdate-hiredate from emp; + --LEAST(date1,date2):��ʾ����ʱ���е���Сֵ���������ͱ���һ�¡� +select least(sysdate,to_date('2018/12/09','YYYY-MM-DD')) from dual; + --GREATEST(date1,date2):��ʾ����ʱ���е����ֵ���������ͱ���һ�¡� +select greatest(sysdate,to_date('2018/12/09','YYYY-MM-DD')) from dual; + --EXTRACT(date from datetime):�Ӳ���datetime����ȡ����dateָ�������ݣ�������ȡ���ꡢ�¡��ա� +select extract(year from sysdate),extract(month from sysdate),extract(day from sysdate) from dual; +select extract(hour from systimestamp),extract(minute from systimestamp),extract(second from systimestamp) from dual; +select ename from emp where extract(year from hiredate) = 1980; + +--10.(4)��ֵ������ +select * from emp where comm is not null and comm != 0; + --NVL(arg1,arg2):��nullת��Ϊ��nullֵ�����arg1Ϊnull������arg2�����򷵻ر����� +select ename,nvl(comm,0) from emp; +select * from emp where nvl(comm,0)>0; + --NVL(arg1,arg2),���arg1Ϊnull������arg2�����򷵻ر���������Ա�������루�¹��ʼӽ��𣨽������Ϊnull���� +select ename, sal, comm, sal+nvl(comm,0) as "������" from emp; + --NVL2(arg,res1,res2):���argΪnull���򷵻�res2,arg��Ϊnull�򷵻�res1. +select ename, sal, comm, nvl2(comm,comm+sal,sal) as"������" from emp; + +--11.������ѯ���: + --��ѯ����10�µ�Ա����Ϣ�� +select * from emp where deptno = 10; + --��ѯԱ������ְλ��'SALESMAN'��Ա����Ϣ +select * from emp where job = 'SALESMAN'; + +--12.��ѯ����: + --��ѯְԱ����нˮ����2000��ְԱ��Ϣ +select ename, sal from emp where sal < 2000; + --��ѯְԱ���в����ڲ���10��Ա����Ϣ +select * from emp where deptno <>10; + --��ѯְԱ������1981��1��1���Ժ���ְ��ְԱ��Ϣ +select * from emp where hiredate > to_date('1981/01/01','YYYY/MM/DD'); + --��ѯнˮ����1000������ְλ��'CLERK'��ְԱ��Ϣ +select * from emp where sal>1000 and job='CLERK'; + --��ѯнˮ����1000������ְλ��'CLERK'��ְԱ��Ϣ +select * from emp where sal>1000 or job='CLERK'; + --NVL(arg1,arg2)��NVL2(arg,res1,res2) + --��ѯнˮ����1000�����ҽ���Ϊnull��ְԱ��Ϣ +select * from emp where sal>1000 and comm>0; +select * from emp where sal>1000 and nvl(comm,0)>0; +select * from emp where (deptno=20 or deptno=30) and sal>800; + --ģ����ѯ��LIKE + --��ѯԱ������������'A'��ͷ��Ա����Ϣ +select * from emp where ename like 'ALLEN'; + --��ѯԱ�����������ڶ�����ĸ��'A'��Ա����Ϣ +select * from emp where ename like '_A%'; + --IN��NOT IN�� + --��ѯְλ�Ǿ�������ְԱ��Ա����Ϣ +select * from emp where job in ('MANAGER','CLERK'); + --��ѯ���Ų���10��20��Ա�� +select * from emp where deptno not in (10,20); +select * from emp where deptno <> all(10,20); + --BETWEEN...AND...:��������ѯ����ij��ֵ��Χ���������ݡ� + --��ѯнˮ��1500~3000֮���ְԱ��Ϣ +select * from emp where sal between 1500 and 3000;--[1500,3000] + --��ѯ�Ⱦ�����͹��ʸߵ�Ա����Ϣ�� +select * from emp where sal > any(select sal from emp where job='MANAGER'); + --��ѯԱ���IJ��ű�� +select distinct(deptno) from emp; +select distinct deptno,job from emp; + +--14.ORDER BY ����(д�������Ӿ����) +select ename, sal from emp order by sal; +select ename, sal from emp order by sal asc; +select * from emp where sal>2000 order by mgr desc; + --�������򣬶�Ա�����е�ְԱ�����Ȱ��ղ��ű���������У��ٰ���нˮ�������С� +select ename, deptno, sal from emp order by deptno asc, sal desc; + +--15.�ۺϺ��� + --MAX��MIN +select deptno,max(sal),min(sal) from emp group by deptno; +select max(hiredate),min(hiredate) from emp; +select ename from emp a where hiredate = (select max(hiredate) from emp); + --ERROR: +select ename from emp a where hiredate = (select max(hiredate),min(hiredate) from emp); + --RIGHT: +select ename from emp where hiredate in ((select min(hiredate) from emp), (select max(hiredate) from emp)); + --AVG��SUM +select avg(sal),sum(sal) from emp; +select avg(comm),sum(comm) from emp;--����nullֵ + --COUNT +select count(*) from emp; +select count(comm) from emp;--����nullֵ +select count(*) from emp where comm is not null; + --��ϣ���ۺϺ�������NULLֵʱ�� +select avg(nvl(comm,0)) from emp; + +--16.GROUP BY +select max(sal),min(sal),avg(sal) from emp group by deptno; +select deptno,max(sal),min(sal),avg(sal) from emp group by deptno; +select deptno,ename,max(sal),min(sal),avg(sal) from emp group by deptno,ename; + --where���治��ʹ�þۺϺ���,����ִ��. +select max(sal),deptno from emp where max(sal)>4000 group by deptno; + --��ѯ����������ʳ���4000�IJ����Լ�����ʡ� +select max(sal),deptno from emp group by deptno having max(sal)>4000; + --�鿴���ű�ţ��Լ��ò��ŵ�ƽ�����ʣ�Ҫ���Ǹñ��ŵ�ƽ�����ʳ���2000��ְλ��PRESIDENT�IJ��ܲ���ͳ��) +select deptno, avg(sal) from emp where job<>'PRESIDENT' group by deptno having avg(sal)>2000; + +--19.������ѯ + --��ѯÿ��Ա�����ڲ������� +select ename, dname from emp, dept where emp.deptno=dept.deptno; + --�鿴Ա�����֡����ʡ��������� +select ename, sal, dname from emp e, dept d where e.deptno=d.deptno and loc='CHICAGO'; + --KING�����﹤���Լ����IJ��ű���Ƕ��٣� +select ename, e.deptno, loc from emp e, dept d where e.deptno=d.deptno and ename='KING'; + +--20.�������� + --������ +select ename, dname from emp inner join dept on emp.deptno=dept.deptno; + --������ + --���ٴ�һ�����и������� +create table empc as select * from emp; + --��empc���в���һ�����ݣ����ź�Ϊ50 +insert into empc(empno, ename, deptno)values(1000,'JACK',50); +select * from empc; + --�������ӣ���ѯԱ���ı�š��������Լ��������ƣ������������κβ��ŵ�Ա���� +select empno, ename, dname, empc.deptno from empc left outer join dept on empc.deptno=dept.deptno; + --��������,��ѯԱ���ı�š��������Լ��������ƣ������κ�û��Ա���IJ��š� +select empno, ename, dname, c.deptno from empc c right outer join dept d on c.deptno=d.deptno; + --ȫ�����ӣ���ѯԱ���ı�š��������Լ��������ƣ������������κβ��ŵ�Ա����Ҳ�����κ�û��Ա���IJ��š� +select empno, ename, dname, c.deptno from empc c full outer join dept d on c.deptno=d.deptno; + --�����ӣ���ѯÿ��Ա�����ϼ��쵼 +select e.ename, m.ename from emp e, emp m where e.mgr=m.empno; +select e.ename emp, m.ename mgr from emp e left outer join emp m on e.mgr=m.empno; + --��ѯSMITH����˾���ĸ����й��� + --��ֵ���� +select e.ename emp, m.ename mgr, d.loc mgrloc +from emp e, emp m, dept d +where e.mgr=m.empno and m.deptno=d.deptno and e.ename='SMITH'; + --������ +select e.ename emp, m.ename mgr, d.loc mgrloc +from emp e inner join emp m +on e.mgr=m.empno and e.ename='SMITH' +inner join dept d +on m.deptno=d.deptno; + +--(�����ӵ���һ��д��):��ѯÿһ��Ա����ͬʱ������Ա���IJ��ű�Ų�ѯ���ŵ���Ϣ������ѯ�������ţ���Ա���IJ�����Ϣ����null +select e.ename, e.sal, e.deptno from empc e;--ERROR + --Solution 1. +select e.ename, e.sal, +(select d.deptno from dept d where d.deptno=e.deptno) deptno +from empc e;--RIGHT + --Solution 2.������������д����Ϊ����Ҫ��Ҫд��d.deptno +select e.ename, e.sal,d.deptno +from empc e left outer join dept d +on e.deptno=d.deptno; + --Solution 3.�����ű�ʱ(���Ӳ������ű�)�����ַ����Ƚϼ�һЩ +select e.ename, e.sal, +(select d.dname from dept d where d.deptno=e.deptno) dname, +(select d.loc from dept d where d.deptno=e.deptno) loc +from empc e; + +--21.�Ӳ�ѯ + --WHERE: + --�����Ӳ�ѯ����ѯ���ڲ��ź�20����߹��ʵ�Ա����Ϣ +select ename, sal from emp where sal>(select max(sal) from emp where deptno=20); + --�����Ӳ�ѯ����ѯ��SCOTTͬ���ŵ�Ա����Ϣ +select ename, job from emp where deptno=(select deptno from emp where ename='SCOTT') + --�����Ӳ�ѯ������нˮ����������ƽ��нˮ�ߵ�Ա�� +select ename, job, sal from emp where sal>(select avg(sal) from emp); + --�����Ӳ�ѯ����ѯ������SALESMAN��ְλ����SALESMAN��Ա������Ϣ�� +select * from emp where deptno in (select deptno from emp where job='SALESMAN') and job<>'SALESMAN'; + --�����Ӳ�ѯ����ѯ��10�Ų���ij��Ա��������ȵ�Ա����Ϣ +select * from emp where sal = any (select sal from emp where deptno = 10); + --�����Ӳ�ѯ��?????????????????????????????????????????????????????????????????????????? + --EXISTS�ؼ��֣��г�����Щ��Ա���IJ�����Ϣ +select deptno, dname from dept d where EXISTS (select * from emp e where e.deptno=d.deptno); + --HAVING: + --��ѯ��͹��ʸ��ڲ���30����͹��ʵIJ�����Ϣ +select min(sal) from emp group by deptno having min(sal)<(select min(sal) from emp where deptno=30); + +--22.��ҳ��ѯ + --��ѯrownum����2�����м�¼ +select rownum, e.* from emp e where rownum>2;--ERROR����Ϊû���κμ�¼�� +select rownum, e.* from emp e where rownum>=1; +select rownum, e.* from emp e where rownum<6; + --�ҵ�Ա�����й�����ߵ�ǰ�������������򣩣� + --ERROR +select rownum, e.* from emp e order by sal desc where rownum<=3; + --RIGHT +select * from emp order by sal desc;--�Ƚ����ٰ�������һ�ű�����ȥѡrownumС�ڵ���3���С� +select rownum, t1.* from (select * from emp order by sal desc) t1 where rownum <=3; + --��ѯ��6~10����¼(���ַ����� +select * from (select rownum rn, e.* from emp e) t where t.rn between 6 and 10; +select t1.* from (select rownum rn, emp.* from emp where rownum<=10) t1 where rn between 6 and 10; + --���ʽ����ѡ6~10����¼��ԭ���Ƚ����ټ�α�У��ٵ��±�ѡ��Χ�� +select * from (select rownum rn, e.* from (select * from emp order by sal desc) e) t where t.rn between 6 and 10; + +--23.DECODE(����1��ֵ1�����1��ֵ2�����2...[,Ĭ��ֵ]); + --��ѯְԱ��������ְԱ��ְλ���㽱������ְλ�ֱ���'MANAGER','SALESMAN','ANALYST'ʱ��������ֱ���нˮ��1.2,1.1,1.05��������Ϊԭ����нˮֵ�� +select ename, job, sal, decode(job,'MANAGER',sal*1.2,'SALESMAN',sal*1.1,'ANALYST',sal*1.05,sal) "����" from emp; +select decode(job,'MANAGER','VIP','ANALYST','VIP','OPERATION') from emp; + --������������������ +select decode(job,'MANAGER','VIP','ANALYST','VIP','OPERATION') job, count(*) from emp group by decode(job,'MANAGER','VIP','ANALYST','VIP','OPERATION'); + --�Զ������� +select * from emp order by decode(job,'MANAGER','1','SALESMAN','2','ANALYST','3'); + +--24 + --union + --���ʴ���1500��20�Ų����µ�Ա�� +select * from emp where sal>1500 or deptno=20; + --���ʴ���1500�� +select * from emp where sal>1500;--7����¼ + --20�Ų����µ�Ա�� +select * from emp where deptno=20;--5����¼ + --�������㣺union(�����ظ����ݣ�9����¼��������), union all(�������ظ����ݣ�12����¼��������) +select * from emp where sal>1500 union select * from emp where deptno=20;--����֮���ܹ�9����¼ + --INTERSECT������ +select * from emp where sal>1500 INTERSECT select * from emp where deptno=20; + --MINUS��������������� + --1981����ְԱ��(�������ܲú;���) +select * from emp where to_char(hiredate,'YYYY')='1981'; +select * from emp where job in ('PRESIDENT','MANAGER'); +select * from emp where to_char(hiredate,'YYYY')='1981' MINUS select * from emp where job in ('PRESIDENT','MANAGER'); + --ע����� +select empno,ename from emp where to_char(hiredate,'YYYY')='1981' MINUS select null,ename from emp where job in ('PRESIDENT','MANAGER'); + +--25.DDL���������� +CREATE tablespace YYP datafile 'e:/YYP.dbf' size 100m autoextend on next 10m;--���� +drop tablespace YYP;--ɾ����ȡ�����ļ��Ĺ���������ɾ���ļ�����ȡ������֮�󣬿����ֶ�ɾ������ +create user zicheng identified by qqq default tablespace YYP;--�����û� +drop user zicheng cascade;--ɾ���û� +alter user zicheng identified by eee;--�޸����� +grant connect to zicheng;--��Ȩ +grant dba to zicheng;--�����ɫ + --ʹ���Ӳ�ѯ�ķ�ʽ�������� +create table emp as select * from scott.emp;--ֻ���Ʊ��ṹ�ͱ����ݣ����Ḵ���е�Լ���� +create table dept as select * from scott.dept where 1=2;--ֻ���Ʊ��ṹ��������,��Ϊ1=2Ϊfalse�� +insert into dept select * from scott.dept;--ֻ���Ʊ�����,��ǰ���DZ��Ľṹ�����б��ṹһ�¡� +select * from dept; + +--26.�е�Լ�� + --���Լ�� +CREATE TABLE users( + id number(10) primary key,--���� + name varchar2(20) unique,--Ψһ��ʶ + sex varchar2(2) check(sex in ('��' ,'Ů')),--���Լ�� + salary number(10,2) check(salary>3000), + create_time date default sysdate +); +insert into users values(1,'admin','��',3500,default); + --���Լ�� +create table category( + id number(10) primary key, + cname varchar2(10) +); +create table books( + book_id number(10) primary key, + book_name varchar2(20) not null, + price number(10,2) check(price>100),--���Լ�� + cid number(10) not null references category(id)--���Լ�� + --foreign key(cid) references category(id)--��һ�����Լ���ķ�ʽ +); + --���Բ��ڶ�����н������Լ���������ô����Ҳ��ʵ�����Լ���� +alter table books add foreign key(cid) references category(id); + --���ӣ� +insert into category values(1,'С˵'); +insert into category values(2,'��ѧ'); +insert into category values(3,'����'); +select * from category; +insert into books values(1,'���μ�',101,1); +select * from books; + --����ɾ���������Ϊ�ӱ�������ص��ѽ������������Լ�� +delete from category where id=1; +delete from books where cid=1;--ɾ��������������ַ��� +update books set cid=null where cid=1;--Ȼ����ɾ�������е����ݾ�ok�� + +--27.��ͼ + --����һ������ͼv_emp_10����ʾ����10�е�Ա���ı��롢������нˮ�� +create view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10; + --�޸���ͼ�����滻����ͼ���Ӳ�ѯ��ʹ��create or replace view ��ͼ�� ���ɣ�����ͼ�����ھʹ��������ھ��滻��������ͼʱ�����Ը��и�������� +create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10; + --��ѯ��ͼ +select * from v_emp_10; + --��ͼ��DML����,���޸�ԭ�������� +insert into v_emp_10 values(1,'Tony',8888,50) +select * from emp; + --with check option +create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10 with check option + --with read onlyֻ�������ܶ���ͼ����DML������ +create or replace view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10 with read only; + --����һ����ͼv_emp_salary,��ְԱ���е����ݰ����ŷ��飬���ÿ�����ŵ�ƽ��нˮ��нˮ�ܺ͡����нˮ�� +create view v_emp_salary as select d.dname, avg(sal) avg_sal,sum(sal) sum_sal,min(sal) min_sal,max(sal) max_sal from emp e, dept d where e.deptno=d.deptno group by d.deptno, d.dname; +select * from v_emp_salary; + --drop view ��ͼ���ƣ�ɾ����ͼ +drop view v_emp_slary; + +--28.���� + --�������������С����������ɾ������ +CREATE TABLE users( + id number(10) primary key,--���� + name varchar2(20), --unique,--Ψһ��ʶ + salary number(10,2), + create_time date default sysdate +); + --�������У�create sequence ������ start with **(��ʼ����) increment by **(����,ÿ�����Ӽ��������ɸ�) maxvalue **(���ֵ) / minvalue **(��Сֵ); +create sequence users_seq start with 100 increment by 10;--��������,��һ��һ��Ҫ��nextval��������currval +insert into users(id,name)values(users_seq.nextval,'����÷');--������� +select users_seq.currval from dual;--�鿴��ǰ����ֵ�� +drop sequence users_seq;--ɾ������ + --ѭ������ +begin + for i in 1..10000 + loop + insert into users(id,name,salary) values( users_seq.nextval,'˲�䱬ը',8888); + end loop; + commit; +end; + +--29.���� + --����������create [unique] index �������� on ����(����) +create index idx_users_name on users(name);--��users����ename���Ͻ������� + --���Ӷ��������� +create index idx_users_salary on users(name,salary); + --��ѯ�������Զ�Ӧ������������Ҫ����ȥ�ƶ�����߲�ѯ�ٶȡ� +select users.* from users order by name, salary; + --�ع�������������ƽ����������޸ı�������ع�һ��������ƽ��������� +alter index �������� rebuild;--�����޸����ݵı��������齨�������� + --ɾ�������� +drop index idx_users_salary; + + + + + + + + + +--��ϰ +create table student( + id number(10) not null, + name varchar2(20), + hobby varchar2(100), + school varchar2(20), + create_time date +); +--�޸ı���student_t +rename student to student_t; +--�����ֶΣ���Ŀproject���ɼ�score +alter table student_t add(project varchar2(20), score number(3)); +--ɾ��ѧУ�ֶ� +alter table student_t drop(school); +--�޸��ֶθ�ʽ +alter table student_t modify(create_time varchar2(20) default sysdate); +--�޸ı����ֶ����ƣ�name��Ϊstu_name +alter table student_t rename column name to stu_name; + diff --git a/JDBC/src/note/sql/SQLNote.sql b/JDBC/src/note/sql/SQLNote.sql new file mode 100644 index 0000000..a18543f --- /dev/null +++ b/JDBC/src/note/sql/SQLNote.sql @@ -0,0 +1,422 @@ +���ݿ�//297��308,415 +һ�� ʹ��sqlplus�������ݿ� + 1. ��ʽ�� + 1�� sqlplus���س�������û��������� + 2�� sqlplus/nolog ------conn scott/tiger + 3�� ʹ�ÿ��ӻ����棬developer��ע�⣺ʹ�ô������ӿ��ӻ�����ȴ�������ӣ�������޷�����޼���������Ҫ�������ü�������a.�رշ��� b.�������ü����ļ��޸�HOSTΪ��ǰIP�����޸�Ϊlocalhost����127.0.0.1������������ c.�ٿ��������������ӵ�½���ɡ��� + 2. �����ע����� + 1�� ����sql����ԡ�;����β���س�ִ�С� + 2�� sql��䲻���ִ�Сд���ַ������⡣ + 3. Oracle�û���oracle��װ����Զ�����4���˻����� + 1�� sys�û��������û����������Ȩ�ޣ�����sysdba�����ݿ����Ա����ɫ���������ֵ����ͼȨ�ޣ��ܴ������ݿ⣬Ĭ������manager�� + 2�� system�û�������Ա�������ڳ����û�������sysoper(ϵͳ����Ա)��ɫ�����ܴ����⡣ + 4. DB��Database���ݿ⣬��ϵ�����ݿ⣬�������ݽṹ���������ݵIJֿ⣬�Զ�ά��(�У��ֶΣ� �У�����)����ʽ�洢���ݡ� + DBMS�����ݿ����ϵͳ��Oracle, mysql, sqlserver���� +���� sql��� + 1. SQL ��Structed Query Language,�ṹ����ѯ���ԣ� + 2. SQL���ࣺ + DDL�����ݶ������ԣ����ڲ������ݶ��󣨱�����ͼ�����У��������� + CREATE������������������ + ALTER���޸ı�����������ṹ�� + DROP��ɾ�������������� + TRUNCATE��ɾ�������ݣ��������ṹ����ձ��IJ����� + DML�����ݲ������ԣ�ͨ����Ҫ����TCL����������ɾ�ı��е����ݡ� + INSERT INTO�������ݲ�����С� + DELETE��ɾ���������ݡ� + UPDATE SET�����±������ݡ� + TCL������������ԡ� + COMMIT���ύ�� + ROLLBACK���ع��� + DQL�����ݲ�ѯ���� + SELECT����ѯ���ݡ� + DCL�����ݿ������ԡ� + GRANT������Ȩ�ޡ� + grant��create��view��to��scott; + 3. �������ͣ� + char(n)�������ַ�����n���ֽ��������2000�ֽڡ� + varchar2(n):�䳤�ַ�����n���ֽ��������4000�ֽڡ� + number(n,m):n������λ���֣�m�����м�λС���� + date:ʱ�����ڣ�select sysdate from dual; +����SQL��DDL�� DML��TCL��DQL�� +�����Ĺ��ɣ� + 1.ǰ�ˣ�HTML��CSS��JS��JQuery + 2.��̨��Java + 3.���ݿ⣺Oracle�� Mysql�����׹����չ��� + +1.��װOracle + + �����û��� + sys�������û� �������Ȩ�� ����sysdba��ɫ �����ݿ����Ա�� + �д������ݿ��Ȩ�� + system����������Ա Ȩ�޽����ڳ�������Ա + + scott:��ͨ�û� һ��������ϰ scott -- tiger + �����û���alter user scott account unlock + �������룺alter user scott identified by tiger + +2.��¼��ʽ + 1��sqlplus �س� + �����û��� + �������� + + 2��sqlplus/nolog + conn scott/tiger + +3. DB��DataBase �������ݽṹ���������ݵIJֿ�(��ϵ�����ݿ�),�Զ�ά����ʽ�洢����(�����У��ֶ��� �����У�����) + DBMS�����ݿ����ϵͳ(mysql��oracle��SqlServer) + +4.�������� + sqlplus �����ݿ� + conn �û���/���� �������ݿ� + show user; ��ʾ��ǰ�û� + exit; �Ͽ����� + quit; �Ͽ����� + +5.SQL:�ṹ����ѯ���� + SQL�ķ��ࣺ + DDL ���ݶ������� ���ڲ������ݶ��󣨱� ��ͼ ���� ������ + DML ���ݲ������� ������ɾ�ı��е�����(ͨ����������) + TCL ����������� �������ݵ��ύ�ͳ��� + DQL ���ݲ�ѯ���� ���ڲ�ѯ���ݼ�¼ + DCL ���ݿ������� ����ִ��Ȩ�޷��� + + DDL�����ݶ������� + ---���ڲ������ݿ���󣨱� ��ͼ ���� ������ + CREATE���������������������� + ALTER���޸ı�����������Ľṹ + DROP��ɾ��������������Ľṹ + TRUNCATE����ձ�(ɾ���������ݣ��������ṹ) + DML�����ݲ������� + ---������ɾ�ı��е�����(ͨ�����������������) + INSERT INTO�������ݲ��뵽���� + DELETE��ɾ���������� ---- WHERE + UPDATE ���� SET�����±������� --- WHERE + + TCL������������� + COMMIT���ύ--���� + ROLLBACK���ع�---���� + + DQL�����ݲ�ѯ���� + SELECT ��ѯ***** + + DCL�����ݿ������� + ����ִ��Ȩ�޷��� GRANT REVOKE +6.DDL ���ݶ������� + �鿴�û�����ı��� + SELECT table_name FROM user_tables;--�û���������б����ᱻ�г��� + (1)������ + CREATE TABLE ���� ( + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��], + ����(�ֶ���) �������� [Ĭ��ֵ Լ��] + ); + CREATE TABLE users( + id number(10) primary key, + name varchar2(20) not null unique, + salary number(10,2), + create_time date default sysdate + ); + id(����) �ص㣺���������ű���ÿһ���������ֵ������ͬ���ұ�����ֵ���ǿ���Ψһ�� + �������� + ������������ + 1����������ĸ��ͷ + 2��������1-30���ַ�֮�� + 3������ֻ�ܰ���A-Z,a-z,0-9,_,$,��# + 4�����벻�ܺ��û������������������ + 5�����벻����Oracle�ı����� + ��������: + 1��NUMBER:��ֵ���� + NUMBER(X):x����λ�� + NUMBER(3) ����ܴ��3λ�� + NUMBER(X,Y):x��ʾ����λ y��ʾ�����м�λ��С�� + NUMBER(6,2) 8888.88 + 2��CHAR:�����ַ��� + CHAR(X):X��ʾ��ռ�����ֽ� ����2000�ֽ� + 3��VARCHAR2:�䳤�ַ��� + VARCHAR2(X):X��ʾ��ռ�����ֽ�(�������б�������ݳ��ȣ�ռ�õĿռ��DZ仯��) ���4000�ֽ� + 4��DATE ʱ������ SELECT SYSDATE FROM DUAL; + α����dualȷʵ��һ�ű�����һ��ֻ��һ��һ�еı���ϰ���ϳ�Ϊα������Ϊ�����洢��������ݡ� + �鿴���ṹ(��command window)��DESC table_name; + (2)ɾ������ + DROP TABLE users;--ɾ�������ݺͽṹ + TRUNCATE TABLE users;--ɾ�������ݣ��������ͱ��ṹ + (3)�޸ı�(ALTER): + �޸ı�����RENAME old_name TO new_name + �޸ı��ṹ�� + ����������ֶΣ�ֻ�����ӵ���󣬲��ܲ��뵽���е������У���ALTER TABLE table_name ADD(�ֶ�������������[Ĭ��ֵ Լ��]), ���磺ALTER TABLE usersplus ADD(school varchar2(20)) + ɾ�����������ֶΣ� ALTER TABLE biaoming DROP(�ֶ����������磺ALTER TABLE userplus DROP(school�� + �޸ı��������ֶεĸ�ʽ��ALTER TABLE ���� MODIFY(�ֶ�������������[Ĭ��ֵ Լ��])�����磺ALTER TABLE userplus MODIFY(salary number(10)) + �޸ı����ֶ����ƣ�ALTER TABLE userplus RENAME column name to username +7.DML ���ݲ������� + (1)������������ݣ� + ȫ�ֶβ������ݣ�INSERT INTO ���� VALUES(1, '����', 8000,sysdate, '����Ա'); ע�⣺values�е�����˳��Ҫ�Ͷ����ֶ�ʱһ�¡� + �����ֶβ������ݣ�INSERT INTO ����(��Ҫ�������ݵ��ֶ�)VALUES(���ֶ�˳�����Ӧ������)�� + (2)�޸ı����������ݣ� + ���޸ı�������ʱ��ͨ������where�Ӿ����޶�Ҫ�޸ĵļ�¼��������ֻ���޸�����where�����ļ�¼�������޸�ȫ���� + UPDATE ���� SET �ֶ���=���� WHERE �ֶ���=���� + (3)ɾ���������ݣ� + DELETE FROM ���� WHERE �ֶ���=���ݡ�ͨ����where�Ӿ����ã�����ɾ�������������ݡ� + ��DDL����е�TRUNCATE���ͬ����ɾ�������ݵ����á� + TRUNCATE��DELETE������DELETE������������ɾ�����ݣ���TRUNCATE�����е�����ȫ��ɾ����DELETE��DML���ԣ�����rollback��TRUNCATE������������ԣ�������Ч���޷�rollback. + ɾ��ȫ����¼��DELETE FROM userplus(Ч�ʵ�); TRUNCATE TABLE userplus(Ч�ʸ�). +8.TCL������������� + (1)commit:�ύ�� ����ͨ������ťȥ�ύ�����������ر����ݿ�ʱ��Ĭ���ύ�� + (2)rollback���ع��� + ������ĸ����ԣ� + ԭ����(Atomicity): +�� �� ԭ������ָ������������в���Ҫôȫ���ɹ�ȫ��Ӧ�ã�Ҫôȫ��ʧ�ܻع����������IJ�������ɹ��ͱ���Ҫ��ȫӦ�õ����ݿ⣬�������ʧ�����ܶ����ݿ����κ�Ӱ�졣 + һ����(Consistency): + һ������ָһ������ִ��֮ǰ��ִ��֮�󶼱��봦��һ����״̬����ת����˵�������û�A���û�B���ߵ�Ǯ������һ����5000����ô����A��B֮�����ת�ˣ�ת�����ˣ���������������û���Ǯ�������Ӧ�û�����5000������������һ���ԡ� + ������(Isolation): +���� ��������ָ�����������֮��Ҫ�໥���롣������û������������ݿ�ʱ���������ͬһ�ű�ʱ�����ݿ�Ϊÿһ���û����������񣬲��ܱ���������IJ��������š� + �־���(Durability): +���� �־�����ָһ������һ�����ύ�ˣ���ô�����ݿ��е����ݵĸı���������Եģ������������ݿ�ϵͳ�������ϵ������Ҳ���ᶪʧ�ύ����IJ����� +9.DQL�����ݲ�ѯ���� + ������ѯ�����SELECT�Ӿ��FROM�Ӿ���ɡ� + SELECT��ָ����ʾ���ֶ����ƣ� FROM��ָ��������Դ�� + (1)��ѯ���У�SELECT * FROM ����; + (2)��ѯ����������Ϣ��SELECT field1, field2 FROM table_name where �޶�����; + ע�⣺SELECT������ʾ�ֶΣ�����ʽ�������� <>�Dz����ڡ����������ƽϳ��������ڲ�ѯ�����������ʾ���������б����� + SELECT salary*12 "��н" FROM userplus WHERE username='����';--���� + SELECT salary*12 AS "��н" FROM userplus WHERE username='����';--���� + �������������ֿ�ͷ���������ֻ���ſ�ͷ��������Ҫ��""���ϡ� +10.���ú���(���к���):4 + (1)�ַ���������13 + CONCAT(c1,c2),�����ַ�������c1,c2�����ֶ�ƴ����ʾ�����磺SELECT CONCAT(CONCAT(ename, ': ') ,sal)from EMP; + ||���Ӷ���ַ������š����磺SELECT ename||': '||sal as "����������" from EMP; + LENGTH(ch):��ʾ�ַ������ȣ� ���磺SELECT LENGTH(ename) from EMP; + UPPER(�ֶ���)��ת��д + LOWER(�ֶ���): תСд + INITCAP(�ֶ���): ֻ������ĸ��д + TRIM():ȥ���ַ���ǰ��ָ�����ݡ����磨�Ҳ�д�ַ�������SELECT TRIM('E' from 'EEHELLO WORLDEEE') from dual; SELECT ename, trim('A' FROM ename) from emp; --ȥ��Ա������ǰ���'A' + LTRIM('',''):ȥ���ַ��ش����ָ�����ݡ����磨���д�ַ�������SELECT LTRIM('EEHELLO WORLDEEE', 'E') from dual; + RTRIM('',''):ȥ���ַ����Ҳ�ָ�����ݡ� + LPAD(ch1, m, ch2): ��λ������ch1��Ҫ��ѯ��ʾ�����ݣ�mָ��λ��������λ������ʱ��ch2���в�λ����emp����ʹ����λ����sql��0����6λ�����磺select ename, LPAD(sal, 6, 0) AS salary from emp + RPAD(ch1, m, ch2): �Ҳ�λ������ + SUBSTR��ch,m,n):��ȡ�ַ�������ȡch����ַ�������m��ʼ��ȡ������ȡn������mΪ���������ǴӺ���ǰ�����±��ѯ���Ӷ����н�ȡ�� + INSTR(ch1,ch2,m,n):����ch1��ch2���ֵ�λ�ã�m�������ĸ�λ�ÿ�ʼ������n�����ڼ��γ��֣���ָ��m��n��Ĭ��ֵ����1���±��1��ʼ����ƥ�����ݵ�ʱ�򷵻�0. + (2)���ֺ�����5 + ROUND(n,m):�����������룬�����е�n�������κ�����(Ҫ������������);m������������mȡ�������������뵽С������mλ��mȡ0���������뵽����λ(mȱʡ��Ĭ����0)��mȡ�������������뵽С����ǰmλ�� + TRUNC(n,m):��ȡ����û���������� + MOD(m,n):����m����n���������nΪ0��ֱ�ӷ���m�� + CEIL(n):����ȡ�������ڸ�������С������ + FLOOR(n):����ȡ����С�ڸ�������������� + (3)���ں���:11 + sysdate:���ص�ǰ��ϵͳʱ�䣬��ȷ���롣 + systimestamp:ʱ��������ص�ǰϵͳ���ں�ʱ�䣬��ȷ�����롣 + (1)TO_DATE():����ת���������൱��Java�е�SimpleDateFormat��parse()���������������ַ������ո��������ڸ�ʽת��ΪDate�� + select to_date('2018-12-01 11:30','YYYY-MM-DD HH:MI:SS') from dual; + select to_date('2018-12-01 13:30','YYYY-MM-DD HH24:MI:SS') from dual; + (2)TO_CHAR():���ڽ�DATE���ո��������ڸ�ʽת��Ϊ�ַ����� + select ename, to_char(hiredate,'YYYY-MM-DD HH:MI:SS') from emp; + select ename, to_char(hiredate,'YYYY"��"MM"��"DD"��"') from emp; + (3)LAST_DAY(date):����ָ�����������µ����һ�졣 + (4)ADD_MONTHS(date,i):��������date����i���º������ֵ����i�Ǹ��������õ��Ǽ�ȥi���º������ֵ�� + (5)MONTHS_BETWEEN(date1,date2):������������֮�������ٸ���;��date2ʱ���date1ʱ��������õ���ֵ�������������ڼ���������£������õ���С��λ�Ľ���� + (6)date�������������ݿ��п��Խ��м������������������������select ename, sysdate-hiredate from emp; + (7)LEAST(date1,date2):��ʾ����ʱ���е���Сֵ���������ͱ���һ�¡� + (8)GREATEST(date1,date2):��ʾ����ʱ���е����ֵ���������ͱ���һ�¡� + (9)EXTRACT(date from datetime):�Ӳ���datetime����ȡ����dateָ�������ݣ�������ȡ���ꡢ�¡��ա� + (4)��ֵ������2 + null�ĺ��壺��ʱ���е�ijЩ�ֶ�ֵ����δ֪����ʱ�����ڣ�ȡֵnull����Java���������͵�Ĭ��ֵΪnull����sql�У��κ��������;���ȡֵΪnull�� + null������ѯ:�������κ�ֵ�� is null �� is not null. + NVL(arg1,arg2):��nullת��Ϊ��nullֵ�����arg1Ϊnull������arg2�����򷵻ر�����arg1��arg2�������κβ������ͣ��������������������ͱ�����һ�µġ� + NVL2(arg,res1,res2):���argΪnull���򷵻�res2,arg��Ϊnull�򷵻�res1. +11.������ѯ��� + (1)from�Ӿ�:select * from table_name;select����ָ��Ҫ��ѯ���У�fromָ��Ҫ���ĸ����в�ѯ�� + (2)where�Ӿ�:��select����п�����where�Ӿ���ʹ�ñȽϲ��������Ʋ�ѯ����� +12.��ѯ���� + (1)�Ƚ��������>,<,>=,<=,<>,= + (2)�ؼ��֣�AND(����),OR(��)��AND���ȼ�����OR������ʹ��()�����OR�����ȼ�˳�� + (3)ģ����ѯ + LIKE������ģ��ƥ���ַ���; + _��������һ���ַ�,%��������0~n���ַ��� + (4)IN��NOT IN�� + IN(list):ȡ���б���Χ�е����ݡ� + NOT IN(list):ȡ�������ϴ��б��е����ݼ�¼�� + (5)BETWEEN...AND...:��������ѯ����ij��ֵ��Χ���������ݡ� + (6)ALL��ANY: ALL��ANY���ܵ���ʹ�ã���Ҫ��ϵ��бȽϲ�����(>,<,>=,<=,<>,=)һ��ʹ�á� + >ANY:������С + ALL:������� + , <, >=, <=, <>, !=, is null(is not null), between...and..., in; + �߼������not, and, or + BETWEEN...AND...��AND������ + BETWEEN...AND...:ֻ�������Ƚ�һ���ֶ�ֵ + AND:���������Ƚϲ�ͬ���ֶ� +14.ORDER BY�����򣬶Խ���������򣬱��������select�����һ���Ӿ䡣 + asc��д������desc��������������ֶ��а���null�����������ֵ�� + ���Զ�����Ϊ�����׼ʱ�����Ȱ��յ�һ�н������������һ��������ͬ�����Եڶ��������Դ����ơ� +15.�ۺϺ���(���к��������麯��) + (1)MAX��MIN:����ȡ���л����ʽ�������Сֵ����������ͳ���κ��������ͣ��������֡��ַ������ڡ�����NULLֵ�� + (2)AVG��SUM:����ͳ���л����ʽ��ƽ��ֵ���ܺͣ�ֻ�ܲ����������ͣ�����NULLֵ�� + (3)COUNT:����������еļ�¼����������NULLֵ�� + (4)�ۺϺ�����NULLֵ�Ĵ������ۺϺ�������NULLֵ��select count(comm)��avg(comm) from emp;--�Զ�����commΪnull�ļ�¼; + �����ϣ������NULLֵ������Ҫʹ��NVL��NVL2:select count(nvl(comm,0)), avg(nvl(comm,0)) from emp; +16.GROUP BY:���顣�����������ָ���ֶν��з��顣������򣺸��ֶ��µ�ֵһ���ļ�¼������һ�顣 + ���select��������˾ۺϺ�����ͬʱ�ֳ�����ijһ�ֶ�ʱ����ô���ֶα��������group by�Ӿ��С� + where���治��ʹ�þۺϺ���,��Ϊwhere�ǷǷ��麯���Ĺ����жϡ�select max(sal),deptno from emp where max(sal)>4000 group by deptno;--����ִ�� + HAVING:�Ƿ��飨�ۺϣ������Ĺ����ж�;�������GROUP BY֮�󣬲��ܵ�������;�����Է����Ľ����һ������;���簴���ŷ���󣬵õ�ÿ�����ŵ���߹��ʣ����Լ��������������� + ��ѯ����������ʳ���4000�IJ����Լ������:select max(sal),deptno from emp group by deptno having max(sal)>4000; +17.SELECT���ִ�й��̣� + (1)ͨ��FROM�Ӿ����ҵ���Ҫ��ѯ�ı��� + (2)ͨ��WHERE���зǷ��麯����ɸѡ�жϡ� + (3)ͨ��GROUP BY�Ӿ���ɷ�������� + (4)ͨ��HAVING�Ӿ�����麯��ɸѡ�жϡ� + (5)ͨ��SELECT�Ӿ�ѡ��Ҫ��ʾ���л����ʽ���麯���� + (6)ͨ��ORDER BY�Ӿ������������� +18.�����ܽ� + ���к����Ͷ��к����ķ��ಢ����˵���� + ���к����� + �ַ�����13��CONCAT(c1,c2), LENGTH(ch), UPPER(�ֶ���), LOWER(�ֶ���), INITCAP(�ֶ���), TRIM('e'FROM'ee'), LTRIM('ee','e'), RTRIM('ee','e'), LPAD(ch1, m, ch2), RPAD(ch1, m, ch2), SUBSTR��ch,m,n), INSTR(ch1,ch2,m,n) + ��ֵ����5��ROUND(n,m), TRUNC(n,m), MOD(m,n), CEIL(n), FLOOR(n) + ���ں���9��sysdate, systimestamp, LAST_DATE(date), ADD_MONTHS(date,i), MONTHS_BETWEEN(date1,date2), LEAST(date1,date2), GREATEST(date1,date2), EXTRACT(date from datetime) + ת�������� + ��ʾת����TO_DATE(), TO_CHAR() + ��ʽת���������������ַ�������ͨ����ת��Ϊ�ַ����� + ͨ�ú���2��NVL(), NVL2() + ע�⣺ͨ�����ڲ����������ݣ���������Ƕ�׶�㣬���ʱ������������ִ�С� + ���к���(�ۺϺ���)5��MAX()���ֵ, MIN()��Сֵ, SUM()���, AVG()ƽ��ֵ, COUNT()ͳ�� +19.������ѯ�� + (1)���ʵ��Ӧ��������Ҫ�����ݣ���������Ҫ��ѯ�������������ϵı������ֲ�ѯ�������Ӳ�ѯ�����Ӳ�ѯͨ�����������໥��ϵ�ĸ��ӹ�ϵ�� + (2)�����������һ�ű��������г�Ϊ�������������ı����ڹ�����ϵ�����ڶ��һ���� + (3)�����֮��Ĺ�ϵ��һ�Զ�(���)����Զ�(ͨ�������ű�������ϵ)��һ��һ�� + (4)N�ű��Ĺ�����ѯ������Ҫ(N-1)����������;��ֵ���ӣ����=����;����ָ����������������ֵѿ�����(ָ������������ÿ������ÿһ�к���������ÿһ������ϣ���������Ľ����)�� + (5)�ܽ᣺ + ���Ը����������ͨ������ָ���ֶ������ĸ��������������ֶ�������ͬ�����ָ��������Դ�� + ֻ���������������Ľ�����Żᱻ��ѯ������(����deptno40��û���ˣ���˲��ᱻ��ʾ����)�� + ���й�����������ͬʱ�������������͹������������ݲŻᱻ��ѯ������ +20.�������ࣺ + ?�����ӣ����������������������ļ�¼�������Ϻ͵�ֵ����û������ֻ��д����ͬ�� + �﷨����1 INNER JOIN ��2 ON ��������;(INNER����ʡ��) + �����ӣ����������������������ݼ�¼����Щ�������Ҫ������Щ���������������ļ�¼����Ҫʹ�������ӡ�������Ͳ��������������Ľ��ȫ����ѯ������ + create table empc as select * from emp;--���ٴ�һ�����и������� + insert into empc(empno, ename, deptno)values(1000,'JACK',50); + �������ӣ�(�����ȫ�����ұ��IJ���)��������ļ�¼���ڱ����ӵ��ұ����ҳ����������ļ�¼��֮ƥ�䣬����Ҳ��������ƥ�����null��ʾ�� + �﷨����1 LEFT OUTER JOIN ��2 ON ��������;(OUTER����ʡ��) + �������ӣ�(�ұ���ȫ��������IJ���)�����ұ��ļ�¼���ڱ����ӵ�������ҳ����������ļ�¼��֮ƥ�䣬����Ҳ������ұ�ƥ�����null��ʾ�� + �﷨����1 RIGHT OUTER JOIN ��2 ON ��������;(OUTER����ʡ��) + ȫ�����ӣ����ط������������б��ļ�¼��û����֮ƥ��ģ���null��ʾ(����������Ӻ������ӵIJ���)�� + �﷨����1 FULL OUTER JOIN ��2 ON ��������;(OUTER����ʡ��) + ?�����ӣ����ӵ�����������ͬһ���������е�һ���ֶο��Զ�Ӧ��ǰ���������ֶΡ� + Oracle���ݿ��Դ��﷨�� + (1)�ѿ������� + (2)�����ӣ�a.��ֵ���� + b.�ǵ�ֵ����:���Ա����š����������ʡ����ʵȼ���select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal; + c.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno=d.deptno(+);--��ѯԱ���ı�ţ��������������ƣ������������κβ��ŵ�Ա���� + d.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno(+)=d.deptno;--��ѯԱ���ı�ţ��������������ƣ�����û��Ա���IJ��š� + e.�����ӣ� + ͨ�ñ�׼��SQL99�﷨�� + (1)�������ӣ��������Ӻ͵ѿ�����������ģʽ��ͬ�����DZ�1�е�ÿһ�ж����2�е������н���һ�����ӡ�select * from emp cross join dept; + (2)��Ȼ���ӣ�select * from emp natural join dept;--���ܹ�ƥ�䵽��ֵ���������Լ������ͬ���У�����ô���ǵ�ֵ���ӵĽ��.����dept���е�ֵ���ӡ� + select * from emp natural join salgrade;--������ƥ�䵽��ֵ��������ô������ǽ������ӡ� + (3)�������ӣ�left outer join + (4)�������ӣ�right outer join + (5)ȫ�����ӣ�full outer join +21.�Ӳ�ѯ�� + Ϊ�˸���ѯ�ṩ���ݶ�����ִ�еIJ�ѯ�������Ӳ�ѯ���Ӳ�ѯ��Ƕ��������SQL����е�SELECT��䡣�Ӳ�ѯǶ�������������ѯ�򸸲�ѯ�� + ע����� + (1)�Ӳ�ѯ��Ҫд�������С� + (2)�Ӳ�ѯ��Ҫд����������Ҷˡ� + (3)�Ӳ�ѯ����д��WHERE��HAVING��FROM�Ӿ��С� + (4)�Ӳ�ѯ��ͨ����дORDER BY�� + �Ӳ�ѯ��WHERE�Ӿ��У����ݷ��ؽ���IJ�ͬ���Ӳ�ѯ��Ϊ�����Ӳ�ѯ�������Ӳ�ѯ�������Ӳ�ѯ�� + (1)�����Ӳ�ѯ������һ��һ�����ݣ�Ҫ��ʹ�õ��в�����(>,>=,<,<=,<>,=) + (2)�����Ӳ�ѯ�����ض������ݣ�ʹ�ö��в�����(in,all,any) + (3)�����Ӳ�ѯ: ͨ�����ڽ����ڶ��β�ѯ����������FROM�Ӿ��С� + ͳ�Ƶ����Լ����ʵ������ڲ���ƽ�����ʵ�Ա����Ϣ�� + a.����ͳ�Ʋ���ƽ�����ʣ�select avg(sal) aa, deptno from emp group by deptno + b.Ա������С��<����ƽ�����ʣ�select sal, ename, e.deptno from emp e, (select avg(sal) aa, deptno from emp group by deptno) t where e.deptno=t.deptno and e.sal=6; + ҳ�룺page��ÿҳ���pageSizeΪ5����һҳ1~5���ڶ�ҳ6~10������ҳ11~15. start=(page-1)pageSize+1; end=pageSize*page; +23.DECODE���� + DECODE(����1��ֵ1�����1��ֵ2�����2...[,Ĭ��ֵ]);�ò�������ȥ��ֵƥ�䣬ƥ�䵽������ʾ��Ӧ�Ľ��;��ƥ������ʾĬ��ֵ��Ĭ��ֵ����������ʾnull(�൱��Java�е�switch)�� + --��ѯְԱ��������ְԱ��ְλ���㽱������ְλ�ֱ���'MANAGER','SALESMAN','ANALYST'ʱ��������ֱ���нˮ��1.2,1.1,1.05��������Ϊԭ����нˮֵ�� + select ename, job, sal, decode(job,'MANAGER',sal*1.2,'SALESMAN',sal*1.1,'ANALYST',sal*1.05,sal) "����" from emp; + select * from emp order by decode(job,'MANAGER','1','SALESMAN','2','ANALYST','3');--�Զ������� +24.���Ϻ��� + (1)UNION��������������ѯ�����������union(�����ظ����ݣ�������), union all(�������ظ����ݣ�������); + select * from emp where sal>1500 union select * from emp where deptno=20;--����֮���ܹ�9����¼ + (2)INTERSECT������ͬʱ����������������е����ݡ� + (3)MINUS��������������� + (4)ʹ�ü��Ϻ�����ԭ�����еIJ�ѯ������ܲ�������ͬһ�ű������ұ����֮���޹����� + (5)ע�����a.��Ӧ���е�������Ҫƥ�䡣 + b.�е�����Ҫ����˳��д�� + c.�е�����Ҫһ�£������һ��ʱ����nullȥ���䲻ƥ����С� +25.DDL���������� + (1)�������ռ䣺�߼���λ��ͨ�������½�һ����Ŀ���ͻ��´������ռ䣬�ڱ��ռ��д����û����������� + ���л���system���﷨��CREATE tablespace ���ռ����� datafile '�ļ���·��' size ��С autoextend on �Զ���չ next ÿ����չ�Ĵ�С; + (2)ɾ�����ռ䣺drop tablespace ���ռ����� + (3)�����û���create user �û��� identified by ���� default tablespace ���ռ������; + (4)ɾ���û���drop user �û��� cascade; + (5)�����û����룺alter user �û��� identified by ����; + (6)��Ȩ��grant connect to �û���; + (7)�����ɫ��grant dba to �û���; + (8)ʹ���Ӳ�ѯ�ķ�ʽ������(�л������´������û�֮�󣩣� + ֻ���Ʊ��ṹ�ͱ����ݣ����Ḵ���е�Լ����create table emp as select * from scott.emp;-- + �����ѯ����н�������Ǹ��Ʊ��ṹ�����ݣ����û�н�������Ǹ��Ʊ��ṹ��create table dept as select * from scott.dept where 1=2;--ֻ���Ʊ��ṹ��������,��Ϊ1=2Ϊfalse�� + ֻ���Ʊ�����(������������û��values�ؼ���):insert into ���� ��ѯ��� (���Ľṹ�����б��ṹһ��):insert into dept select * from scott.dept; +26.�е�Լ����Լ����Ҫ������Լ���������ݵĹ��� + ����Լ����primary key ����Ϊ�գ�����Ψһ�� + �ǿ�Լ����not null + ΨһԼ����unique + ���Լ����check(����) ��MySql���ǿ���д�ģ���MySql��ֱ�Ӻ��Լ�顣price number(10,2) check(price>100),--���Լ�����ڴ�������ʱ��ֱ��д(��primary key, not null, uniqueһ��λ�û�֮��) + ���Լ������Ҫ������Լ���ӱ�A�еļ�¼�����Ǵ���������B��(��֤����������)�� + (1)references �����(���������), --�ڴ�������ʱ��ֱ��д(��primary key, not null, unique, checkһ��λ�û�֮��) + (2)foreign key(�Լ�������) references �����(���������)--��һ�����Լ���ķ�ʽ��д�����һ�����ŵ�ǰ�� + (3)alter table books add foreign key(cid) references category(id);--���Բ��ڶ�����н������Լ���������ô����Ҳ��ʵ�����Լ���� +27.��ͼ�����ݿ����֮һ����ͼ��sql��������ֵĽ�ɫ�ͱ���ȫ��ͬ������ͼ������һ����ʵ���ڵı�������Ӧ����һ����ѯ���Ľ��������ͼ���ͱ�������һ������ + ���ã��򻯸��Ӳ�ѯ�������������ݷ��ʡ� + (1)��������ͼ��������������Ȩ�ޣ�ʹ��system�û���Ȩ����ͼ��Ȩ��䣺grant create view to �û����� + create view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10; + (2)�޸���ͼ�����滻����ͼ���Ӳ�ѯ��ʹ��create or replace view ��ͼ�� ���ɣ�����ͼ�����ھʹ��������ھ��滻��������ͼʱ�����Ը��и�������� + create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10; + (3)��ѯ��ͼ���������б������Ա���Ϊ׼������ԭ������select * from v_emp_10; + (4)��ͼ��DML����(������)������ͼ����DML�������Ƕ���ͼ������Դ�Ļ��������в����� + a.�������ݣ���ͼ�п��������ֶζ�������Ĭ��ֵ�����Բ���Υ������������Ӧ�ֶε�Լ�������翴�������ֶε�not null��Լ������������ʧ�ܣ��� + b.Ϊ��ͼ���Ӽ��ѡ����Ա������ͼ����DML����ʱ��Ⱦ��������������ڴ��������with check option + create or replace view v_emp_10 as select empno id,ename name,sal,deptno from emp where deptno=10 with check option;--���ܶԻ�������deptno10��������ݽ���DML������ + c.with read only��ֻ�������ܶ���ͼ����DML���������û������ͼ��ִ��DML�����ı�Ҫ���ڽ�����ͼʱ����Ϊֻ�������������������֤��ͼ��Ӧ�Ļ��������ݲ��ᱻ�Ƿ��޸ġ� + create or replace view v_emp_10 as select empno,ename,sal,deptno from emp where deptno=10 with read only;--with read onlyֻ�������ܶ���ͼ����DML������ + (5)��ͼ�����Ӳ�ѯ�IJ�ͬ����Ϊ����ͼ�͸�����ͼ�����Ӳ�ѯ�а������к���������ʽ����麯��ʱ������ͼ��һ��������ͼ��������ͼ���ܽ���DML���������ұ��뱻�Ӳ�ѯ�еı���ʽ������������� + (6)ɾ����ͼ��DROP VIEW ��ͼ���ơ� +28.���У�sequence����������Ψһ����ֵ�����ݿ����ͨ����Ϊ��������ֵ�������Ƕ��������ݿ�������в��������ڱ��� + (1)�������У�create sequence ������ [start with **(��ʼ����) increment by **(����,ÿ�����Ӽ��������ɸ�) maxvalue **(���ֵ) / minvalue **(��Сֵ)]; + (2)������������α��:nextval/*��ȡ���е��¸�ֵ*/ ,currval/*��ȡ���еĵ�ǰֵ*/ + �����д����Ժ������ִ��nextval��֮�����ʹ��currval����ȡ���еĵ�һ��ֵ����ʹ������ֵΪusers�������µļ�¼�� + select users_seq.currval from dual;--�鿴��ǰ����ֵ�� + (3)ɾ������:drop sequence users_seq; + (4)���ӣ� + create sequence users_seq start with 100 increment by 10;--��������,��һ��һ��Ҫ��nextval��������currval + insert into users(id,name)values(users_seq.nextval,'Tony');--������� + select users_seq.currval from dual;--�鿴��ǰ����ֵ�� + drop sequence users_seq;--ɾ������ + +29.������һ����߲�ѯЧ�ʵĻ���(�������ֵ��Ŀ¼)��������Ϊ����������ȥ�ء�����������������ֶΣ�����Ϊ������������ + ?(1)����������create [unique] index �������� on ����(����) + create index idx_users_name on users(ename);--��users����ename���Ͻ������� + (2)���Ӷ���������create index idx_users_salary on users(name,salary); + (3)��ѯ�������Զ�Ӧ������������Ҫ����ȥ�ƶ�����߲�ѯ�ٶȡ� + select users.* from users order by name, salary; + (4)�ع�������������ƽ����������޸ı�������ع�һ��������ƽ��������� + alter index �������� rebuild;--�����޸����ݵı��������齨�������� + (5)ɾ��������drop index idx_users_salary; \ No newline at end of file diff --git a/JDBC/src/note/sql/SQLPractice.sql b/JDBC/src/note/sql/SQLPractice.sql new file mode 100644 index 0000000..ad16146 --- /dev/null +++ b/JDBC/src/note/sql/SQLPractice.sql @@ -0,0 +1,233 @@ +1~10 homework: +--1����USERS���в����Ա��ֶΣ���ָ��Ĭ��ֵΪ�� +alter table userplus add(sex char(2) default '��'); +--2��ɾ���������������˵���Ϣ +delete from userplus where username='����'; +--3����ѯUSERS�����û������� +select username from userplus; +--4����ѯÿ���û��Ĺ����Ƕ��� +select username, salary from userplus; +--5���鿴�����Ĺ��� +select salary from userplus where username='����'; +--6����ѯÿ���û�����н +select salary*12 as "��н" from userplus; +--7����ѯ�û����ʴ���2000���û���Ϣ +select * from userplus where salary > 2000; +--8���� ���� ���� ������ʾ +select username||': '||salary as "����������" from userplus; +--9����ѯUSERS����ÿ���û������ֳ��� +select username,length(username) from userplus; +--10����ѯ����3λ���ϵ��û���š����������ʡ����� +select id, username, salary, job from userplus where length(username)>=3; + +10~12 homework: +--1�����ѯ��DEPT�����в��ŵ������ +select * from dept; +--2����ѯ��DEPT�еIJ��źš��������������ֶε�������Ϣ�� +select deptno, dname from dept; +--3����ӱ�EMP�в�ѯ10�Ų��Ź����Ĺ�Ա�����͹��ʡ� +select ename, sal from emp where deptno=10; +--4����ӱ�EMP�в��ҹ�����ְԱCLERK����MANAGER�Ĺ�Ա���������ʡ� +select ename, sal from emp where job in ('CLERK','MANAGER'); +--5������EMP���в��Ҳ��ź���10��30֮��Ĺ�Ա�����������źš����ʡ������� +select ename, deptno, sal, job from emp where deptno between 10 and 30; +--6����ӱ�EMP�в���������J��ͷ���й�Ա�����������ʡ�ְλ�� +select ename, sal, job from emp where ename like 'J%'; +--7����ӱ�EMP�в��ҹ��ʵ���2000�Ĺ�Ա�����������������ʣ��������ʽ������С� +select ename, job, sal from emp where sal<2000 order by sal desc; +--8���ڱ�EMP�в�ѯ���й��ʸ���JONES�����й�Ա�����������͹��ʡ� +select ename, job, sal from emp where sal>(select sal from emp where ename='JONES'); +--9���г�û�ж�Ӧ���ű���Ϣ�����й�Ա�������������Լ����źš� +select ename, job, deptno from emp where deptno not in(select deptno from dept); +--10�����ҹ�����1000��3000֮��Ĺ�Ա���ڲ��ŵ�������Ա��Ϣ +select * from emp where deptno in (select distinct deptno from emp where sal between 1000 and 3000); +--11����ѯ���й�Ա��������SAL��COMM֮�͡� +select ename, sal, comm, NVL2(comm,comm+sal,sal) from emp; +--12����ѯ������CHICAGO�����ľ���MANAGER������ԱSALESMAN������������ +select ename, sal from emp where deptno=(select deptno from dept where loc='CHICAGO') and job in('MANAGER','SALESMAN'); +select a.ename, a.sal from emp a, dept b where a.deptno=b.deptno and b.loc='CHICAGO' and a.job in('MANAGER','SALESMAN'); +--13����ѯ��ʾÿ����Ա���빫˾��׼ȷʱ�䣬����������������¡����� ʱ������ʾ�� +select ename, to_char(hiredate,'YYYY"��"MM"��"DD"��" HH:MI:SS') from emp; + +13~18 homework: +select * from dept +select * from emp +--1.�г�������һ��Ա�������в��š� +select deptno from emp group by deptno having count(empno)>=1; +--2.�г�н��ȡ�SMITH���������Ա���� +select * from emp where sal>(select sal from emp where ename='SMITH'); +--3.�г����н�����1500�ĸ��ֹ����� +select distinct(job) from emp group by job having min(sal)>1500; +--4.��ѯ����20,30 �еĸ�λ����"CLERK"��"SALESMAN"������Ա����Ϣ +select * from emp where deptno in (20,30) and job not in('CLERK','SALESMAN'); +--5.��ѯ��ƽ��Ա�����ʸߵ�Ա����Ϣ +select * from emp where sal > (select avg(sal) from emp); +--6.�ҳ�����10�����о����Ͳ���20�����а���Ա����ϸ���ϡ� +select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK'); +--7.��ʾ�����С�R���Ĺ�Ա������ +select ename from emp where ename not like '%R%'; +--8.��ʾ��Ա������������������ޣ������ϵĹ�Ա������ǰ�档 +select ename, hiredate from emp order by hiredate asc; +--9.����ÿ����Ա����ʾ����빫˾������ +select ename, trunc(sysdate-hiredate+1) as "����" from emp; +--10.��ѯ����ְλ��ƽ�������Ƕ��٣� +select job, avg(sal) from emp group by job; +--11.�鿴ÿ�������ж����� +select deptno, count(*) from emp group by deptno; +--12.�鿴ÿ��ְλ��ƽ�������빤���ܺ� +select job, avg(sal),sum(sal) from emp group by job; +--13.���emp�������в��ŵ����нˮ�����нˮ�����ű��Ϊ10�IJ��Ų���ʾ�� +select deptno, max(sal), min(sal) from emp where deptno<>10 group by deptno; +--14.��ѯ���������С�A���ַ�������нˮ��1000���ϣ�������1000��������Ա����Ϣ�� +select * from emp where ename like '%A%' and sal>1000; +--15.��ʾнˮ����˵�ְλ�� +select job from emp where sal=(select max(sal) from emp); +select job from emp where sal>=all(select sal from emp); +--16.�г�����Ա�����깤��,����н�ӵ͵������� +select ename, sal*12 as "��н" from emp order by sal; + +19~21 homework: +select * from emp; +select * from salgrade; +--1.��ʾԱ�����еIJ��ظ��ĸ�λjob +select distinct(job) from emp; +--2.��ѯ10���Ź��ʴ���3000��Ա����Ϣ��Ҫ��Ա������ְʱ����ǰ�������� +select * from emp where deptno=10 and sal>3000 order by hiredate; +--3.�г��������ŵ�MANAGER �����н�� +select deptno,min(sal) from emp where job='MANAGER' group by deptno; +--4.��ѯ����Ա���Ĺ����ص㣬����û��Ա���ĵص�ҲҪ��ʾ +select ename,loc from dept d left outer join emp e on d.deptno=e.deptno; +--5.��ѯ���е�Ա����Ϣ�Ͳ������ƣ������������κβ��ŵ�Ա�� +select empno, ename, dname, e.deptno from emp e left outer join dept d on e.deptno=d.deptno; +--6.�г���"SCOTT"������ͬ����������Ա������������ +select empno, ename, job, dname from emp e,dept d where e.deptno=d.deptno and job=(select job from emp where ename='SCOTT'); +--7.�г��ܹ���������ֱ���ϼ�������Ա���ı�ţ��������������� +select empno, ename, e.deptno, dname from emp e,dept d where e.deptno=d.deptno and hiredate<(select hiredate from emp m where e.mgr=m.empno); +--8.�г�ְλΪ��CLERK����Ա�������������ڲ������ƣ��������� +select ename, dname, t.b from emp e, dept d, (select deptno,count(*) b from emp group by deptno) t where e.deptno=d.deptno and e.deptno=t.deptno and job='CLERK'; +--9.��ѯ������4��Ա���IJ��ŵIJ������� +select e.deptno, count(*),dname from emp e, dept d where e.deptno=d.deptno group by e.deptno,dname having count(*)>4; +select d.dname from dept d where deptno in (select deptno from emp group by deptno having count(*)>4); + --�Լ�д�� +select d.deptno, d.dname, t.count from emp e, dept d, (select count(*) count from emp a group by deptno) t where e.deptno=d.deptno and t.count>4; +--10.��ѯ���ʸ���30�Ų��ŵ�����Ա���Ĺ��ʵ�Ա�����������ʼ��������� +select ename, sal, e.deptno, dname from emp e,dept d where sal>(select max(sal) from emp where deptno=30); +--11.��ѯ����KING�����ڲ��ŵIJ��ű�š����������Լ��ò������Ա������ +select e.deptno, dname, count(*) from emp e,dept d where e.deptno=d.deptno and e.deptno=( select deptno from emp where ename='KING') group by e.deptno,dname; + --�Լ�д�ģ���̫�ã� +select e.deptno, dname, e.deptno,(select count(*) count from emp where deptno= (select deptno from emp where ename='KING')) from emp e, dept d where e.deptno=d.deptno and ename='KING'; +--12.��ѯƽ��������ߵIJ��ŵIJ��ű�š��������ƺ͸ò��ŵ�ƽ������ +select e.deptno, d.dname, avg(sal) from emp e, dept d where e.deptno=d.deptno group by e.deptno,d.dname having avg(sal)>=all(select avg(sal) from emp group by deptno); + --�Լ�д�� +select deptno from emp group by deptno having avg(sal)>=all(select avg(sal) from emp group by deptno); +--13.���ʵȼ�Ϊ2�ȼ��� Ա����Ϣ�������ڲ��ŵ���Ϣ +select * from emp e,dept d, salgrade s where e.deptno=d.deptno and s.grade=2 and e.sal between s.losal and s.hisal; + --�Լ�д�� +select e.ename, e.sal from emp e,dept d, (select losal, hisal from salgrade where grade=2) t where e.deptno=d.deptno and e.sal between t.losal and t.hisal; +--14.10�Ų��ŵIJ�����Ϣ ��Ա��Ϣ ���ʵȼ� +select d.*,e.*,s.grade from emp e,dept d, salgrade s where e.deptno=d.deptno and e.deptno=10 and e.sal between s.losal and s.hisal; + --�Լ�д�� +select ename, sal, (select t.grade from salgrade t where e.sal between t.losal and t.hisal) grade from emp e where deptno=10; +--15.��ѯ����쵼�ߵ�нˮ�ȼ� +select grade from salgrade where (select sal from emp where mgr is null) between losal and hisal + --�Լ�д�� +select ename, sal, (select t.grade from salgrade t where e.sal between t.losal and t.hisal) grade from emp e where mgr is null; +--16.SQL����ִ�й��̼���ÿ���Ӿ�����ã� +FROM,WHERE,GROUP BY,HAVING,SELECT,ORDER BY +--17.���ݿ����������ͼ����ȼ��� +();���������*,/,+,-;���ӷ�||;�Ƚ������=, >, <, >=, <=, <>, !=, is null(is not null), between...and..., in;�߼������not, and, or +--18.���ݿ�ĺ������ࣿ +DDL,DML,TCL,DQL,DCL +�ַ�,��ֵ,����,ת�� +--19.������ĸ����� +ԭ���� +һ���� +������ +�־��� + +--С�ܽ᣺ +--SQL����ִ�й��̼���ÿ���Ӿ�����ã� +(1)ͨ��FROM�Ӿ����ҵ���Ҫ��ѯ�ı��� +(2)ͨ��WHERE���зǷ��麯����ɸѡ�жϡ� +(3)ͨ��GROUP BY�Ӿ���ɷ�������� +(4)ͨ��HAVING�Ӿ�����麯��ɸѡ�жϡ� +(5)ͨ��SELECT�Ӿ�ѡ��Ҫ��ʾ���л����ʽ���麯���� +(6)ͨ��ORDER BY�Ӿ������������� + +--���ݿ����������ͼ����ȼ��� +(); +���������*,/,+,-; +���ӷ�||; +�Ƚ������=, >, <, >=, <=, <>, !=, is null(is not null), between...and..., in; +�߼������not, and, or + +--���ݿ�ĺ������ࣿ +���к�����CONCAT(c1,c2), LENGTH(ch), UPPER(�ֶ���), LOWER(�ֶ���), INITCAP(�ֶ���), TRIM('e'FROM'ee'), LTRIM('ee','e'), RTRIM('ee','e'), LPAD(ch1, m, ch2), RPAD(ch1, m, ch2), SUBSTR��ch,m,n), INSTR(ch1,ch2,m,n) +��ֵ������ROUND(n,m), TRUNC(n,m), MOD(m,n), CEIL(n), FLOOR(n) +���ں�����sysdate, systimestamp, LAST_DATE(date), ADD_MONTHS(date,i), MONTHS_BETWEEN(date1,date2), LEAST(date1,date2), GREATEST(date1,date2), EXTRACT(date from datetime)�ۺϺ�����count(),avg(),sum(),max(),min(), +ת�������� + ��ʾת����TO_DATE(), TO_CHAR() + ��ʽת���������������ַ�������ͨ����ת��Ϊ�ַ����� +ͨ�ú�����NVL(), NVL2() +���к���(�ۺϺ���)��MAX()���ֵ, MIN()��Сֵ, SUM()���, AVG()ƽ��ֵ, COUNT()ͳ�� + +--�ṹ����ѯ���Է�������ã� +SQL���ࣺ + DDL�����ݶ������ԣ����ڲ������ݶ��󣨱�����ͼ�����У��������� + CREATE������������������ + ALTER���޸ı�����������ṹ�� + DROP��ɾ�������������� + TRUNCATE��ɾ�������ݣ��������ṹ����ձ��IJ����� + DML�����ݲ������ԣ�ͨ����Ҫ����TCL����������ɾ�ı��е����ݡ� + INSERT INTO�������ݲ�����С� + DELETE��ɾ���������ݡ� + UPDATE SET�����±������ݡ� + TCL������������ԡ� + COMMIT���ύ�� + ROLLBACK���ع��� + DQL�����ݲ�ѯ���� + SELECT����ѯ���ݡ� + DCL�����ݿ������ԡ� + GRANT������Ȩ�ޡ� + grant��create��view��to��scott; + +--��������ԣ� +ԭ����(Atomicity): +�� ԭ������ָ������������в���Ҫôȫ���ɹ���Ҫôȫ��ʧ�ܻع����������IJ�������ɹ��ͱ���Ҫ��ȫӦ�õ����ݿ⣬�������ʧ�����ܶ����ݿ����κ�Ӱ�졣 +һ����(Consistency): + һ������ָһ������ִ��֮ǰ��ִ��֮�󶼱��봦��һ����״̬����ת����˵�������û�A���û�B���ߵ�Ǯ������һ����5000����ô����A��B֮�����ת�ˣ�ת�����ˣ���������������û���Ǯ�������Ӧ�û�����5000������������һ���ԡ� +������(Isolation): +�����������ǵ�����û������������ݿ�ʱ���������ͬһ�ű�ʱ�����ݿ�Ϊÿһ���û����������񣬲��ܱ���������IJ��������ţ������������֮��Ҫ�໥���롣 +�־���(Durability): +�����־�����ָһ������һ�����ύ�ˣ���ô�����ݿ��е����ݵĸı���������Եģ������������ݿ�ϵͳ�������ϵ������Ҳ���ᶪʧ�ύ����IJ����� + +--Oracle���ݿ��Դ��﷨��ͨ�ñ�׼��SQL99�﷨�� +Oracle���ݿ��Դ��﷨�� + (1)�ѿ������� + (2)�����ӣ�a.��ֵ���� + b.�ǵ�ֵ����:���Ա����š����������ʡ����ʵȼ���select e.empno, e.ename, e.sal, s.grade from emp e, salgrade s where e.sal between s.losal and s.hisal; + c.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno=d.deptno(+);--��ѯԱ���ı�ţ��������������ƣ������������κβ��ŵ�Ա���� + d.�������ӣ�select empno, ename, dname from emp e, dept d where e.deptno(+)=d.deptno;--��ѯԱ���ı�ţ��������������ƣ�����û��Ա���IJ��š� + e.�����ӣ� +ͨ�ñ�׼��SQL99�﷨�� + (1)�������ӣ��������Ӻ͵ѿ�����������ģʽ��ͬ�����DZ�1�е�ÿһ�ж����2�е������н���һ�����ӡ�select * from emp cross join dept; + (2)��Ȼ���ӣ�select * from emp natural join dept;--���ܹ�ƥ�䵽��ֵ���������Լ������ͬ���У�����ô���ǵ�ֵ���ӵĽ��.����dept���е�ֵ���ӡ� + select * from emp natural join salgrade;--������ƥ�䵽��ֵ��������ô������ǽ������ӡ� + (3)�������ӣ�left outer join + (4)�������ӣ�right outer join + (5)ȫ�����ӣ�full outer join + +--�����е�Լ���� +����Լ����primary key ����Ϊ�գ�����Ψһ�� +�ǿ�Լ����not null +ΨһԼ����unique +���Լ����check(����) ��MySql���ǿ���д�ģ���MySql��ֱ�Ӻ��Լ�顣price number(10,2) check(price>100),--���Լ�����ڴ�������ʱ��ֱ��д(��primary key, not null, uniqueһ��λ�û�֮��) +���Լ������Ҫ������Լ���ӱ�A�еļ�¼�����Ǵ���������B��(��֤����������)�� + (1)references �����(���������), --�ڴ�������ʱ��ֱ��д(��primary key, not null, unique, checkһ��λ�û�֮��) + (2)foreign key(�Լ�������) references �����(���������)--��һ�����Լ���ķ�ʽ��д�����һ�����ŵ�ǰ�� + (3)alter table books add foreign key(cid) references category(id);--���Բ��ڶ�����н������Լ���������ô����Ҳ��ʵ�����Լ���� + +--��һ��sql�����emp���й��ʴӴ�С����4��5��6λ���ʣ� +select * from (select rownum rn, e.* +from (select ename, sal from emp order by sal desc) e where rownum<=6)t +where t.rn >=4; \ No newline at end of file diff --git "a/JDBC/src/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\347\255\224\346\241\210\357\274\211.txt" "b/JDBC/src/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\347\255\224\346\241\210\357\274\211.txt" new file mode 100644 index 0000000..ca083df --- /dev/null +++ "b/JDBC/src/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\347\255\224\346\241\210\357\274\211.txt" @@ -0,0 +1,97 @@ +1.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门 +select dept.dname,emp.* from dept +left outer join emp +on dept.deptno=emp.deptno; + +2.列出在每个部门工作的员工数量、平均工资。 +select dept.deptno,count(emp.empno),avg(sal) +from dept +left outer join emp +on dept.deptno=emp.deptno +group by dept.deptno; + +3.列出所有员工的姓名、部门名称和工资。 +select emp.ename, dept.dname,emp.sal +from emp, dept +where emp.deptno=dept.deptno; + +4.查询每个部门的部门编号,部门名称,部门人数,最高工资,最低工资,工资总和,平均工资 +select d.deptno,d.dname,count(e.ename), +max(e.sal),min(e.sal),sum(sal),avg(sal) +from dept d join emp e on e.deptno=d.deptno +group by d.deptno,d.dname + +5.查询每个经理所管理的人数,经理编号,经理姓名,要求包括没有经理的人员信息。 +select count(e.empno),m.empno,m.ename +from emp e +left join emp m +on e.mgr = m.empno +group by m.empno,m.ename; + +6.查询部门人数大于2的部门编号,部门名称,部门人数 +select d.deptno,d.dname,count(e.ename) +from dept d +join emp e on d.deptno=e.deptno +group by d.dname,d.deptno +having(count(e.ename)>2); + +7.查询部门平均工资在2500元以上的部门名称及平均工资。 +select d.dname ,avg(e.sal) +from dept d +join emp e on d.deptno=e.deptno +group by d.dname +having(avg(e.sal)>2500); + +8.查询员工岗位中不是以"SA"开头并且平均工资在 2500元以上的岗位及平均工资,并按平均工资降序排序。 +select e.job,avg(e.sal) +from emp e +where e.job not like 'SA%' +group by e.job +having avg(e.sal)>2500 +order by avg(e.sal) desc + +9.显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于 +3000的,按最低工资由高到低排序。 +select m.empno,m.ename,min(e.sal) +from emp e +left join emp m +on e.mgr = m.empno +group by m.empno,m.ename +having min(e.sal)>=3000 +order by min(e.sal) desc; + +10.查询部门平均工资大于2000,且人数大于2的部门编号,部门名称,部门人数,部门平均工资,并按照部门人数升序排序。 +select d.deptno,d.dname,count(e.ename),avg(e.sal) +from dept d +join emp e on d.deptno=e.deptno +group by d.dname,d.deptno +having(count(e.ename)>2) and (avg(e.sal)>2000) +order by count(e.ename) ; + +11.查询部门最低工资 高于10号部门 最低工资的部门的编号、名称及部门最低工资。 +SELECT e.deptno,d.dname,MIN(e.sal) +FROM emp e,dept d +WHERE e.deptno=d.deptno +GROUP BY deptno,dname +HAVING MIN(e.sal)>(SELECT MIN(sal) from emp WHERE deptno=10) + +12.显示30号部门的雇员所从事的不重复的工作名称,并显示30部门的所在地。 +SELECT DISTINCT e.job,d.loc +FROM emp e,dept d +WHERE e.deptno=d.deptno AND e.deptno=30; + +13.查询工作在广州的员工人数,最高工资及最低工资。 +select count(*) 员工人数, +max(e.sal) 最高工资, +min(e.sal) 最低工资 +from emp e, dept d +where e.deptno = d.deptno +and d.loc = '广州'; + +14.统计各个部门不同职务的员工薪水的总和,平均工资,部门编号,部门名称,部门位置,职务 +SELECT dept.deptno,dname,loc,JOB,sum(sal),avg(sal) +FROM emp,dept +WHERE emp.deptno=dept.deptno +GROUP BY dept.deptno,dname,loc,JOB; + + diff --git "a/JDBC/src/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\350\207\252\345\267\261\357\274\211.sql" "b/JDBC/src/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\350\207\252\345\267\261\357\274\211.sql" new file mode 100644 index 0000000..2c215cd --- /dev/null +++ "b/JDBC/src/note/sql/\345\244\232\350\241\250\346\237\245\350\257\242\347\273\203\344\271\240\357\274\210\350\207\252\345\267\261\357\274\211.sql" @@ -0,0 +1,33 @@ +--1.�г��������ƺ���Щ���ŵ�Ա����Ϣ��ͬʱ�г���Щû��Ա���IJ��� +select dname, e.* from dept d left outer join emp e on d.deptno=e.deptno; +--2.�г���ÿ�����Ź�����Ա��������ƽ�����ʡ� +select count(ename), avg(sal) from emp e right outer join dept d on d.deptno=e.deptno group by d.deptno; +--3.�г�����Ա�����������������ƺ͹��ʡ� +select ename, dname, sal from emp e, dept d where e.deptno=d.deptno; +--4.��ѯÿ�����ŵIJ��ű�ţ��������ƣ�������������߹��ʣ���͹��ʣ������ܺͣ�ƽ������ +select d.deptno, dname, count(*), max(sal), min(sal), sum(sal), avg(sal) from emp e, dept d where d.deptno=e.deptno group by d.deptno, dname; +--5.��ѯÿ��������������������������ţ�����������Ҫ�����û�о�������Ա��Ϣ�� +select count(e.ename), m.empno,m.ename from emp e left join emp m on e.mgr=m.empno group by m.empno,m.ename +--6.��ѯ������������2�IJ��ű�ţ��������ƣ��������� +select d.deptno, d.dname, count(*) from emp e, dept d where e.deptno=d.deptno group by d.deptno, d.dname having count(*)>2; +--7.��ѯ����ƽ��������2500Ԫ���ϵIJ������Ƽ�ƽ�����ʡ� +select d.dname, avg(sal) from emp e, dept d where e.deptno=d.deptno group by d.dname having avg(sal)>2500; +--8.��ѯԱ����λ�в�����"SA"��ͷ����ƽ�������� 2500Ԫ���ϵĸ�λ��ƽ�����ʣ�����ƽ�����ʽ������� +select job, avg(sal) from emp where job not like 'SA%' group by job having avg(sal)>2500 order by avg(sal) desc +--9.��ʾ��������;����������������������Ա������͹��ʣ�û�о�����KINGҲҪ��ʾ�� +--��������͹���С��3000�ģ�����͹����ɸߵ������� +select m.empno, m.ename, min(e.sal) from emp e left outer join emp m +on e.mgr=m.empno group by m.empno, m.ename having min(e.sal)>=3000 order by min(e.sal) desc; +--10.��ѯ����ƽ�����ʴ���2000������������2�IJ��ű�ţ��������ƣ���������������ƽ�����ʣ������ղ��������������� +select d.deptno, d.dname, count(*), avg(sal) from emp e, dept d +where e.deptno=d.deptno group by d.deptno, d.dname +having count(*)>2 and avg(sal)>2000 order by count(*); +--11.��ѯ������͹��� ����10�Ų��� ��͹��ʵIJ��ŵı�š����Ƽ�������͹��ʡ� +select d.deptno, dname, min(sal) from emp e, dept d where d.deptno=e.deptno group by d.deptno, dname +having min(sal)>(select min(sal) from emp where deptno = 10); +--12.��ʾ30�Ų��ŵĹ�Ա�����µIJ��ظ��Ĺ������ƣ�����ʾ30���ŵ����ڵء� +select distinct job, d.loc from emp e, dept d where d.deptno=e.deptno and e.deptno=30; +--13.��ѯ������'CHICAGO'��Ա����������߹��ʼ���͹��ʡ� +select count(empno),max(sal),min(sal) from emp e, dept d where d.deptno=e.deptno and loc='CHICAGO'; +--14.ͳ�Ƹ������Ų�ְͬ���Ա��нˮ���ܺͣ�ƽ������,���ű�ţ���������,����λ�ã�ְ�� +select sum(sal),avg(sal),d.deptno,dname,loc, job from emp e, dept d where d.deptno=e.deptno group by d.deptno,dname,loc, job; diff --git a/JDBC/src/project/EmpManage_JDBC/bean/Dept.java b/JDBC/src/project/EmpManage_JDBC/bean/Dept.java new file mode 100644 index 0000000..2ec3353 --- /dev/null +++ b/JDBC/src/project/EmpManage_JDBC/bean/Dept.java @@ -0,0 +1,40 @@ +package project.EmpManage_JDBC.bean; + +public class Dept { + private int deptno;//部门编号 + private String dname;//部门名称 + private String loc;//部门地址 + + public Dept() {} + + public Dept(int deptno, String dname, String loc) { + this.deptno = deptno; + this.dname = dname; + this.loc = loc; + } + + public int getDeptno() { + return deptno; + } + public void setDeptno(int deptno) { + this.deptno = deptno; + } + public String getDname() { + return dname; + } + public void setDname(String dname) { + this.dname = dname; + } + public String getLoc() { + return loc; + } + public void setLoc(String loc) { + this.loc = loc; + } + + @Override + public String toString() { + return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; + } + +} diff --git a/JDBC/src/project/EmpManage_JDBC/bean/Emp.java b/JDBC/src/project/EmpManage_JDBC/bean/Emp.java new file mode 100644 index 0000000..4e3c941 --- /dev/null +++ b/JDBC/src/project/EmpManage_JDBC/bean/Emp.java @@ -0,0 +1,112 @@ +package project.EmpManage_JDBC.bean; + +import java.util.Date; + +/* + * ORM˼�룺�����ϵӳ�� + */ +public class Emp { + + private int empno;//Ա����� + private String ename;//Ա������ + private String job;//Ա��ְλ + private int mgr;//Ա���ľ������ + private Date hiredate;//Ա����ְʱ�� + private double sal;//Ա������ + private double comm;//Ա������ + private int deptno;//Ա�����ű�� + private Dept dept;//Ա���IJ��� + + public Dept getDept() { + return dept; + } + + public void setDept(Dept dept) { + this.dept = dept; + } + + public Emp() {} + + public Emp(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) { + this.empno = empno; + this.ename = ename; + this.job = job; + this.mgr = mgr; + this.hiredate = hiredate; + this.sal = sal; + this.comm = comm; + this.deptno = deptno; + } + + public int getEmpno() { + return empno; + } + + public void setEmpno(int empno) { + this.empno = empno; + } + + public String getEname() { + return ename; + } + + public void setEname(String ename) { + this.ename = ename; + } + + public String getJob() { + return job; + } + + public void setJob(String job) { + this.job = job; + } + + public int getMgr() { + return mgr; + } + + public void setMgr(int mgr) { + this.mgr = mgr; + } + + public Date getHiredate() { + return hiredate; + } + + public void setHiredate(Date hiredate) { + this.hiredate = hiredate; + } + + public double getSal() { + return sal; + } + + public void setSal(double sal) { + this.sal = sal; + } + + public double getComm() { + return comm; + } + + public void setComm(double comm) { + this.comm = comm; + } + + public int getDeptno() { + return deptno; + } + + public void setDeptno(int deptno) { + this.deptno = deptno; + } + + @Override + public String toString() { + return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate + + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + ", dept=" + dept + "]"; + } + + +} diff --git a/JDBC/src/project/EmpManage_JDBC/dao/EmpDao.java b/JDBC/src/project/EmpManage_JDBC/dao/EmpDao.java new file mode 100644 index 0000000..289b290 --- /dev/null +++ b/JDBC/src/project/EmpManage_JDBC/dao/EmpDao.java @@ -0,0 +1,29 @@ +package project.EmpManage_JDBC.dao; + +import java.util.*; + +import project.EmpManage_JDBC.bean.Emp; + +/* + * JavaEE����ṹ + * dao�㣺dao������ݿ���н��� + * service�㣺ҵ���߼����� + * web�㣺������չʾ + * Ա������ϵͳ���� + * 1.��ѯ����Ա�� + * 2.��ѯָ����ŵ�Ա�� + * 3.����Ա����Ϣ + * 4.ɾ��ָ����ŵ�Ա�� + */ +public interface EmpDao { + + public Emp findOneEmp(int empno);//��ѯָ����ŵ�Ա�� + public List findAllEmp();//��ѯ����Ա�� + public List selEmpInfo();//��ѯԱ����ţ����������ʣ����ű�ţ��������� + public List selEmpByPage(int page,int pageSize);//��ҳ��ѯ + public List fuzzyQuery(String str);//ģ����ѯ + public int saveEmp(Emp e);//����Ա����Ϣ + public int deleteEmp(int empno);//ɾ��ָ����ŵ�Ա�� + public int updateEmp(Emp emp);//�޸�Ա��ְλ�����ʡ����� + +} diff --git a/JDBC/src/project/EmpManage_JDBC/dao/impl/EmpDaoImpl.java b/JDBC/src/project/EmpManage_JDBC/dao/impl/EmpDaoImpl.java new file mode 100644 index 0000000..11d87ca --- /dev/null +++ b/JDBC/src/project/EmpManage_JDBC/dao/impl/EmpDaoImpl.java @@ -0,0 +1,229 @@ +package project.EmpManage_JDBC.dao.impl; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.experimental.theories.FromDataPoints; + +import project.EmpManage_JDBC.bean.Dept; +import project.EmpManage_JDBC.bean.Emp; +import project.EmpManage_JDBC.dao.EmpDao; +import project.EmpManage_JDBC.util.DBUtil; + +public class EmpDaoImpl implements EmpDao{ + + @Override + public Emp findOneEmp(int empno) {//��ѯָ��Ա�� + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + Emp emp = null; + try{ + //1.�������Ӷ��� + conn = DBUtil.getConnection(); + String sql = "select * from emp where empno=?"; + //2.����Ԥ������� + ps = conn.prepareStatement(sql); + //3.��ֵ + ps.setInt(1, empno); + //4.ִ��sql��� + rs = ps.executeQuery(); + if(rs.next()){//5.���������,����һ��,���Կ���дif + int eno = rs.getInt("empno"); + String ename = rs.getString("ename"); + String job = rs.getString("job"); + int mgr = rs.getInt("mgr"); + Date hiredate = rs.getDate("hiredate"); + double sal = rs.getDouble("sal"); + double comm = rs.getDouble("comm"); + int deptno = rs.getInt("deptno"); + emp = new Emp(eno, ename, job, mgr, hiredate, sal, comm, deptno); + } + }catch(Exception e){ + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps, rs); + } + return emp; + } + + @Override + public List findAllEmp() {//��ѯ����Ա�� + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List list = new ArrayList(); + try{ + conn = DBUtil.getConnection(); + String sql = "select * from emp"; + ps = conn.prepareStatement(sql); + rs = ps.executeQuery(); + while(rs.next()){ + int eno = rs.getInt("empno"); + String ename = rs.getString("ename"); + String job = rs.getString("job"); + int mgr = rs.getInt("mgr"); + Date hiredate = rs.getDate("hiredate"); + double sal = rs.getDouble("sal"); + double comm = rs.getDouble("comm"); + int deptno = rs.getInt("deptno"); + list.add(new Emp(eno, ename, job, mgr, hiredate, sal, comm, deptno)); + } + }catch(Exception e){ + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps, rs); + } + return list; + } + + @Override + public List selEmpInfo() {//��ѯԱ����ţ����������ʣ����ű�ţ��������� + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List list = new ArrayList(); + try { + conn = DBUtil.getConnection(); + String sql = "select ename,sal,e.deptno dno,dname " + + "from emp e,dept d where e.deptno=d.deptno"; + ps = conn.prepareStatement(sql); + rs = ps.executeQuery(); + while(rs.next()){ + String ename=rs.getString("ename"); + double sal = rs.getDouble("sal"); + int deptno = rs.getInt("dno"); + String dname = rs.getString("dname"); + Emp emp = new Emp(0, ename, null, 0, null, sal, 0, deptno); + Dept dept = new Dept(deptno, dname, null); + emp.setDept(dept); + list.add(emp); + } + } catch (Exception e) { + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps, rs); + } + return list; + } + + @Override + public List selEmpByPage(int page, int pageSize) {//��ҳ��ѯ + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List list = new ArrayList(); + try { + conn = DBUtil.getConnection(); + String sql = "select * from " + + "(select rownum rn, e.* from " + + "(select * from emp order by sal desc) e where rownum<=?)t " + + "where t.rn>=?"; + ps = conn.prepareStatement(sql); + ps.setInt(1, page*pageSize); + ps.setInt(2, (page-1)*pageSize+1); + rs = ps.executeQuery(); + while(rs.next()){ + int rownum = rs.getInt("rn"); + int eno = rs.getInt("empno"); + String ename = rs.getString("ename"); + String job = rs.getString("job"); + int mgr = rs.getInt("mgr"); + Date hiredate = rs.getDate("hiredate"); + double sal = rs.getDouble("sal"); + double comm = rs.getDouble("comm"); + int deptno = rs.getInt("deptno"); + list.add(new Emp(eno, ename, job, mgr, hiredate, sal, comm, deptno)); + } + } catch (Exception e) { + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps, rs); + } + + + return list; + } + + @Override + public List fuzzyQuery(String str) {//ģ����ѯ + Connection conn = null; + PreparedStatement ps = null; + ResultSet rs = null; + List list = new ArrayList(); + try { + conn = DBUtil.getConnection(); + String sql = "select * from emp where ename like ?"; + ps = conn.prepareStatement(sql); + ps.setString(1, "%"+str+"%"); + rs = ps.executeQuery(); + while(rs.next()){ + int eno = rs.getInt("empno"); + String ename = rs.getString("ename"); + String job = rs.getString("job"); + int mgr = rs.getInt("mgr"); + Date hiredate = rs.getDate("hiredate"); + double sal = rs.getDouble("sal"); + double comm = rs.getDouble("comm"); + int deptno = rs.getInt("deptno"); + list.add(new Emp(eno, ename, job, mgr, hiredate, sal, comm, deptno)); + } + } catch (Exception e) { + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps, rs); + } + return list; + } + + @Override + public int saveEmp(Emp emp) {//ʹ�ù�����DBUtil�е�executeUpdate���� + String sql = "insert into emp values(?,?,?,?,?,?,?,?)"; + //util.Date-->sql.Date + java.sql.Date date = new java.sql.Date(emp.getHiredate().getTime()); + Object[] obj = {emp.getEmpno(), + emp.getEname(), + emp.getJob(), + emp.getMgr(), + date, + emp.getSal(), + emp.getComm(), + emp.getDeptno()}; + return DBUtil.executeUpdate(sql, obj); + } + + @Override + public int deleteEmp(int empno) {//ʹ�ù�����DBUtil�е�executeUpdate���� + String sql = "delete from emp where empno=?"; + Object[] obj = {empno}; + return DBUtil.executeUpdate(sql, obj); + } + + @Override + public int updateEmp(Emp emp) {//����ʹ�õ��dz��淽�����и��£�Ҳ��ʹ�ù�����DBUtil�е�executeUpdate���� + Connection conn = null; + PreparedStatement ps = null; + int rows = 0; + try { + conn = DBUtil.getConnection(); + String sql = "update emp set job=?,sal=?,comm=? where empno=?"; + ps = conn.prepareStatement(sql); + ps.setString(1, emp.getJob()); + ps.setDouble(2, emp.getSal()); + ps.setDouble(3, emp.getComm()); + ps.setInt(4, emp.getEmpno()); + rows = ps.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps, null); + } + return rows; + } + + +} diff --git a/JDBC/src/project/EmpManage_JDBC/test/Test.java b/JDBC/src/project/EmpManage_JDBC/test/Test.java new file mode 100644 index 0000000..a5f09cd --- /dev/null +++ b/JDBC/src/project/EmpManage_JDBC/test/Test.java @@ -0,0 +1,179 @@ +package project.EmpManage_JDBC.test; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; + +import project.EmpManage_JDBC.bean.Emp; +import project.EmpManage_JDBC.dao.EmpDao; +import project.EmpManage_JDBC.dao.impl.EmpDaoImpl; + +public class Test { + + public static void main(String[] args) { + while(true){ + int choose = menu(); + switch (choose) { + case 1: + findOneEmp(); + break; + case 2: + findAllEmp(); + break; + case 3: + selEmpInfo(); + break; + case 4: + fuzzyQuery(); + break; + case 5: + selEmpByPage(); + break; + case 6: + saveEmp(); + break; + case 7: + deleteEmp(); + break; + case 8: + updateEmp(); + break; + case 9: + System.out.println("�˳�ϵͳ."); + return; + default: + System.out.println("û���ҵ���Ҫִ�еIJ���."); + break; + } + } + } + + public static int menu(){//�˵� + System.out.println("----------��ӭʹ��Ա������ϵͳ-------------"); + System.out.println("���ܲ˵��� "); + System.out.println("\t1.��ѯָ����ŵ�Ա����Ϣ"); + System.out.println("\t2.��ѯ����Ա����Ϣ"); + System.out.println("\t3.��ѯԱ����ţ����������ʣ����ű�ţ���������"); + System.out.println("\t4.��ҳ��ѯ����Ա����Ϣ"); + System.out.println("\t5.ģ����ѯ"); + System.out.println("\t6.����Ա����Ϣ"); + System.out.println("\t7.ɾ��Ա����Ϣ"); + System.out.println("\t8.�޸�Ա����Ϣ"); + System.out.println("\t9.�˳�ϵͳ"); + System.out.print("��ѡ����Ҫִ�еIJ�����"); + return new Scanner(System.in).nextInt(); + } + + public static void findOneEmp(){//��ѯָ����ŵ�Ա�� + //1.�ӿ���̨¼��Ҫ��ѯ��Ա����ţ� + Scanner sc = new Scanner(System.in); + System.out.print("������Ҫ��ѯ��Ա����ţ�"); + int empno = sc.nextInt(); + //2.����dao�㷽���� + EmpDao ed = new EmpDaoImpl(); + Emp emp = ed.findOneEmp(empno); + if(emp!=null){ + System.out.println(emp); + }else{ + System.out.println("��Ҫ��ѯ��Ա��������!"); + } + } + + public static void findAllEmp(){//��ѯ����Ա�� + List list = new EmpDaoImpl().findAllEmp(); + System.out.println("Ա����Ϣ���£�һ��("+list.size()+")��"); + if(!list.isEmpty()){ + for(Emp e: list){ + System.out.println(e); + } + }else{ + System.out.println("��"); + } + } + + public static void selEmpInfo(){//��ѯԱ����ţ����������ʣ����ű�ţ��������� + EmpDao ed = new EmpDaoImpl(); + for(Emp emp: ed.selEmpInfo()){ + System.out.println(emp); + } + } + + public static void selEmpByPage(){//��ҳ��ѯ + Scanner sc = new Scanner(System.in); + System.out.print("������ҳ����"); + int page = sc.nextInt(); + EmpDao ed = new EmpDaoImpl(); + List list = ed.selEmpByPage(page, 5); + for(Emp emp: list){ + System.out.println(emp); + } + } + + public static void fuzzyQuery(){//ģ����ѯ + System.out.print("������Ҫ��ѯ��Ա������(֧��ģ����ѯ)��"); + Scanner sc = new Scanner(System.in); + String str = sc.next(); + EmpDao ed = new EmpDaoImpl(); + List list = ed.fuzzyQuery(str); + for(Emp emp: list){ + System.out.println(emp); + } + } + + public static void saveEmp(){//����Ա����Ϣ + Emp emp = new Emp(); + + Scanner sc = new Scanner(System.in); + System.out.print("Ա����ţ� "); + emp.setEmpno(sc.nextInt()); + System.out.print("Ա�������� "); + emp.setEname(sc.next()); + System.out.print("Ա��ְλ�� "); + emp.setJob(sc.next()); + System.out.print("Ա���ľ�����ţ� "); + emp.setMgr(sc.nextInt()); + System.out.print("Ա����ְʱ�䣺 "); + try {//String--->util.Date + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + emp.setHiredate(sdf.parse(sc.next())); + } catch (ParseException e) { + e.printStackTrace(); + } + System.out.print("Ա�����ʣ� "); + emp.setSal(sc.nextDouble()); + System.out.print("Ա������ "); + emp.setComm(sc.nextDouble()); + System.out.print("Ա�����ű�ţ� "); + emp.setDeptno(sc.nextInt()); + + EmpDao ed = new EmpDaoImpl(); + int a = ed.saveEmp(emp); + System.out.println(a>0?"����ɹ�!":"����ʧ��!"); + } + + public static void deleteEmp(){//ɾ��ָ����ŵ�Ա�� + System.out.print("Ա����ţ� "); + int empno = new Scanner(System.in).nextInt(); + EmpDao ed = new EmpDaoImpl(); + int rows = ed.deleteEmp(empno); + System.out.println(rows>0?"ɾ���ɹ�!":"ɾ��ʧ��!"); + } + + public static void updateEmp(){//�޸�Ա��ְλ�����ʡ����� + EmpDao ed = new EmpDaoImpl(); + + Scanner sc = new Scanner(System.in); + System.out.print("Ա����ţ� "); + Emp emp = ed.findOneEmp(sc.nextInt()); + System.out.println(emp); + System.out.print("Ա��ְλ�� "); + emp.setJob(sc.next()); + System.out.print("Ա�����ʣ� "); + emp.setSal(sc.nextDouble()); + System.out.print("Ա������ "); + emp.setComm(sc.nextDouble()); + int a = ed.updateEmp(emp); + System.out.println(a>0?"���³ɹ�!":"����ʧ��!"); + } + +} diff --git a/JDBC/src/project/EmpManage_JDBC/util/DBUtil.java b/JDBC/src/project/EmpManage_JDBC/util/DBUtil.java new file mode 100644 index 0000000..9676aec --- /dev/null +++ b/JDBC/src/project/EmpManage_JDBC/util/DBUtil.java @@ -0,0 +1,66 @@ +package project.EmpManage_JDBC.util; + +import java.sql.*; + +/* + * ʵ��JDBC�Ĺ����� + * ���巽����ֱ�ӷ������ݿ�����Ӷ��� + * �ر�������Դ + */ +public class DBUtil { + //�������ݿ�ķ�װ���� + public static Connection getConnection(){ + Connection conn = null; + try { + //�������� + Class.forName("oracle.jdbc.driver.OracleDriver"); + String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String user = "zicheng"; + String password = "qqq"; + //��ȡ���Ӷ��� + conn = DriverManager.getConnection(url, user, password); + System.out.println("ʹ��DBUtil����Oracle�ɹ�!"); + } catch (Exception e) { + e.printStackTrace(); + } + return conn; + } + + //���ӡ�ɾ�����޸� + public static int executeUpdate(String sql,Object[] obj){ + Connection conn = null; + PreparedStatement ps = null; + int rows = 0; + try { + conn = DBUtil.getConnection(); + ps = conn.prepareStatement(sql); + for(int i=0;i0 && days<3){ + price = this.getPerRent()*days; + }else if(days>=3 && days<7){ + price = this.getPerRent()*days*0.9; + }else if(days>=7 && days<30){ + price = this.getPerRent()*days*0.8; + }else if(days>=30 && days<150){ + price = this.getPerRent()*days*0.7; + }else{ + price = this.getPerRent()*days*0.6; + } + return price; + } + + @Override + public String toString() { + return "Bus [seat=" + seat + ", toString()=" + super.toString() + "]"; + } + + +} diff --git a/JDBC/src/project/carRent_JDBC/bean/Car.java b/JDBC/src/project/carRent_JDBC/bean/Car.java new file mode 100644 index 0000000..b2997c5 --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/bean/Car.java @@ -0,0 +1,51 @@ +package project.carRent_JDBC.bean; + +public class Car extends Vehicle { + + //�γ��ͺ� + private String type; + + public Car() {} + + public Car(String brand, String vehicleId, int perRent,String type) { + super(brand, vehicleId, perRent); + this.type = type; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + @Override + public double calRent(int days) { + // TODO Auto-generated method stub + double price = 0; + if(days>0 && days<=7){ + price = this.getPerRent()*days; + }else if(days>7 && days<=30){ + price = this.getPerRent()*days*0.9; + }else if(days>30 && days<=150){ + price = this.getPerRent()*days*0.8; + }else{ + price = this.getPerRent()*days*0.7; + } + return price; + } + + @Override + public String toString() { + return "Car [type=" + type + ", toString()=" + super.toString() + "]"; + } + + + + + + + + +} diff --git a/JDBC/src/project/carRent_JDBC/bean/Truck.java b/JDBC/src/project/carRent_JDBC/bean/Truck.java new file mode 100644 index 0000000..730865a --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/bean/Truck.java @@ -0,0 +1,34 @@ +package project.carRent_JDBC.bean; + +public class Truck extends Vehicle { + + //��λ + private int tonnage; + + public Truck() {} + + public Truck(String brand, String vehicleId, int perRent,int tonnage) { + super(brand, vehicleId, perRent); + this.tonnage = tonnage; + } + + public int getTonnage() { + return tonnage; + } + + public void setTonnage(int tonnage) { + this.tonnage = tonnage; + } + + @Override + public double calRent(int days) { + // TODO Auto-generated method stub + return tonnage*days*50; + } + + @Override + public String toString() { + return "Truck [tonnage=" + tonnage + ", toString()=" + super.toString() + "]"; + } + +} diff --git a/JDBC/src/project/carRent_JDBC/bean/Vehicle.java b/JDBC/src/project/carRent_JDBC/bean/Vehicle.java new file mode 100644 index 0000000..5ee8c7d --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/bean/Vehicle.java @@ -0,0 +1,44 @@ +package project.carRent_JDBC.bean; + +public abstract class Vehicle { + //����Ʒ�� + private String brand; + //���ƺ� + private String vehicleId; + //����� + private int perRent; + + public Vehicle() {} + public Vehicle(String brand, String vehicleId, int perRent) { + super(); + this.brand = brand; + this.vehicleId = vehicleId; + this.perRent = perRent; + } + + public String getBrand() { + return brand; + } + public void setBrand(String brand) { + this.brand = brand; + } + public String getVehicleId() { + return vehicleId; + } + public void setVehicleId(String vehicleId) { + this.vehicleId = vehicleId; + } + public int getPerRent() { + return perRent; + } + public void setPerRent(int perRent) { + this.perRent = perRent; + } + + public abstract double calRent(int days); + @Override + public String toString() { + return "Vehicle [brand=" + brand + ", vehicleId=" + vehicleId + ", perRent=" + perRent + "]"; + } + +} diff --git a/JDBC/src/project/carRent_JDBC/dao/Operation.java b/JDBC/src/project/carRent_JDBC/dao/Operation.java new file mode 100644 index 0000000..444d94e --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/dao/Operation.java @@ -0,0 +1,8 @@ +package project.carRent_JDBC.dao; + +import project.carRent_JDBC.bean.Vehicle; + +public interface Operation { + public Vehicle lease(String brand,String type,int seat,int tonnage); + public void choose(); +} diff --git a/JDBC/src/project/carRent_JDBC/dao/impl/OperationImpl.java b/JDBC/src/project/carRent_JDBC/dao/impl/OperationImpl.java new file mode 100644 index 0000000..23a7cf7 --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/dao/impl/OperationImpl.java @@ -0,0 +1,155 @@ +package project.carRent_JDBC.dao.impl; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Scanner; + +import project.carRent_JDBC.bean.Bus; +import project.carRent_JDBC.bean.Car; +import project.carRent_JDBC.bean.Truck; +import project.carRent_JDBC.bean.Vehicle; +import project.carRent_JDBC.dao.Operation; +import project.carRent_JDBC.util.DBUtil; + +public class OperationImpl implements Operation{ + + + /** + * ѡ�����������ķ��� + */ + public void choose() { + Scanner sc = new Scanner(System.in); + Vehicle v = null; + System.out.println("*****��ӭ�����ڷ��������޹�˾*****"); + System.out.println("1���γ�\t2���ͳ�\t3������"); + System.out.print("��ѡ����Ҫ���޵��������ͣ�"); + int vType = sc.nextInt(); + String brand = ""; + String type = ""; + int seat = 0; + int tonnage = 0; + if(vType==1) { + System.out.println("��ѡ����Ҫ���޵Ľγ�Ʒ�ƣ�1����� 2������"); + int carBrand = sc.nextInt(); + if(carBrand==1) { + brand = "���"; + System.out.println("��ѡ����Ҫ���޵��������ͣ�1�������� 2��GL8"); + type = (sc.nextInt()==1)?"������":"GL8"; + }else if(carBrand==2) { + brand = "����"; + System.out.println("��ѡ����Ҫ���޵��������ͣ�1��X6 2��550i"); + type = (sc.nextInt()==1)?"X6":"550i"; + }else { + System.out.println("û���ҵ���Ҫ���޵Ľγ�Ʒ��"); + } + }else if(vType==2) { + type = ""; + System.out.println("��ѡ����Ҫ���޵�����Ʒ�ƣ�1������ 2����"); + brand = (sc.nextInt()==1)?"����":"��"; + System.out.println("��ѡ����Ҫ���޵�������λ����1��16�� 2��34��"); + seat = (sc.nextInt()==1)?16:34; + }else if(vType==3) { + type = ""; + System.out.println("��ѡ����Ҫ���޵�����Ʒ�ƣ�1��һ����� 2���������"); + brand = (sc.nextInt()==1)?"һ�����":"�������"; + System.out.println("��ѡ����Ҫ���޵�������λ��1��5�� 2��10��"); + tonnage = (sc.nextInt()==1)?5:10; + }else { + System.out.println("û���ҵ���Ҫ���޵���������"); + } + v = this.lease(brand, type, seat, tonnage); + System.out.print("��������Ҫ���޵�������"); + int days = sc.nextInt(); + double money = v.calRent(days); + System.out.println("��������ij��ƺ��ǣ�"+v.getVehicleId()); + System.out.println("����Ҫ֧��������ǣ�"+money+"Ԫ"); + System.out.println(v); + } + + /** + * ���������������õ�һ�����������Ķ��� + */ + public Vehicle lease(String brand,String type,int seat,int tonnage) { + Connection conn = null; + PreparedStatement ps1 = null; + PreparedStatement ps2 = null; + PreparedStatement ps3 = null; + ResultSet rs = null; + int rows1 = 0; + int rows2 = 0; + Vehicle v = null; + String sql1 = null; + String sql2 = null; + String sql3 = "insert into rented values (?,?,?,?,?,?)"; + if(type != null) { + sql1 = "select * from car where type = ?"; + sql2 = "delete from car where type = ?"; + }else if(seat != 0) { + sql1 = "select * from bus where seat = ?"; + sql2 = "delete from bus where seat = ?"; + }else if (tonnage != 0) { + sql1 = "select * from truck where tonnage = ?"; + sql2 = "delete from truck where tonnage = ?"; + } + try { + conn = DBUtil.getConnection(); + ps1 = conn.prepareStatement(sql1); + ps2 = conn.prepareStatement(sql2); + ps3 = conn.prepareStatement(sql3); + if(type != null) { + ps1.setString(1, type); + ps2.setString(1, type); + }else if(seat != 0) { + ps1.setInt(1, seat); + ps2.setInt(1, seat); + }else if (tonnage != 0) { + ps1.setInt(1, tonnage); + ps2.setInt(1, tonnage); + } + rs = ps1.executeQuery(); + + conn.setAutoCommit(false); + while (rs.next()) { + String vehicleId = rs.getString("vehicleId"); + int perRent = rs.getInt("perRent"); + + ps3.setString(1, brand); + ps3.setString(2, vehicleId); + ps3.setInt(3, perRent); + ps3.setString(4, type); + ps3.setInt(5, seat); + ps3.setInt(6, tonnage); + + if(type != null) { + v = new Car(brand, vehicleId, perRent, type); + ps2.executeUpdate(); + ps3.executeUpdate(); + }else if(seat != 0) { + v = new Bus(brand, vehicleId, perRent, seat); + ps2.executeUpdate(); + ps3.executeUpdate(); + }else if (tonnage != 0) { + v = new Truck(brand, vehicleId, perRent, tonnage); + ps2.executeUpdate(); + ps3.executeUpdate(); + } + } + conn.commit(); + + } catch (Exception e) { + try { + conn.rollback(); + } catch (SQLException e1) { + e1.printStackTrace(); + } + e.printStackTrace(); + }finally { + DBUtil.close(conn, ps1, ps2, ps3, rs); + } + return v; + } + +} \ No newline at end of file diff --git a/JDBC/src/project/carRent_JDBC/test/Test.java b/JDBC/src/project/carRent_JDBC/test/Test.java new file mode 100644 index 0000000..bb50281 --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/test/Test.java @@ -0,0 +1,14 @@ +package project.carRent_JDBC.test; + +import project.carRent_JDBC.dao.Operation; +import project.carRent_JDBC.dao.impl.OperationImpl; + +public class Test { + + public static void main(String[] args) { + // TODO Auto-generated method stub + Operation op = new OperationImpl(); + op.choose(); + } + +} diff --git a/JDBC/src/project/carRent_JDBC/util/DBUtil.java b/JDBC/src/project/carRent_JDBC/util/DBUtil.java new file mode 100644 index 0000000..5080b3c --- /dev/null +++ b/JDBC/src/project/carRent_JDBC/util/DBUtil.java @@ -0,0 +1,72 @@ +package project.carRent_JDBC.util; + +import java.sql.*; + +/* + * ʵ��JDBC�Ĺ����� + * ���巽����ֱ�ӷ������ݿ�����Ӷ��� + * �ر�������Դ + */ +public class DBUtil { + //�������ݿ�ķ�װ���� + public static Connection getConnection(){ + Connection conn = null; + try { + //�������� + Class.forName("oracle.jdbc.driver.OracleDriver"); + String url = "jdbc:oracle:thin:@localhost:1521:orcl"; + String user = "zicheng"; + String password = "qqq"; + //��ȡ���Ӷ��� + conn = DriverManager.getConnection(url, user, password); + System.out.println("ʹ��DBUtil����Oracle�ɹ�!"); + } catch (Exception e) { + e.printStackTrace(); + } + return conn; + } + + //���ӡ�ɾ�����޸� + public static int executeUpdate(String sql,Object[] obj){ + Connection conn = null; + PreparedStatement ps = null; + int rows = 0; + try { + conn = DBUtil.getConnection(); + ps = conn.prepareStatement(sql); + for(int i=0;i