MySQL基本指令汇总

 

创建数据库:

create database  数据库名字;

 

删除数据库:

drop database 数据库名字;

 

查看数据库:

show databases;

 

切换数据库:

use databasename;

 

select database();

 

Create table 表名(列名  数据类型 [约束],列名  数据类型 [约束],列名  数据类型 [约束]);

 

删除表:

drop table 表名;

 

查看表结构:

desc 表名;

 

查看建表的语句:

show create table表名;

insert  update  delete

show variables like 'char%';

show variables like 'collation%';

sudo vim /etc/mysql/my.cnf

添加以下内容

[mysqld]

collation-server = utf8_unicode_ci

init-connect='SET NAMES utf8'

character-set-server = utf8

重启:

service mysql restart

 

alter  修改表结构:

添加字段:

alter table 表名 add  [column]  字段名  数据类型   [约束];

 

删除字段:

alter table 表名 drop  [column]  字段名;

 

修改字段的名称和类型

alter table 表名 change  旧字段名   新字段名   数据类型  [约束];

 

修改字段的类型

alter table 表名  modify  字段名  新的数据类型  [约束];

 

 

约束:

主键约束: 主键  =  唯一  + not null

 

primary key

自增: auto_increment

create table food(id int primary key auto_increment,name varchar(20),price float)

 

非空约束:

not null

create table qq(id int primary key auto_increment,nick_name varchar(16) not null,password varchar(64) not null,qq_no varchar(10) not null);

 

默认值约束:

default

create table  book(id int primary key auto_increment,bname varchar(30) not null, publisher varchar(50) default '默认值');

 

insert into book(bname,publisher) values ('daomubijia',null)

 

select * from book;

 

insert into book(bname,publisher) values('daomubiji',default);

 

 

唯一约束:

unique   保证数据的唯一性

但是允许null

create table  user(id int primary key auto_increment,username varchar(16) unique,password varchar(64) not null,phone char(11) unique not null);

 

 

检查约束:mysql  ---- 不支持 check

create table user(id int primary key auto_increment,username varchar(16) unique,password varchar(64) not null,phone char(11) unique,gender  enum('',''));

 

 

外键约束:

foreign key 

ALTER TABLE score1 ADD CONSTRAINT fk_sid FOREIGN KEY(sid) REFERENCES stu(sid)

 

一张表可以有多个外键,只能有一个主键(idusername+phone

 

username   phone

admin           18900001111

admin           18900001112

 

 

 

添加数据:

insert into 表名(id,name,age)  values(1,值2,值3)

id

name

age

101

aa

19

 

 

 

修改数据:

update 表名  set  字段名=新值;   ---- 更新该字段下的所有的值

 

update 表名  set  字段名=新值 where 条件

 

案例:

 

update student set age =18;

 

update student set age=19 where id =3;

 

update student set age =20 where id =1 or  id=5;

 

update student set age =age+1  where  id>=2;

 

update student set age=age+1 where id in (2,4,5,8,10);

 

--  1-100   50~70

 

update student set age =age+1  where  id>=50 and id<=70;

 

update student set age =age+1  where id between 50 and 70;

 

 

update student set age=age+1,name='laowang' where id =4;

 

 

删除数据:

 

delete from 表名 where 条件】

 

delete from student where id=4;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网络毒刘

授人玫瑰,手有余香。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值