-- 查詢所有數(shù)據(jù)庫
show databases;
-- 創(chuàng)建數(shù)據(jù)庫,數(shù)據(jù)庫名為mydatabase
?
create database mydatabase;
-- 如果沒有名為 mydatabase的數(shù)據(jù)庫則創(chuàng)建,有就不創(chuàng)建
?
create database if not exists mydatabase;
-- 如果沒有名為 mydatabase的數(shù)據(jù)庫則創(chuàng)建,有就不創(chuàng)建,數(shù)據(jù)庫字符編碼設(shè)置為utf8mb4
create database if not exists mydatabase character set utf8mb4;
-- 使用數(shù)據(jù)庫
use mydatabase;
-- 刪除名為mydatabase的數(shù)據(jù)庫
drop database mydatabase;
-- 如果有名為 mydatabase的數(shù)據(jù)庫則刪除,沒有就不刪除
drop database if exists mydatabase;
-- 查看mydatabase數(shù)據(jù)庫的表結(jié)構(gòu)
desc mydatabase;
-- 創(chuàng)建表,comment用于字段說明,創(chuàng)建一個表有id,name,gender,age,class字段
create table mytable(
? ?id int not null ,
? ?name varchar(20) not null comment '姓名',
? ?gender varchar(2) not null comment '性別',
? ?age int not null comment '年齡',
? ?class varchar(20) not null comment'班級'
);
-- 刪除名為mytable的表
drop table mytable;
-- 如果存在則刪除mytable表,不存在就不刪除
drop table if exists mytable;
-- 數(shù)據(jù)庫主要有增刪查改等操作
-- 創(chuàng)建一個學(xué)生表student,字段有id ,sn, name,qq_meail
create table student (
? ?id int ,
? ?sn int comment '學(xué)號',
? ?name varchar (20) comment '姓名' ,
? ?qq_email varchar(32) comment 'qq郵箱'
);
-- 增,插入數(shù)據(jù)
insert into student (id,sn,name,qq_email) values (1,'10000','張三','123456@qq.com');
-- 可以指定列插入
?
insert into student (sn,name,qq_email) values( '1002','李四','234567@qq.com');
?
?-- 查,查詢記錄
?-- 查詢student表里所有字段
?-- 一般不用這種全列查詢,因為全列查詢效率低
?select * from student;
?
?-- 查詢student表里的指定列 sn,name,qq_email
?select sn,name,qq_email from student;
?
?-- 查詢字段也可以進(jìn)行表達(dá)式運算
?select sn+10,name,qq_email from student;
?-- 再比如,插查詢字段為id, name, chninese,english,math三門成績相加
?select id,name,chinese+english+math from student;
?
?
?-- 數(shù)據(jù)庫約束
?
?-- NULL約束,當(dāng)字段設(shè)置為not null,那么這一列就不能為空
?create table student (
? ?id int ?not null ,
? ?name varchar(32) not null,
? ?age int not null
?);
?
?-- unique約束:指定字段的某列是唯一的,不可以重復(fù)的
?-- 創(chuàng)建一個學(xué)生表,id為主鍵自增,sn設(shè)置為唯一的unique
?drop table if exists student ;
?create table student(
? ? id int not null primary key auto_increment,
? ? sn int unique comment '學(xué)號',
? ? name varchar(32) not null comment '名字'
?);
?-- default約束:規(guī)定沒有給列賦值時可以給一個默認(rèn)值
-- 當(dāng)name不知道時設(shè)置默認(rèn)為'未知'
?
drop table if exists student;
?create table student (
? ? id int not null primary key auto_increment,
? ? sn int unique comment '學(xué)號',
? ? name varchar(32) default ?'未知' comment '名字'
?);
?-- 主鍵約束
?-- id為主鍵且設(shè)為自增
drop table if exists student;
?create table student (
? ? id int not null primary key auto_increment,
? ? sn int ?comment '學(xué)號',
? ? name varchar(32) comment '名字'
?);
?
?-- 外鍵約束:外鍵約束用來關(guān)聯(lián)表之間的關(guān)系,可以用來關(guān)聯(lián)其他表的主鍵
?-- 添加外鍵約束語法 ?foreign key (字段名) references (主表的列)
?
1.查詢與過濾
題目:現(xiàn)在運營想要查看用戶信息表中所有的數(shù)據(jù),請你取出相應(yīng)結(jié)果
select*from user_profile;
題目:現(xiàn)在運營同學(xué)想要用戶的設(shè)備id對應(yīng)的性別、年齡和學(xué)校的數(shù)據(jù),請你取出相應(yīng)數(shù)據(jù)
select device_id,gender,age,university from user_profile;
?題目:現(xiàn)在運營需要查看用戶來自于哪些學(xué)校,請從用戶信息表中取出學(xué)校的去重數(shù)據(jù)。
select distinct university from user_profile;
題目:現(xiàn)在運營只需要查看前2個用戶明細(xì)設(shè)備ID數(shù)據(jù),請你從用戶信息表?user_profile 中取出相應(yīng)結(jié)果。
-- LIMIT n OFFSET m:從第m+1條開始,取n條數(shù)據(jù)
select device_id from user_profile limit 2 offset 0;
題目:現(xiàn)在你需要查看前2個用戶明細(xì)設(shè)備ID數(shù)據(jù),并將列名改為 'user_infos_example',,請你從用戶信息表取出相應(yīng)結(jié)果。
select device_id as user_infos_example from user_profile limit 2 offset 0;
題目:現(xiàn)在運營想要篩選出所有北京大學(xué)的學(xué)生進(jìn)行用戶調(diào)研,請你從用戶信息表中取出滿足條件的數(shù)據(jù),結(jié)果返回設(shè)備id和學(xué)校。
select device_id,university from user_profile where university='北京大學(xué)';
題目:現(xiàn)在運營想要針對24歲以上的用戶開展分析,請你取出滿足條件的設(shè)備ID、性別、年齡、學(xué)校。
select device_id,gender,age,university from user_profile where age>=24;
題目:現(xiàn)在運營想要針對20歲及以上且23歲及以下的用戶開展分析,請你取出滿足條件的設(shè)備ID、性別、年齡。
select device_id,gender,age from user_profile where age>=20 and age<=23;
題目:現(xiàn)在運營想要查看除復(fù)旦大學(xué)以外的所有用戶明細(xì),請你取出相應(yīng)數(shù)據(jù)文章來源:http://www.zghlxwxcb.cn/news/detail-655737.html
select device_id,gender,age,university from user_profile where university!='復(fù)旦大學(xué)';
題目:現(xiàn)在運營想要對用戶的年齡分布開展分析,在分析時想要剔除沒有獲取到年齡的用戶,請你取出所有年齡值不為空的用戶的設(shè)備ID,性別,年齡,學(xué)校的信息。文章來源地址http://www.zghlxwxcb.cn/news/detail-655737.html
select device_id,gender,age,university from user_profile where age is not null;
到了這里,關(guān)于數(shù)據(jù)庫SQL語句使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!