筆記:
約束:
|-- 主鍵約束(primary key)
|-- 外鍵約束(foreign key)
|-- 非空約束(not null)
|-- 唯一約束(unique)
|-- 檢查約束(check)
|-- 默認值(default)
對表內容的操作:
在關系型資料庫中,主鍵一般交給資料庫自己維護(主鍵自增):
|-- MySQL auto_increment
增加(insert):
insert into t_name[(欄位1 …)] values(v1 …);
更新(update):
將表中原有資料修改為我們需要的資料
update t_name set 欄位1 = 新值, 欄位2 = 新值 … where 條件
update t_hero set age = 18, gender = “女” where id = 7;
洗掉(delete):
delete from tablename where 條件
delete from t_hero where id =11;
truncate 陳述句(慎用)
該陳述句也是用來洗掉表,是無法恢復的
select
select * from 表名;
select id, username, age, gender, tel from t_hero;
select username from t_hero;
表的修改:
對表結構的修改:alter table
|-- 增加新的欄位
ALTER TABLE 表名 ADD 列名 列型別 約束條件;
alter table t_hero add address varchar(255);
|-- 修改欄位的型別
ALTER TABLE 表名 MODIFY 列名 列型別;
alter table t_hero modify id bigint;
|-- 修改欄位名稱
ALTER TABLE 表名 CHANGE 舊列名 新列名 列型別;
alter table t_hero change id hero_id int;
|-- 修改表名稱
ALTER TABLE 表名 RENAME 新表名;
alter table t_hero rename hero;
– 第二種寫法
rename table hero to t_hero;
表的復制:
|-- create table 新表名 like 源表
create table xiyou like t_hero;
|-- create table 新表名 select * from 源表
create table sanguo select * from xiyou;
實操:
在t_hero上練習;

update:

delete:

|-- 增加新的欄位
ALTER TABLE 表名 ADD 列名 列型別 約束條件;


|-- 修改欄位的型別
ALTER TABLE 表名 MODIFY 列名 列型別;

|-- 修改欄位名稱
ALTER TABLE 表名 CHANGE 舊列名 新列名 列型別;

|-- 修改表名稱
ALTER TABLE 表名 RENAME 新表名;

– 第二種寫法
rename table hero to t_hero;

表的復制:


第二種方法:

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/156110.html
標籤:其他
上一篇:2020-10-03
