MySQL基本操作(表,字段)

MySQL 三種數據類型(數值,字符串,日期)

數值格式有哪
在這裏插入圖片描述
字符串格式有哪些
在這裏插入圖片描述
日期型
在這裏插入圖片描述

MySQL常用增刪改查命令

創建數據庫和表

創建數據庫

mysql> show databases; #查看所有數據庫
mysql> create database tomdb charset utf8;     #創建數據庫tomdb,使用utf-8做字符編碼
mysql> use tomdb       #選擇使用tomdb數據庫
mysql> drop database tomdb;  # 刪除數據庫
mysql> show create database tomdb;     # 查看創建數據庫的語法

創建表結構

mysql> show tables  # 查看所有表
mysql> desc student; # 查看錶字段
mysql> drop table student; # 清空表數據

創建表結構

create table student(
id int auto_increment,
name char(32) not null,
age int not null,
register_data date not null,
primary key (id));

mysql> create table student(               #在tomdb數據庫中創建表:student
    -> id int auto_increment,              #在表中創建第一個字段:“id”
    -> name char(32) not null,             #在表中創建第二個字段:“name”
    -> age int not null,                   #在表中創建第三個字段:“age”
    -> register_data date not null,        #在表中創建第四個字段:日期
    -> primary key (id));                  #將表的主鍵設置爲字段:“id
Query OK, 0 rows affected (0.10 sec)       #這裏是創建成功的提示

插入數據

mysql> desc student;    # 查看student表所有字段                                                  
mysql> insert into student(name,age,register_data) values("zhangsan",100,"2016-06-20");
mysql> select * from student;   #查看student表有哪些數據                                           

常用查詢語句

最基本查詢語句

mysql> select * from student limit 2;   #僅查看student表中前兩行數據
mysql> select * from student limit 5 offset 3;  #從第三行開始查詢,並且只顯示5行數據

where; like; order by;使用

mysql> select * from student where id >3 and age >103;
mysql> select * from student where register_data like "2016-06%";            #查詢所有在2016-06這一條新建的條目
mysql> select * from student order by id desc;    #按主鍵降續                                         
mysql> select * from student order by id asc;  #按主鍵升續排序(默認升續)                                              
mysql> select * from student where name like binary "%si" order by id desc;         #查找名字以“si”結尾的所有條目,並且按照id降續排列

GROUP BY 語句:指定以什麼分組(比如可以統計出有多少同名數據)

mysql> select name,count(*) from student group by name;
mysql> select coalesce(name,"Total age"),sum(age) from student group by name with rollup;

修改(update)

mysql> update student set name="lisi",age=22 where id=1; #將表中id=1的條目改成name=lisi,age=22
mysql> update student set name="lisi",age=22 where id>4; #上面僅僅修改一條,這裏修改id>4的所有

刪除(delete)

mysql> delete from student where name="zhangsan"; #刪除student表中所有name=“zhangsan”                               

刪除,添加或修改表字段

添加一個字段(add)

mysql> alter table student add sex enum("M","F");      #添加一個字段sex,只能用M,或者F
mysql> insert into student(name,age,register_data,sex) values("zhangsan",100,"2016-06-20","M");
mysql> ALTER TABLE user MODIFY COLUMN NAME VARCHAR(50) default null;          # 修改字段屬性
mysql> ALTER TABLE user CHANGE name new_name char(32);            # 修改字段名稱

刪除一個字段(drop)

mysql> alter table student drop age;                                                         #刪除student表的age字段

僅能修改一個字段的屬性(modify)

mysql> alter table student modify sex enum("F","M") not null;  #修改剛創建的student表屬性不能爲空         

把字段的名字也能改掉(change)

mysql> alter table student change sex gender char(32) not null default "x";   #將sex字段名由sex改成gender,設置不能爲空,默認值爲“x”

修改表名

mysql> alter table student rename to student_table;                            #將表名從student修改成student_table

刪除有依賴關係的表

導入sql文件前,取消外鍵檢查:set foreign_key_checks=0;
導入sql文件後,加入外鍵檢查:set foreign_key_checks=1;

django中,在已有表添加新字段和新外鍵關係手動解決migrate失敗問題

1. 添加普通字段
 #1、將notify_notifybytagrelation表添加一個新字段max_times,爲int類型,不爲空,默認值爲0
alter table notify_notifybytagrelation add column max_times int not null default 0;

創建外鍵關聯的表

create table notify_tagnotifygroup(
id int auto_increment,
name char(255) not null,
notify_interval int not null default 0,
max_times int not null default 0,
primary key (id));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章