1 DDL:数据库操作语言,

DDL:创建create、删除drop、修改alter

建表test,里面有test_id test_data 两个字段 1 create table test ( test_id int, test_date datetime );

建表hehe与user_inf完全一样
2 create table hehe
as
select * from user_inf;

修改表test加入hehe_id字段
3 alter table test
add hehe_id;

修改表test 加入类型为varchar(255)aaa和bbb两个字段,其中aaa默认为'xxx'
4 alter table test
add

aaa varchar(255) default 'xxx',
bbb varchar(255)
);


将hehe表里面的hehe_id 修改成varchar(255)类型
5 alter table hehe
modifty hehe_id varchar(255);

从hehe表中删除列hehe_id
6 alter table hehe
drop hehe_id;

将hehe表重命名为test
7 alter table hehe
rename to test;

删除数据表
8 drop table wawa;


数据库约束

对数据库的表等进行一个约束,MySql中主要是4种约束

1not null  2 unique  3 primary key  4 foreign key

非空约束
对表hehe中hehe_id建立非空约束
1 alter table hehe
modify hehe_id  varchar(255)not null;

唯一约束
建表hehe中hehe_id建立唯一约束
2 create table hehe
hehe_id varchar(255) unique;

3 主键约束
主键是什么:是数据表的唯一索引。主键约束可以由多个数据列组合而成
//建表test,test_id作为主键约束
1 create table test
{
test_id int primary key,
test_name varchar(255)
};

增加表级约束语法增加主键约束
//增加table_test中test_name和test_pass作为主键约束
2 alter table table_test
add primary key(test_name,test_pass);

4 外键约束
外键约束是什么:外键约束主要用于保证一个或两个数据表之间的参照完整性,举个例子:teatcher_table中有teacher_name,student_table中也有相同的teacher_name,两个表中的teacher_name是可以一一对应的,那么就是说这两个是有联系的。
语法格式:foreign key...references...
//为了保证从表参照的主表存在,通常先建主表
create table teacher_table3
(
teacher_name varchar(255),
teacher_pass varchar(255),
#建立主键约束
primary key(teacher_name,teacher_pass)
);


create table student_table3
{
student_id int auto_increment primary key,
student_name varchar(255),
foreign key(java_teacher_name,java_teacher_pass) references teacher_table3(teacher_name,teacher_pass)
);




索引:存放在模式(schema)中的一个数据库对象,作用:加速表的查询。创建索引:1自动:在建表的时候定义主键约束、唯一约束和外键约束时     手动:可以通过create index...语句创建索引。 删除索引:1自动:删除表的时候,该表上自动删除     2 手动:用户通过drop index...语句删除指定数据表上的指定索引

//下面的索引将会提高对employess表基于last_name字段的查询速度
create index emp_name_idx
on employees(last_name)

//对基于last_name和first_name两列的索引
create index emp_name_idx
on employees(last_name ,first_name)

 

视图:视图是一个或者多个数据表中数据的逻辑显示

// 创建view_test视图
create or replace view view_test
as
select teacher_name,teacher_pass from teacher_table;

 

DML:主要操作数据表里的数据,DML语句由insert  into、update和delete from三个命令组成

1 insert into语句用于指定数据表中插入记录
//向teacher_table2中插入teacher_name

insert into teacher_table2(teacher_name)
values('xyz');

2 update语句
update语句用于修改数据表的记录

//将teacher_table2表中所有记录teacher_name都改为孙悟空
undate teacher_table2
set teacher_name='孙悟空';

//也可以通过where条件来指定修改指定记录
update teacher_table2
set teacher_table2
where teacher_id > 2;

3 delete from语句

//删除表lin中lin_id大于0的
delete from lin
where lin_id > 0;

//delete from可以把一个表中所有数据都删除
delete from lin;



 

单表查询

select语句的功能就是查询,select语句从一个或多个数据表中选出特定行、特定列的交集。

//选出所有数据列
select * from lin;

//选出指定数据列
select lin_name for lin
where lin_id>0;

//
select lin_id+3 as my_id form lin where  lin_id >1;

  

 

JDBC编码数据库过程

package cn.itcast.mybatis.first;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;


public class JDBCTest {

	public static void main(String[] args) throws Exception {
		//1 加载数据驱动
           Class.forName("com.mysql.jdbc.driver");
		
        		try(
		//2 数据库连接
	      Connection connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lin","root" , "root");
	      
		
		//3 connection对象创建statement
	      Statement statement=connection.createStatement();
		
		// 4 使用statment执行sql语句     	   //5操作结果集
	      ResultSet result=statement.executeQuery("select * from lin"))
        		{
        		 while(result.next())
        		 {
        			 System.out.println(result.getInt(1));
        		 }
        		
        		
        		}
        			//6 回收资源
        			
        	
	}

}