数据库学习笔记(1)

来源:互联网 发布:类似于facerig的软件 编辑:程序博客网 时间:2024/06/01 07:59

第一章节
1.创建数据库
CREATE DATABASE gregs_list;
2.使用数据库
USE gregs_list;
3.为数据库创建数据表
//创建列表项时,可以加入限制,比如某一列不能为空,可以在其后加入 NOT NULL,(如下)
//而对于这些在输入时不能省的值,我们可以指定一个缺省的值,如下,当你不给它初值时,就会默认’陈’,当然,你也可以不在NOT NULL情况下使用DEFAULT!这样
//可以防止缺省时为NULL,创建表时,可以加入DEFAULT以填补缺省值。
CREATE TABLE my_contacts
(
last_name VARCHAR(10) NOT NULL DEFAULT ‘陈’,
first_name VARCHAR(10) DEFAULT ‘志威’,
email VARCHAR(50),
gender CHAR(1),
birthday DATE,
profession VARCHAR(50),
location VARCHAR(50),
status_now VARCHAR(20),
interests VARCHAR(100),
seeking VARCHAR(100)
);
4.检查数据表结构情况
DESC my_contacts;

5.删除数据表
DROP TABLE my_contacts;

//当发现要给表加入新的列时,怎么办?难道只能重建一个表吗,而在在建这个表之前还得把原来的表先删除了,实在是烦
//当然,是有方法在原表基础上加上新的列的!!!

6.插入行信息
//3点注意:
//这里要对应上面的列表项,不能多也不能少,当然你列表项可以只写几项,总之要值部分要对应,对于CHAR,VARCHAR,DATE,BLOB等都要加上单引号
//当然列的顺序可以变,但对应值的顺序也要变
//你可以不写列表项,但值部分必须写全而且要一一对应!!
INSERT INTO my_contacts
(
last_name,
first_name,
email,
gender,
birthday,
profession,
location,
status_now,
interests,
seeking
)
VALUES
(
‘chen’,
‘zw’,
‘xx@qq.com’,
‘x’,
‘xx-xx-xx’,
‘student’,
‘xx’,
‘single’,
‘movie,practice’,
‘good job, girl friend’
)

7.查看数据表内容
//* 表示所有的列表项
SELECT * FROM my_contacts;

第二章节
1.限定条件查找
//WHERE 与等号 关键字的使用
//注意 * 表示返回所有的列项,也就是说,你可以指定返回的列项
SELECT * FROM my_contacts WHERE first_name = ‘志威’;
//返回特定的列项,注意,这里要加逗号分隔,否则只会返会写出的最后一个列项
SELECT first_name,last_name FROM my_contacts WHERE first_name = ‘志威’;

/***********************************************************************************************/

//这里有一个习题,先输建一个表
CREATE DATABASE drinks;
USE drinks;
CREATE TABLE easy_drinks
(
drink_name VARCHAR(20) NOT NULL DEFAULT ‘Blackthorn’,
main VARCHAR(20) NOT NULL DEFAULT ‘tonic water’,
amount1 DEC(3,2) NOT NULL DEFAULT 1.00,
second VARCHAR(20),
amount2 DEC(3,2),
directions VARCHAR(255)
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Blackthorn’,
‘tonic water’,
1.5,
‘pineapple juice’,
1,
‘stir with ice, strain into cocktail glass with lemon twist’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Blue Moon’,
‘Soda’,
1.5,
‘blueberry juice’,
0.75,
‘stir with ice, strain into cocktail glass with lemon twist’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Oh My Gosh’,
‘peach nectar’,
1,
‘pineapple,juice’,
1,
‘stir with ice, strain into shot glass’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Lime Fizz’,
‘Sprite’,
1.5,
‘lime juice’,
0.75,
‘stir with ice, strain into cocktail glass’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Kiss on the Lips’,
‘cherry juice’,
2,
‘apricot nectar’,
7,
‘serve over ice with straw’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Hot Gold’,
‘peach nectar’,
3,
‘orange juice’,
6,
‘pour hot orange juice in mug and add’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Lone Tree’,
‘soda’,
1.5,
‘cherry juice’,
0.75,
‘stir with ice ,strain into cocktail glass’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Greyhound’,
‘soda’,
1.5,
‘grapefruit juice’,
5,
‘serve over ice,stir well’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Indian Summer’,
‘apple juice’,
2,
‘hot tea’,
6,
‘add juice to mug and top off with hot tea’
);

INSERT INTO easy_drinks
(
drink_name,
main,
amount1,
second,
amount2,
directions
)
VALUES
(
‘Bull Frog’,
‘iced tea’,
1.5,
‘lemonade’,
5,
‘serve over ice with lime slice’
);

//注意下面的语句可以正常运行,因为DEC,INT 不会因为你加了引号就把它当成文本值的,会自动去掉引号,但是最好不人这样排行
//另外,可以用双引号,但是别使用双引号!!!!!
SELECT * FROM easy_drinks WHERE amount1 = ‘1.5’;

//具体哪些类型怎么用引号呢,下面看
类型 引号
CHAR 通常是单引号
VARCHAR 必用单引号
DEC 千万别用引号
INT 从不用引号
BLOB 必用单引号
DATE 必用单引号
TIME 必用引号

//更多关于标点的问题
//因为你总有可能在单引号里面有一个所有格出现如下的情况
‘Jim’s interest is sleep’
//这时就要转义了如下两种方式:
‘Jim\’s interest is sleep’
‘Jim”s interest is sleep’

2.选择特定显示,这里显示的列项信息与建表时是不一样的
select main, second, drink_name from easy_drinks where main = ‘soda’;

//只选择需要的列是一个好的编程习惯,同时也使得运行效率提高!!!

WHERE 关键字还有很多关于数值的比较运算符,如

< <> = <= >=

3.注意一个AND符,它可以组合多个条件
select main, second, drink_name from easy_drinks where main = ‘soda’ AND second = ‘d’ AND drink_name = ‘dd’;

对于比较运算符,还可以对文本进行比较,但这个是按字母顺序地评选的,比
如说第一个字母都符合要求,则比第二个,直到比出来为止
如:
abc < bca;为真

4.OR 只要符合一项条件

5.用 IS NULL 选择 NULL
当你想要找出NULL的项时,不能写
WHERE XXX=NULL;
也不可以是
WHERE XXX=0;
因为NULL != 0 ,那么如何选择出NULL的项呢?如下:

WHERE XXX IS NULL;

这是唯一正确的做法!

6.关键字LIKE 与通配符 %

SELECT * FROM MMMM WHERE xxx LIKE ‘%CA’;

这表示查找所有以CA结尾的XXX项

//LIKE 有很多通配符,下面一一说明
% 任意数量的未知字符的替身
_ 一个未知字符的替身

7.BETWEEN … AND ….
用法:
WHERE XXX BETWEEN … AND ….;

8.IN 与 NOT IN 给出一个范围

用法:
WHERE XXX IN(‘FA’,’ADF’);
表示()内的符合要求的项

9.NOT 的使用

WHERE NOT XXX BETWEEN … AND …
表示,不在..。….之间的值返回。

NOT 必须紧跟在WHERE后,当然NOT IN 是一个例外

WHERE XXX IS NOT NULL;

搭配AND 或 OR,请直接放其后
WHERE NOT XXX …. AND NOT …..

总之,NOT是表示否定的,跟在WHERE或关键字后面

0 0
原创粉丝点击