標籤:約束 efault orm 唯一性 esc ima 資料庫 upd 需要
mysql -u使用者名稱 -p***** // 進入資料庫
use 資料庫名;//選擇資料庫
show databases;//查看資料庫
drop database 資料庫名;//刪除資料庫
show create database 資料名;//查看建立資料庫的語句
create database 資料庫名 charset utf8;//建立資料庫
show tables;//查看所有表
create table 表名(
id int(10) auto_increment primary key,
name varchar(5) not null
);
表的約束條件:
1.自增長:aotu_increment
2.非空:not null
3.預設值:default‘xx(預設值)‘
4.唯一約束:unique
5.指定字元集:charset 如:utf-8
6.主鍵:primary key 具有唯一性,不可為空
7.外鍵::ALTER TABLE 用來表示兩個表之間的關係
drop table 表名;//刪除表
delete form 表名;//刪除整個表裡的資料truncate tablename,刪除整個表裡的資料#delete清空的表自增長id還會繼續增長
//truncate 清空的表自增長id從1開始,truncate速度比delete要快,因為truncate 是從磁碟上直接把資料刪除,恢複不了
delete from blk where stu_name = ‘條件‘;//刪除指定的資料
改表:
1.alter table 表明 rename 新表明;//改表明
2.alter table 表名 modify 如:id int(20)not null;//修改表的欄位
3.alter table 表名 change 欄位名 欄位類型varchar(30);#修改欄位的資料類型
4.alter table 表名 add 欄位 float not null after 新欄位;#新增一個欄位,放在哪個位置
5.alter table tablename drop 欄位名;//刪除欄位
6.update 表名 set 欄位=**; //如果不指定條件的話,修改的是整個表的資料
7.update 表名 set 欄位=** where 條件欄位=‘***‘ ; //修改指定的資料
8.update 表名 set 欄位=**,欄位 where 條件欄位=‘***‘ ; //修改多個欄位
9.UPDATE 表名 set 欄位=欄位+100 ;//在原來的值基礎上做修改(int型欄位)
備忘:modify和alter執行命令完成即生效(自動認可的),update語句要“commit”之後才會生效(需要手動提交)
desc 表明;//查看錶結構
show tables;//查看所有表
給表添加資料:
insert into 表名(指定添加的欄位如:id,name)
values(‘1’,‘名字’);
//同時插入多條可用:(‘1’,‘名字’),(‘2’,‘名字2’),(‘3’,‘名字3’);
//不指定添加的欄位:“要把所有的字典值都寫全”
mysql基礎篇--增刪改