python 表的操作(一)

2022-08-28 01:54:07 字數 2976 閱讀 5617

1. 建立表

建立表:

檢視表的結構:

2. 表的約束

1.unsigned

2.not null

3.default

4.unique

5.auto_increment

create table t5(

id int unique auto_increment,

username char(10),

password char(18)

)insert into t5(username,password) values('alex','alex3714') # id欄位設定為自增欄位,增加資料時不輸入id欄位的值,它會自動增加

6.primary key 主鍵

create table t6(

id int not null unique, # 你指定的第乙個非空且唯一的字段會被定義成主鍵

name char(12) not null unique

)create table t7(

id int primary key, # 主鍵

name char(12) not null unique

)

7.foreign key 外來鍵 涉及到兩張表

部門表 : pid postname post_comment post_phone

create table post(

pid int primary key,

postname char(10) not null unique,

comment varchar(255),

phone_num char(11)

)員工表

create table staff(

id int primary key auto_increment,

age int,

gender enum('male','female'),

salary float(8,2),

hire_date date,

post_id int,

foreign key(post_id) references post(pid)

)insert into post / staff values …………

update post set pid=2 where pid = 1;

delete from post where pid = 1;

級聯刪除和級聯更新:

create table staff2(

id int primary key auto_increment,

age int,

gender enum('male','female'),

salary float(8,2),

hire_date date,

post_id int,

foreign key(post_id) references post(pid) on update cascade on delete cascade

)

如果級聯刪除外表中關聯的資料後,讓本表中被關聯的外來鍵列資料仍然存在,需要將外來鍵列設定為空null :

create table staff2(

id int primary key auto_increment,

age int,

gender enum('male','female'),

salary float(8,2),

hire_date date,

post_id int,

foreign key(post_id) references post(pid) on update cascade on delete set null

)

on delete:

3. 修改表

1.什麼時候會用到修改表?(一般不會常見)

2.修改表語句

alter table 表名 add —— 新增字段

alter table 表名 drop —— 刪除字段

alter table 表名 modify —— 修改已經存在的字段 的型別 寬度 約束

id name age

alter table 表名 modify age int not null after id; # 將age的位置修改到id後面

alter table 表名 modify age int not null first; # 將age的位置放在第乙個

alter table 表名 change —— 修改已經存在的字段 的型別 寬度 約束 和 欄位名字

4. 表關係

兩張表中的資料之間的關係:

5. 表資料的操作

1.增加 insert

value單數 :一次性寫入一行資料

values複數 :一次性寫入多行資料

t1 :id,name,age

insert into t1 value (1,'alex',83)

insert into t1 values (1,'alex',83),(2,'wusir',74)

insert into t1(name,age) value ('alex',83)

insert into t1(name,age) values ('alex',83),('wusir',74)

資料寫入的角度:

第二個角度:

2.刪除 delete

delete from 表 where 條件;

3.更新 update

update 表 set 字段=新的值 where 條件;

4.查詢 select

表查詢分為:單錶查詢 、多表查詢

mysql操作表 MySQL表的操作(一)

在建立表之前,首先要指明表在哪個資料庫中建立,也就是要指明命令所要操作的資料庫 用use語句選擇資料庫,一般格式 use 資料庫名 建立表的語法格式如下 例如選擇在linda資料庫中建立乙個use1表 use linda create table use1 id int,name varchar 2...

Python 鍊錶的基本操作

一 鍊錶簡介 鍊錶是一種在儲存單元上非連續 非順序的儲存結構。資料元素的邏輯順序是通過鍊錶中的指標鏈結次序實現。鍊錶是由一系列的結點組成,結點可以在執行時動態生成。每個結點包含兩部分 資料域與指標域。資料域儲存資料元素,指標域儲存下一結點的指標。二 單鏈表1.定義節點 an linklist nod...

Python的物件操作 一

2.物件呼叫 例子 刪除檔案的乙個例子 2.2 import 引用要用到的模組 author bright import shutil import os 物件操作實戰 上演出多個檔案 class def init self,root self.root root print 根目錄 s self....