第一天感觉做的还行,看了些MySQL数据库方面的知识,明天继续,估计可以把数据库这一章的内容看完,不知道自己的收获是啥,就是很认真的敲了SQL语句的代码,做了一些简单的笔记。有些SQL语句是直接执行的没有贴上去~~初学数据库的一些简单的知识点:
初学数据库的一些简单的知识点:
SQL语句以“;” 结束,SQL语句的关键字不区分大小写; SQL标识符通常必须以字母开头,可以包括字母、数字和三个特殊字符(# _ $); 修改表使用alter table,修改表包括增加列定义、修改列定义、删除列、重命名列等操作。 SQL语句中的字符串值不是用双引号引起,而是用单引号引起的。 主键约束相当于非空约束和唯一约束,即主键上约束的列既不允许出现重复值,也不允许出现null值;如果对多列组合建立主键约束,则多列里包含的每一列都不能为空,但只要求这些列组合不能重复 对于一对多的关联关系,通常在多的一端增加外键列;对于一对一的关联关系可以选择任意方式来增加一个外键列,增加外键列的表被称为从表,只要将外键列增加唯一约束就可以表示一对一关联了;对于多对多的关系,则需要额外增加一个连接表来记录他们的关联关系. 果想定义当删除主表记录时,从表记录也会随之删除,则需要在建立外键约束后添加on delete cascade 或添加on delete set null;第一种是删除主表记录时,把参照主表记录的从表记录全部级联删除;第二种是指定当删除主表记录时,把参照主表记录的从表记录的外键设为null 创建索引唯一的作用就是加速对表的查询,索引通过使用快速路径访问方法来快速定位数据,从而减少了磁盘的I/O。不能独立存在,必须从属于某个表,但是在数据字典中独立存放。 大部分数据库都采用with check option来强制不允许修改视图数据,但Oracle采用with read only来强制不允许修改视图数据。 可以为列起别名;旧名和新名之间用空格隔开,或者用as关键字 SQL语句可以使用两个通配符下划线“_”和百分号“%”,下划线表示一个任意的字符,百分号可以代表任意多个字符。
---创建表格 create table test ( test_id int, test_price decimal, test_name varchar(255) default 'xxx' , test_desc text , test_date datetime ) ;
//显示表的所有记录
select * from test ;
//修改表结构
alter table test add ( aaa varchar(255) default 'xxx' , bbb varchar(255) ); alter table test modify test_id varchar(255); alter table test modify bbb int ; alter table test drop aaa ; ---修改表名字 alter table test rename to wawa ; --创建表定义约束 create table hehe ( hehe_id int not null , hehe_name varchar(255) default 'xyz' not null , hehe_gender varchar(2) null ); alter table hehe modify hehe_gender varchar(2) not null; alter table hehe modify hehe_name varchar(2) null; alter table hehe modify hehe_name varchar(255) default 'abc' null ; //创建列级的唯一约束 create table unique_test ( test_id int not null , test_naem varchar(255) unique ); //创建表时创建唯一约束,使用表级约束语法建立约束。两个唯一约束,必须都不为空 create table unique_test2( test_id int not null , test_name varchar(255), test_pass varchar(255), unique (test_name), constraint test2_uk unique(test_pass) ); //创建表时创建唯一约束,使用表级约束语法建约束。这两个约束组成一组,只要求不能同时为空 create table unique_test3( test_id int not null , test_name varchar(255), test_pass varchar(255), constraint test3_uk unique(test_name, test_pass) ); //修改表结构时,使用add关键字来增加唯一约束 alter table unique_test3 add unique(test_name, test_pass); //修改表时使用modify关键字、为单列采用列级约束语法来增加唯一约束 alter table hehe modify test_name varchar(255) unique; //drop constraint 方法来删除约束 //删除unique_test3表上的test3_uk 唯一约束 alter table unique_test3 drop index test3_uk ; //建表时创建主键约束,使用列级约束语法建约束 create table primary_test( test_id int primary key , test_name varchar(255) ); //建表时创建主键约束,使用的是表级约束 create table primary_test2( test_id int not null , test_name varchar(255), test_pass varchar(255), ---指定主键约束名为test2_pk ,对大部分数据库有效,但对MySql无效,MySql数据中该主键约束名依然是PRIMARY constraint test2_pk primary key(test_id) ); //建表时创建主键约束,以多列建立组合主题,只能用于表级约束语法: create table primary_test3 ( test_name varchar(255), test_pass varchar(255), --建立多列组合的主键约束 primary key (test_name, test_pass) ); //删除指定表的主键约束,可以在alter table语句后使用drop primary key字句即可 alter table primary_test3 drop primary key; //通过modify修改列定义来增加主键约束,或者通过add添加来增加主键约束 alter table primary_test3 add primary key(test_name, test_pass) ; alter table primary_test3 modify test_name varchar(255) primary key ; //设置自增长 create table primary_test4( test_id int auto_increment primary key, test_name varchar(255), test_pass varchar(255) ); //使用列级约束语法建立外键约束直接使用references关键字,references指定该列参照的哪个主表,以及参照主表的哪一列 create table teacher_table1 ( teacher_id int auto_increment, teacher_name varchar(255), primary key (teacher_id) ); create table student_table1( student_id int auto_increment primary key, student_name varchar(255), java_teacher int references teacher_table1(teacher_id) ); //值得指出的是,虽然MySql支持使用列级约束的语法来建立外键约束,但这种列级约束语法建立的外键约束不会生效 //MySql提供这种列级约束语法仅仅是和标准SQL保持良好的兼容性.因此,如果需要MySql中的外键约束生效,应该使用标记约束语法 create table teacher_table( teacher_id int auto_increment, teacher_name varchar(255), primary key(teacher_id) ); //指定java_teacher参照到teacher_table 的teacher_id 列 create table student_table( student_id int auto_increment primary key , student_name varchar(255), java_teacher int , foreign key(java_teacher) references teacher_table(teacher_id) ); create table teacher_table2( teacher_id int auto_increment, teacher_name varchar(255), primary key(teacher_id) ); create table student_table2( student_id int auto_increment primary key , student_name varchar(255), java_teacher int , constraint student_teacher_fk foreign key(java_teacher) references teacher_table2(teacher_id) ); 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), java_teacher_name varchar(255), java_teacher_pass varchar(255), foreign key(java_teacher_name, java_teacher_pass) references teacher_table3(teacher_name, teacher_pass) ); //删除外键约束 alter table student_table3 drop foreign key student_table3_ibfk_1; //增加外键约束通常使用add constraint命令来增加外键约束 alter table student_table3 add foreign key (java_teacher_name, java_teacher_pass) references teacher_table3(teacher_name, teacher_pass) ; //表级约束法建外键,且直接参照自身 create table foreign_test( foreign_id int auto_increment primary key, foreign_name varchar(255), refer_id int, foreign key(foreign_id) references foreign_test(foreign_id) ); //如果想定义当删除主表记录时,从表记录也会随之删除,则需要在建立外键约束后添加on delete cascade 或添加on delete set null //第一种是删除主表记录时,把参照主表记录的从表记录全部级联删除;第二种是指定当删除主表记录时,把参照主表记录的从表记录的外键设为null create table teacher_table4( teacher_id int auto_increment primary key, teacher_name varchar(255) ); create table student_table4( student_id int auto_increment primary key , student_name varchar(255), java_teacher int , foreign key(java_teacher) references teacher_table4(teacher_id) on delete cascade ); CHECK约束,虽然有但是不起作用 create table check_test( emp_id int auto_increment , emp_name varchar(255), emp_salary decimal, primary key(emp_id), check (emp_salary>0) ); // insert into teacher_table2(teacher_name) values ('xyz'); insert into teacher_table2 values (null, 'abc'); //从属表插入数据 insert into student_table2 values (null, '张三', 2); insert into student_table2 values (null, "Yeeku",1),(null, "Sharfly",3); update语句 update teacher_table2 set teacher_name = '孙悟空'; update teacher_table2 set teacher_name = '猪八戒' where teacher_id > 1; delete from student_table2 where teacher_id > 2; select * from student_table2 where 2 between java_teacher and student_id;//选择出java_teacher 小于等于2,student_id 大于等于2的所 有记录 //默认情况下,分组函数会把所有记录当成一组,为了对记录进行显式分组,可以在select语句后使用group by字句,group by 子句后通常跟一个或多个列名,表明查询结果根据一列或多列进行分组,当一列或多列组合完全相同时,系统把这些记录当成一组。 select count(*) from student_table2 group by java_teacher ;