實體完整性、域完整性
方式一:
create table stu2(
id int primary key auto_increment,
name varchar(50),
);
方式二:同時建立多個主鍵
create table stu2(
id int,
name varchar(50),
primary key(id,name)
);
方式三:建立完表後單獨新增主鍵
create table stu2(
id int,
name varchar(50)
);
alter table stu3 add constraint stu3_id primary key(id);
create table stu4(
id int primary key,
name varchar(50) unique
);
create table stu2(
id int,
name varchar(50) not null
);
create table stu7(
id int primary key auto_increment,
name varchar(50) unique not null,
address varchar(50) default "beijing"
);
方式一:stu_sco_id是給約束起的名字,可以自定義
create table score1( score int, courseid int,stuid varchar(10), constraint stu_sco_id foreign key(stuid) references student(stuid) );
方式二:先建立表,再單獨新增外來鍵
alter table score2 add constraint stu_sco_id2 foreign key(stuid) references student(stuid);
注意:被合併的兩個結果,列數、列型別必須相同
union:
select * from A union select * from B;
union all:
select * from A union all select * from B;
on可以用where替代:(給別名)
select s.stuid,s.stuname,c.score,c.courseid from student s join score c on s.stuid=c.stuid;
左外連線:
select s.stuid,s.stuname,c.score,c.courseid from student s left join score c on s.stuid=c.stuid;
右外連線:
select s.stuid,s.stuname,c.score,c.courseid from student s right join score c on s.stuid=c.stuid;
1.查詢和scott在同一個部門的員工:
select * from emp where deptno=(select deptno from emp where enname='scott');
2.查詢工作型別和工資與martin完全相同的員工資訊:
select * from emp where (job,sal) in(select job,sal from emp where enname='martin');
子查詢涉及返回值爲多個時,返回的是一個元組。where後面的欄位也要元組形式。
注意:可以不需要登錄數據庫,備份路徑要加上備份生成的檔名
mysqldump -u root -p mydb1 > /home/rock/Desktop/mydb1.sql
注意:需要先登錄數據庫,然後進入指定的數據庫,執行sql指令碼。
create database test;
use test;
source /home/rock/Desktop/mydb1.sql;