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)
?
一張表可以有多個外鍵,只能有一個主鍵(id,username+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;
總結
以上是生活随笔為你收集整理的MySQL基本指令汇总的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php的运行方式及vc6和vc9,ts和
- 下一篇: gmap mysql cachet_百度