資料庫MySQL初步學習增刪改

2021-10-05 13:36:57 字數 2180 閱讀 9021

create

table

`c_student`

(`s_id`

int(10)

notnull

,`s_name`

varchar(10

)default

null

,`s_gender`

varchar(5

)default

null

,`c_id`

int(10)

default

null

,primary

key(

`s_id`),

key`c_id`

(`c_id`),

constraint

`c_student_ibfk_1`

foreign

key(

`c_id`

)references

`t_class`

(`c_id`))

;

如上**

constraintc_student_ibfk_1foreign key (c_id) referencest_class

這裡是新增外來鍵的示範,c_student變格的c_id這項,增加**t_classc_id作為

外來鍵。**事列如下:

constraint

`外鍵名`

foreign

key(本表字段)

references外部表名(外部表字段)

也有增加外來鍵的**:

alter

table 表名 add

constraint 外鍵名 foreign

key(本表字段)

references外部表名(外部表字段)

刪除外來鍵:

alter

table 表名 drop

foreign

key 外鍵名;

增加:

create

table

`c_student`

(`s_id`

int(10)

notnull

,`s_name`

varchar(10

)default

null

,`s_gender`

varchar(5

)default

null

,`c_id`

int(10)

default

null

)insert

into 表名 values

(s_id,s_name,s_gender,c_id)

//增加資料的語句的**

>(*

,*,*

,*);這裡為你要增加的資料。這些資料與之前你所建立的**結構對應。

資料型別也要一一相等。

insert

into c_course values

(a,b,c,d)

;意思即為:c_course的資料結構為:(s_id,s_name,s_gender,c_id)

,abcd一一對應資料結構裡的字段的增加值。

更改:

update 表名 set 字段=值

where 條件;

例:update c_student set s_name=

"**"

//where s_id=1;

範例更改的是:當s_id=

1的時候,更改**c_student的s_name欄位的值為"**"。

刪除:

delete

from 表名 where條件;

或truncate

table 表名;

注意:delete和truncate刪除是有區別的。

delete可以用where條件進行可選擇刪除,而truncate會把資料全部清空。

delete不會清除自增長的序號,但是truncate會。

truncate的刪除效率更高。

MySQL資料庫增刪改查

注意 mysql中不區分大小寫 一 新增 新增資料到一張表中 語法 inser into 表名 列名 values 值列表 insert into students id,name values 16408100126 zzh 注意 新增資料時如果不寫欄位名,將會預設向所有欄位中新增值,確保所有no...

mysql資料庫增刪改操作

insert into 表名 列名1,列名2,列名3 values 值1,值2,值3 insert into user user id,name,age values 1,nice 24 delete from 表名 where 條件 update 表名 set 列名 列的值,列名2 列2的值 wh...

Mysql資料庫增刪改查

1.建立使用 使用資料庫 use work test 建立資料庫 create database work test create database ifnot exists work test character set gbk 建立表 create table dept id int prima...