MYSQL数据库基础和常用语法汇总02篇

您所在的位置:网站首页 mysql导出表语句 MYSQL数据库基础和常用语法汇总02篇

MYSQL数据库基础和常用语法汇总02篇

2023-04-02 20:19| 来源: 网络整理| 查看: 265

【一、基础操作SQL语句】

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

show databases; 查看库

create database CHEN; 创建库CHEN

use CHEN; 选择库

drop database stu; 删除库stu

show tables; 查看库中的表

创建表`user`

create table `emp`(

`eno` int unsigned auto_increment,

`ename` varchar(80) not null,

`eage` int unsigned,

`esex` varchar(10) not null,

`edate` date,

`etel` int(20),

`eblood` varchar(10),

primary key(`eno`)

) engine=InnoDB default charset=utf8;

create table `teacher`(

`tno` int unsigned auto_increment,

`tname` varchar(40) not null,

`tage` int unsigned,

`ttel` varchar(15),

primary key(`tno`)

) engine=InnoDB default charset=utf8;

desc stu; #查看表中字段结构

describe stu; #同上

show columns from stu #同上

show create table stu; #显示表结构

create table tab_new like tab_old #复制,使用旧表创建新表,类似于复制

drop table student; #删除表student

create temporary table stu12 like tab_old #创建临时表,这里和创建普通表一样

rename table stu to stu11; #修改表名,重命名

Alter table tabname add primary key(col) #添加主键

Alter table tabname drop primary key(col) #删除主键

Alter table tabname add foreigh key(id) references dept(did) #添加外键

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

例子:关于有外键关联表无法删除

建部门表dept

create table dept(

`did` int PRIMARY key auto_increment,

`dname` varchar(30) ,

) engine=InnoDB default charset=utf8;

建员工表emp

create table emp(

`eid` int PRIMARY key auto_increment,

`ename` varchar(30) ,

`sal` DOUBLE,

dno int,

FOREIGN key(dno) REFERENCES dept(did) #创立外键

) engine=InnoDB default charset=utf8;

DELETE from dept where did=2 #无法删除,提示错误,因为表dept中有存在另外一个关联的表emp.

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

create [unique] index idxname on tabname(col….) 创建索引

drop index idxname 删除索引

注:索引是不可更改的,想更改必须删除重新建。

创建视图:create view viewname as select statement

删除视图:drop view viewname

desc stu; #查看表中字段结构

describe stu; #同上

show columns from stu #同上

show create table stu; #显示表结构,看得比较整齐

show tables; 查看创建的表

select * from stu; #查看表中是否有内容,empty

insert into stu11

values

(5,'shenzhen33',22,'female','2020-03-13','13812349'),

(6,'shenzhe44',48,'male','2020-03-13','13812340'),

(7,'shenzhen55',28,'male','2020-03-13','13812340');

【二、数据操作语言】

insert into stu values(5,"wang",26,"male"); #往表stu中插入一行数据

insert into stu #往表stu中插入数据

(id,name,age,sex)

values

(1,"chenzj",20,"male"),(3,"lishi",25,"female"),(4,"mary",21,"female"); #可同时插入一行或多行

select * from stu; #查看表中数据

只复制表结构到新表

CREATE TABLE 新表 SELECT * FROM 旧表 WHERE 1=2;

复制表结构及数据到新表

CREATE TABLE 新表 SELECT * FROM 旧表

复制旧表的数据到新表(假设两个表结构一样)

INSERT INTO 新表 SELECT * FROM 旧表

复制旧表的数据到新表(假设两个表结构不一样)

INSERT INTO 新表(字段1,字段2,.......) SELECT 字段1,字段2,...... FROM 旧表

insert into stu #只选择某一列插入数据,且非空字段必须要存在

(name,sex,date)

values

("wangwu","male","2020-03-12");

alter table stu add column tel int(15); #修改表,增加一列tel

alter table stu change tel telphone VARCHAR(10); #修改表,修改列tel为telphone

alter table stu drop coulumn telphone; #修改表,删除列telphone

update stu set tel='13512345678'; #更新表,把列tel全部加上值

update stu set date='2020-03-11' where id=1; #更新表,只针对第1行加上日期

update stu set date='2020-03-11' where sex='female' and age='25'; #更新表,多条件筛选后加上日期

update stu set age=21,sex='female' where id=1; #更新表,同时更新第1行中2个字段; 【注:字段间要用,隔开】

update stu set date=NULL where id=4; #更新表,把第4行日期更新为空

update stu set tel=NULL; #更新表,把tel列全部值更新为空

delete from stu where id=5; #删除第5行



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3