mysql语法(基础语法)

小编发现这东西长时间不用就全部忘记了,所以还是经常翻开看或者写博客总结养成这个习惯是有必要的!

创建及数据库:create database 数据库名;

查看数据库:show databases;

选择数据库:use database;

删除数据库:drop database 数据库名;

在dos窗口中查询数据库中的数据的常用命令步骤:

a:show databases;

b:use test;(test库名)

c:show tables;

d:desc student;(student为表名)

e:进行增删改查操作

例如查询:

select * from student;

 ---------------------------------------------------------------------

创建表:

create table [if not exists] 表名(

字段1 数据类型 [字段属性|约束] [索引] [注释]

字段2 数据类型 [字段属性|约束] [索引] [注释]

.......................

) [表类型] [表字符集] [注释]

1:单字段主键

create table [if not exists] 表名 (

字段1 数据类型 primary key;

............................

);

例如:

create table student(

student int(4) primary key,

.........................

);

2:多字段联合主键

create table [if not exists] 表名(

primary key [字段1,字段2...........]

);

例如:

create table tb_temp(

'id' int(4),

'name' varchar(11),

.....................

primary key('id','name')

);

查看表:show table;

删除表:drop table [if not exists] 表名;

例如:drop table student;

---------------------------------修改表----------------------------------

修改表名:

alter table 旧表名rename 新表名

例如:

ALTER TABLE easybuy_comment RENAME TO easybuy_com

添加字段:

alter table 表名 add 字段名 数据类型 [属性]

例如:ALTER TABLE easybuy_comment  ADD easybuy_num INT(10) NOT NULL

修改字段:

alter table 表名 change 原字段名 新字段名 数据类型 [属性]

ALTER TABLE easybuy_comment CHANGE easybuy_num easybuy_sss INT(10) NOT NULL

删除字段:

alter table 表名 drop 字段名

alter table easybuy_comment drop easybuy_sss

添加主键约束:

alter table 表名 add constraint 主键名 primary key 表名 (主键字段)

添加外键约束:

alter table 表名 add constraint 外键名 foreign key (外键字段) references 关联表名 (关联字段)

------------------------------使用DML插入数据---------------------------

1:插入单行数据

insert into 表名(字段名列表) values (列表值)

例如:

insert into student (id,pass,usename) values (1,‘aa’,‘哈哈1’)

2:插入多行数据

insert into 新表 (字段列表) values (列表值1),(列表值2),(列表值3),.....(列表值n)

例如:

insert into student (id,pass,usename)values(1,‘aa’,‘哈哈1’),(2,‘bb’,‘哈哈2’),(3,‘cc’,‘哈哈3’)

3:将查询结果插入到新表

create table 新表(select studentName,phone, from student)

-----------------------------使用DML更新数据-----------------------------------------------

update 表名 set 列名=更新值 where 更新条件

例如:

update student set sex=‘女’

update student set address=‘北京’ where address=‘甘肃’

-----------------------使用DML删除数据---------------------------------------------

delete from 表名 where 删除条件

例如:

delete from student where studentName=‘王宝宝’

删除表中所有记录

truncate table student

--------------------------使用DML查询数据---------------------------------------------------

select 列名|表达式|函数|常量 from 表名 where 查询条件 order by asc|desc

猜你喜欢

转载自my.oschina.net/u/3740271/blog/1650017