初學數據庫的一些簡單的知識點:
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 ;