sql语句

xiaoxiao2021-02-28  108

登陆   mysql -u  root -p 注意最后没有分号

然后输入密码即可

显示所有的数据库 : show databases;

创建数据库:create database XXX;

删除数据库:drop database XXX;

创建表:create table XXX(id int,name varchar(50));

删除表:drop table XXX;

仅仅需要删除表内的数据,但并不删除表本身

切换数据库:  use XXXdatabse;

显示数据库中的所有表: show tables;

显示表的结构:desc XXXtable;

显示表的所有数据:select * from XXXtable;

删除表格中某个字段:alter table student drop column  XXX;

删除表格中某个数据:delete from XXX    where XXX;

添加某个字段到表格中:alter table XXX add XXX  int ;

修改表格中的某个字段:alter table user MODIFY new1 VARCHAR(10); 

添加某个数据到表格中:insert into XXX(columnXXX) values (XXXX);

为某个表添加外键: alter table XXX add constraint foreign key (studentid) references student(id);

为某个字段添加为主键并且自增长:create table department(id int primary key auto_increment);

模糊查询:select * from XXX where XXX like 'XXX';

规定返回的条目:select top 5 from XXX  where XXX  或者  select * from XXX limit 5;

通配符:

在where后面如果需要多个值: select * from XXX where in (XXX,XXXX,XXX);

选取某个区间的值:select * from XXX where  XXX between 1  and 20;

别名:select * from  Websites w where w.id = 1;或者 select * from Websites as w where id=1;

从多个表中返回满足 JOIN 条件的所有行:select * from Websites inner join apps on Websites.id=apps.id;

从左表(table1)返回所有的行,即使右表(table2)中没有匹配:left join

从右表(table1)返回所有的行,即使左表(table2)中没有匹配:right join

关键字只要左表(table1)和右表(table2)其中一个表中存在匹配,则返回行: full join;

UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL:select country from websites union select country from apps order by country;

UNIQUE 约束唯一标识数据库表中的每条记录:create table XXX(id int not null unique);

在创建表格时指定默认值:create table XXX(name varchar(255) default 'Alice');

创建视图: create view view_websites as select w.id,w.name from websites w;

更新视图:  alter view view_websites as select w.id,w.url from websites w;

删除视图:drop view view_websites;

查询平均值: select avg(count) countaverage from assess_log;

转载请注明原文地址: https://www.6miu.com/read-77634.html

最新回复(0)