数据库补充

约束:
not null: 列名 int not null;
unique:UNIQUE (P_Id)(ALTER TABLE Persons
ADD UNIQUE (P_Id),ALTER TABLE Persons
DROP INDEX uc_PersonID)
primary key :PRIMARY KEY (P_Id)
foreign key:FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
check:CHECK 约束用于限制列中的值的范围。check(pid>0)
default: City varchar(255) DEFAULT ‘Sandnes’
大写:
SELECT UPPER(name) AS site_title, url FROM Websites;
小写:
SELECT LOWER (name) AS site_title, url FROM Websites;
从 “Websites” 表的 “name” 列中提取前 4 个字符:
SELECT MID(name,1,4) AS ShortTitlem FROM Websites;
length(a):返回a列的长度
1.order by a,b;先 按照a升序,然后按照b升序;
2.not between a and b;
3.INNER JOIN:如果表中有至少一个匹配,则返回行,没有则不会列出
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行,null
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行,null
FULL JOIN:只要其中一个表中存在匹配,则返回行,null
4.union重复的只显示一个,all重复的全部显示;
SELECT country FROM Websites
UNION(ALL)
SELECT country FROM apps
ORDER BY country;
5.把表中的数据复制到一个新表里面

创建 Websites 的备份复件:

SELECT *
INTO WebsitesBackup2016
FROM Websites;
只复制一些列插入到新表中:

SELECT name, url
INTO WebsitesBackup2016
FROM Websites;
只复制中国的网站插入到新表中:

SELECT *
INTO WebsitesBackup2016
FROM Websites
WHERE country='CN';
复制多个表中的数据插入到新表中:

SELECT Websites.name, access_log.count, access_log.date
INTO WebsitesBackup2016
FROM Websites
LEFT JOIN access_log
ON Websites.id=access_log.site_id;
提示:SELECT INTO 语句可用于通过另一种模式创建一个新的空表。只需要添加促使查询没有数据返回的 WHERE 子句即可:

SELECT *
INTO newtable
FROM table1
WHERE 1=0;

6、从一个表中选择数据插入一个已经存在的表中
INSERT INTO Websites (name, country)
SELECT app_name, country FROM apps;

你可能感兴趣的:(数据库补充)