標籤:upd 查看 username 記錄 images 從表 技術 delete password
一,資料庫操作
主鍵
主鍵是索引的一種,並且是唯一性索引,且必須定義為primary key。一個表只有一個主鍵,主鍵可以由多列組成。
聲明主鍵的關鍵字為:primary key
簡單的一個聲明主鍵的樣本:
create table score(sid int auto_increment primary key)engine=innodb default charset=utf8 primary key為聲明的關鍵字。
外鍵
如果一個實體的某個欄位指向另一個實體的主鍵,就稱為外鍵。
被指向的實體,稱之為主實體(主表),也叫父實體(父表)。
負責指向的實體,稱之為從實體(從表),也叫子實體(子表)。
作用:用於約束處於關係內的實體。增加子表記錄的時候,是否有與之對應的父表記錄,如果主表沒有相關的記錄,從表不能插入。
外鍵範例程式碼:
#建立被關聯的子表create table class(cid int auto_increment primary key,clname char(20))engine=innodb default charset=utf8;#插入資料insert into class(clname) values("三年級"),("一年級"),("四年級"),("二年級");#建立主表(父表)create table student(sid int auto_increment primary key,sname char(12), class_id int,constraint fk_id_class foreign key(class_id) references class(cid))engine=innodb default charset=utf8;#constraint fk_id_class foreign key(class_id) references class(cid)關鍵詞,foreign key後跟父表列名,reference後加子表列名。insert into student(sname,class_id) values("stu1"),("stu2"),("stu3");
外鍵變種:
外鍵與主鍵都有唯一索引,且都不能重複。區別就是主鍵不可為空,外鍵的唯一索引可以為空白。
外鍵變種分為以下幾種關係:
一對多,一對一,多對多
1,一對一關聯性:
create table userinfo1( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table admin( id int not null auto_increment primary key, username varchar(64) not null, password VARCHAR(64) not null, user_id int not null, unique uq_u1 (user_id),#關鍵字,指定外鍵的一對一關聯性 CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id) )engine=innodb default charset=utf8;
2,一對多關聯性
3,多對多關係
create table userinfo2( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table host( id int auto_increment primary key, hostname char(64) )engine=innodb default charset=utf8; create table user2host( id int auto_increment primary key, userid int not null, hostid int not null, unique uq_user_host (userid,hostid),#關鍵字,指定為多對多的關係語句 CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id), CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id) )engine=innodb default charset=utf8;
資料表的操作:
插入,修改,選擇,刪除資料:
#一次插入多條資料的方式:insert into student(sname,class_id) values("stu1"),("stu2"),("stu3");#修改表內資料update student set sname="stu10"#操作顯示表資料select sid,sname from student;#顯示學生表的id與學生名字select sid,sname from student where sid>3;顯示表中sid大於3,的學生id與姓名#刪除表資料delete from student where id=1;#刪除表中id為1的資料
where條件陳述式
where:條件限制語句,在資料操作中需要對資料進行按條件式篩選,就需要用到where語句。在使用時徐注意:
where函數條件後不允許加彙總函式條件
文法: where 條件運算式
條件運算式的運算子
模糊比對的關鍵字 like
模糊比對補充 _底線 指的是該字元之後的一個任意數值,樣本:where username like"a_"
自增資料操作
在資料表的會經常設定自增資料,自增資料是可以對其進行修改查看等操作的。由於mysql的步長是基於會話操作的,每次修改針對的是每一個會話而不是全域,如果需要對全域進行修改要是global
查看:
show session variables like ‘auto_inc%‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| auto_increment_increment | 1 || auto_increment_offset | 1 |+--------------------------+-------+2 rows in set, 1 warning (0.00 sec)
修改自增步長:
set session auto_increment_increment=2;Query OK, 0 rows affected (0.00 sec) show session variables like ‘auto_inc%‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| auto_increment_increment | 2 || auto_increment_offset | 1 |+--------------------------+-------+2 rows in set, 1 warning (0.00 sec)
全域查看:
全域查看是基於關鍵字global。
設定全域步長值:set global auto_increment_increment=2;
show global variables like ‘auto_inc%‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| auto_increment_increment | 1 || auto_increment_offset | 1 |+--------------------------+-------+2 rows in set, 1 warning (0.00 sec)
MySQL基礎學習二