天天看點

MySQL的基本正常指令+案例解說:alter、update、drop、delete、insert

正常指令:

show databases;      #檢視資料庫中都有哪些庫
create database mydb charset=utf8;   #建立mydb庫
use mydb;        #選擇mydb庫
create table students(
    id int primary key auto,
    name varchar(20) Not Noll,
    score float,
    birth date
);     #建立學生表  這裡注意:倒數第二行不可加逗号
show tables;     #檢視目前庫下的所有表
desc students;    #檢視學生表都有哪些屬性
select *  from students;    #檢視學生表都有哪些成員
select database();          #檢視目前所在的庫
select show table 表名;     # 檢視該表的屬性
drop table students;        #删除學生表,指沒有關聯的表
drop database mydb;         #删除mydb庫
delete from 表名;           #删除該表
delete from user where name='張三';    #删除user表中‘張三’這個字段

           

alter:針對表的字段

alter table 舊表名 rename to 新表名;    #修改表名
rename 舊表名 to 新表名;
eg:alter table scores rename grades;

alter table 表名 modify 字段名  資料類型;   #修改字段的資料類型
eg:alter table scores modify score int;

alter table 表名 change 舊字段  新字段  資料類型;   #修改字段名
eg:alter table scores change score grade int;

alter table 表名 add 新字段 資料類型;    #添加字段
eg:alter table scores add name varchar(10);

alter table 表名 drop 字段名;   #删除字段
eg:alter table scores drop address;

alter table 表名 drop foreign key 外鍵限制名;    #删除表的外鍵限制
eg:alter table scores drop foreign key name;

           

注意:删除關聯表時,先解除關聯,再進行删除。

insert插入:針對資料

insert into scores(name,school,grade,teacher) values(jack,beijing,75,wangqiang);   #單行插入
insert into scores(name,school,grade,teacher) values(jack,beijing,75,wangqiang),
                                                    (tom,xian,86,liuhan),
                                                    (alice,shanghai,69,noll);   #多行插入
           

update更新

update scores set name=tom;    #将scores表中name全部更新為tom】
update school set num=num+20 where address='China';  #給school表中位址為China的加20