MySQL—經常使用指令總結

一、數據庫操做

建立庫:   mysql> create database db_name;php

查詢庫:   mysql> show databases; //顯示全部的數據庫   mysql> show create databases db_name; //顯示特定的數據庫java

刪除庫:   mysql> drop database db_name;mysql

修改庫:   mysql> alter database db_name [modify command];   mysql> alter database db_name character set gbk;sql


二、表操做

建立表:   mysql> use db_name;   mysql> create table tbl_name (column_structure) [tbl_option];數據庫

如:spa

  mysql> create table tbl_name(
  variable_name1 varchar(10)
  variable_name2 int
    );

或:命令行

  mysql> create table db_name.tbl_name(
  variable_name1 varchar(10)
  variable_name2 int
  );`

查詢表查看有哪些表
  mysql> show tables; 或   mysql> show tables like 'pattern_%'; 查看錶的建立信息:   mysql> show create table tbl_name; 或   mysql> show create table tbl_name \G 查看錶的結構:   mysql> describe tbl_name; 或   mysql> desc tbl_name;rest

刪除表:   mysql> drop table [if exists] tbl_name;code

修改表修改表名:   單個:mysql> rename table old_tbl_name to new_tbl_name;   多個:mysql> rename table old_tbl_name1 to new_tbl_name1, old_tbl_name2 to   new_tbl_name2;   跨數據庫:mysql> rename table old_tbl_name to db_name.new_tbl_name; 修改列定義增長新列定義:add   alter table tbl_name add new_column_name data_type; 如:   alter table exam_student add height int; 修改列的定義(新的屬性或者數據類型):modify   mysql> alter table tbl_name modify column_name new_data_type; 刪除一個列:drop   mysql> alter table tbl_name drop column_name; 重命名一個列:change   mysql> alter table tbl_name change old_column_name new_column_name new_data_type; 修改表選項:   mysql> alter table tbl_name new_tbl_option;   mysql> alter table tbl_name character set utf8;rem

三、數據操做

建立數據:   mysql> insert into tbl_name (fields_list) values (values_list); 如:   mysql> insert into exam_student (stu_name, stu_no) values ('xiaoming', 'php030_01'); 若插入全部字段值:   insert into exam_student values ('xiaoming', 'php030_01', 98);

查詢數據查看字段信息:   mysql> select * from tbl_name; 參看具體字段列表信息
  mysql> select fields_list from tbl_name where condition; 如:默認狀態下   mysql> select stu_name, stu_no from exam_student; 或:   mysql> select stu_name, stu_no from exam_student where 1; 有條件查詢:   mysql> select * from tbl_name where condition; 如:   mysql> select * from exam_student where fenshu >= 60;

刪除數據:   mysql> delete from tbl_name condition; 如:   mysql> delete from exam_student where fenshu <= 50;

修改數據:   mysql> update tbl_name set field = new_value where condition; 如:   mysql> update exam_student set fenshu =100 where fenshu >= 97;

四、主鍵:primary

如:法一:

  create table teacher(
  t_id int primary key [auto_increment],
  t_name varchar(5),
  class_name varchar(6),
  t_days tinyint unsigned
  );

法二:

  mysql> create table teacher(
  t_id int,
  t_name varchar(5),
  class_name varchar(6),
  t_day tinyint unsigned,
  primary key (t_id)
  );
  
  mysql> insert into teacher values (1, '王老師', '0225', 23);

在已建立表中添加主鍵操做:   mysql> alter table tbl_name add primary key (field) ;   mysql> alter table tbl_name modify colunm_name data_type not null primary key auto_increment; **自動增加:**auto_increment   mysql> alter table tbl_name auto_increment initial_value; 在已建立表中的主鍵id上添加auto_increment   mysql> alter table tbl_name id_name id_name int auto_increment;

五、外鍵:foreign key

如: 主表:

  mysql> create table itcast_class(
  class_id int primary key auto_increment,
  class_name varchar(10) not null default 'itcast_php' comment '班級名稱'
  ) character set utf8;

從表:

  mysql> create table itcast_student(
  stu_id int primary key auto_increment,
  stu_name varchar(10) not null default '',
  class_id int,
  foreign key 
  (class_id) references
   itcast_class (class_id)
  ) character set utf8;

插入數據的順序:先主表,後從表   mysql> insert into itcast_class values (null, 'php0331');   mysql> insert into itcast_student values (null, '張三', 1); 刪除操做:   mysql> alter table itcast_student drop foreign key itcast_student_ibfk_1;   mysql> alter table itcast_student add foreign key (class_id) references itcast_class (class_id)   on delete set null; //主表裏刪除某個字段,從表若對應外鍵則變成null 或者:    on delete cascade;//主表刪除一個字段,從表對應的外鍵則會刪除相應的值 又或者:   on update restrict;//不容許主表進行更新操做   mysql> delete from itcast_class where class_id = 1;

六、最大值

  mysql> select max(field) from tbl_name;

七、鏈接

內鏈接

  mysql> select tbl1_name.field, tbl2_name.field1, tbl2_name.field2... from tbl1_name inner join tbl2_name on tbl1_name.id = tbl2_name.id where condition;

外鏈接

  select tbl1_name.fields, tbl2_name.fields from tbl1_name left outer join tbl2_name on tbl1_name.id = tbl2_name.id;

八、更改數據庫密碼方法:

  進入mysql中後,在命令行中mysql> 輸入:mysql> set password for [root@localhost](mailto:root@localhost) = password('123456');

相關文章
相關標籤/搜索