MySQL ch7查詢資料1452錯誤

2021-10-02 17:24:32 字數 3820 閱讀 8426

a表中有a_id不屬於b表的b_id,需要保證表a中的a_id全部都屬於b表的b_id。

我這裡是因為沒有向表b中新增資料就在a表中新增外來鍵了。

use db1;

#建立表

create

table employee(

noint(11

)primary

keynot

null

unique

, name varchar(50

)not

null

, gender char(2

),dept int(11

)not

null

, job varchar(50

)not

null

, salary int(11

)not

null

, hiredate date

notnull);

#插入資料

insert

into employee (

no,name,gender,dept,job,salary,hiredate)

value

(1001

,'smith'

,'m',20

,'clerk'

,800

,'2005-11-12'),

(1002

,'allen'

,'f',30

,'salesman'

,1600

,'2003-05-12'),

(1003

,'ward'

,'f',30

,'salesman'

,1250

,'2003-05-12'),

(1004

,'jones'

,'m',20

,'manager'

,2975

,'1998-05-18'),

(1005

,'martin'

,'m',30

,'salesman'

,1250

,'2001-06-12'),

(1006

,'black'

,'f',30

,'manager'

,2850

,'1997-02-15'),

(1007

,'clark'

,'m',10

,'manager'

,2450

,'2002-09-12'),

(1008

,'scott'

,'m',20

,'analyst'

,3000

,'2003-05-12'),

(1009

,'king'

,'f',10

,'president'

,5000

,'1995-01-01'),

(1010

,'turner'

,'f',30

,'salesman'

,1500

,'1997-10-12'),

(1011

,'adams'

,'m',20

,'clerk'

,1100

,'1999-10-05'),

(1012

,'james'

,'f',30

,'clerk'

,950

,'2008-06-15');

#檢視表中所有資料

select

*from employee;

#計算表中gender='f'的dept列的資料總和

select

sum(dept)

as dept_sum from employee where gender=

'f';

#檢視表的3行到6行

select

*from employee limit2,

4;#檢視表中salary最小的值

select

min(salary)

as salary_min from employee;

#檢視表中name欄位以n或s結尾的行的資訊

select

*from employee where name like

'%n'

or name like

'%s'

;#建立表dept

create

table dept(

d_no int(11

)primary

keynot

null

unique

auto_increment

, d_name varchar(50

)not

null

, d_location varchar

(100))

;#插入值

insert

into dept (d_no,d_name,d_location)

value(10

,'accounting'

,'shanghai'),

(20,'research'

,'beijing'),

(30,'sales'

,'shenzhen'),

(40,'operation'

,'fujian');

#向employee表中新增外來鍵

alter

table employee add

constraint dno_fk foreign

key(dept)

references dept(d_no)

;#聯合外表查詢

select name,job from employee where dept=

(select d_no from dept where d_location=

'beijing');

#左連線

select

*from employee left

outer

join dept on employee.

no=dept.d_no;

#查詢入職日期大於2001小於2023年的員工資訊(兩種方式)

select

*from employee where hiredate>=

'2001-01-01'

and hiredate<=

'2005-12-31'

;select

*from employee where

year

(hiredate)

>=

2001

andyear

(hiredate)

<=

2005

;#使用union合併查詢結果

select

*from employee where dept=

20union

select

*from employee where dept=30;

#使用like查詢表中name欄位含a字母的資訊

select

*from employee where name like

'%a%'

;#使用regexp查詢表中name欄位含 t 或 c 或 m 的資訊

select

*from employee where name regexp

't'or

'c'or

'm';

資料結構7 查詢

2樹形結構的查詢 3雜湊結構的查詢 4字串模式匹配 include include define number 10 1 define min 1 31 typedef struct sstable int search seq1 sstable st,int key int main return...

Leveldb原始碼分析7 資料查詢

leveldb只是單純的在檔案末尾增加,並不改寫原有的內容,那麼如果刪除乙個key,或者更新乙個key應該怎麼辦呢?比如 table liming 18del table liming table liming 20table liming 21 leveldb將每乙個操作變化成如下的格式 user...

drupal7 資料庫操作,查詢

database api 測試檔案 define drupal root getcwd require once drupal root includes bootstrap.inc drupal bootstrap drupal bootstrap database 單值 nbr records ...