主要總結mysql一些常用知識點
[常用命令]
1、查看數據庫
show database;
2、創建數據庫
create database database_name;
3、切換數據庫
use database_name;
4、查看某數據庫中所有的數據表
show table;
5、創建數據表
CREATE TABLE my_table ( name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE );
6、查看數據表結構
describe table_name; --縮寫: desc
7、查看數據表中的記錄
select * from table_name; -- 去重復 select distinct name from table_name
8、往數據表中添加數據記錄
INSERT INTO table_name VALUES('puffball','Diane','hanst','f','1999-03-23',NULL); -- 指定屬性 insert into user3 (name) value('asfjl');
9、刪除數據
delete from table_name where name='puffball';
10、修改數據
update table_name set name='wang' where owner='haha'
11、建表約束--主鍵
create table user( id int primary key, name varchar(20) ); -- 聯合主鍵 create table user2( id int, name varchar(20), password varchar(20), primary key(id,name) ); -- 后來添加主鍵 create table user4( id int, name varchar(20) ); alter table user4 add primary key(id); -- 刪除主鍵約束 alter table user4 drop primary key; -- 修改約束 alter table user4 modify id int primary key;
12、建表約束--自增
create table user3( id int primary key auto_increment, name varchar(20) );
12、建表約束--唯一:約束修飾的字段的值不可以重復
create table user5( id int, name varchar(20), unique(name) -- 可一起約束多個,不一起重復即可;unique(id,name) ); -- 再另一種寫法 create table user5( id int, name varchar(20) ); alter table user5 add unique(name); -- 刪除唯一約束 alter table user5 drop index name -- modify alter table user5 modify name varchar(20) unique;
13、非空約束:修飾的字段不能為NULL
create table user6( id int, name varchar(20) not null ); -- 反null? 異常 insert into user6 (name) value('jfsl');
14、默認約束
create table user7( id int, name varchar(20), age int default 10 ); insert into user7 (id,name) value(1,'slfj'); insert into user7 (id,name,age) values(1,'slsfj',5);
15、外鍵約束
create table classes( id int primary key, name varchar(20) ); create table students( id int primary key, class_id int, foreign key(class_id) references classes(id) );
[查詢]
1、多表查詢
-- 兩表查詢 select sname,cno, degree from student,score where student.sno = score.sno; -- 三表查詢 select sname, cname,degree from student,course,course,score where student.sno = score.sno and course.cno = score.cno;
2、分組查詢
-- 子查詢加分組求評均 select cno, avg(degree) from score where sno in (select sno from student where class='1233') group by cno; -- year函數與帶in關鍵字的子查詢 select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,117));
3、多層嵌套查詢
-- select tno from teacher where tname='zhang'; -- select cno from course where tno = (select tno from teacher where tname='zhang'); -- select * from score where cno=() -- select * from score where cno=(select cno from course where tno =(select tno from reacher where tname='zhang'));
4、union與not in
-- not in select prof from teacher where depart='電子工程系'; select * from teacher where depart='計算機系' and prof not in (select prof from teacher where depart='電子工程系'); select * from teacher where depart='電子工程系' and prof not in (select prof from teacher where depart='計算機系'); -- UNION 操作符用於合並兩個或多個 SELECT 語句的結果集 select * from teacher where depart='計算機系' and prof not in (select prof from teacher where depart='電子工程系') union select * from teacher where depart='電子工程系' and prof not in (select prof from teacher where depart='計算機系');
5、any與all
-- any表示至少一個 select * from score where cno='34' and degree>any(select degree from score where cno='334') order by degree desc; -- all表示所有 select * from score where cno='34' and degree>all(select degree from score where cno='334') order by degree desc;
