AS/400 Chapter 4: SQL

Now that we've created a library and saw some basic AS/400 features let's have a look at how we can create and manipulate tables using SQL.

You must create the DEMO library and define it has your current library to proceed with this chapter.

Type the following Start SQL command on the system prompt:

STRSQL
STRTSQL screen
STRTSQL screen

This way you begin a SQL session in AS/400. This application accepts most of the common SQL syntax (CREATE,INSERT, DELETE, SELECT, DROP, etc).

Creating a table with SQL

From the SQL command line we're going to create the CLIENTS table and then insert some rows. For this tutorial let's assume that all phone numbers have, at most, 9 digits.

Press F3 to leave the table view and type the following commands. Type:

CREATE LIBRARY DEMO/CLIENTS( id_cli numeric(10) PRIMARY KEY, name_cli char(50), birth_cli date, phone_cli numeric(9) )

Note: You can place the code in separate lines (like in the example) or in a continued fashion (without indentation) as long as you don't press the Enter key before you finish the command.

A message will appear saying that the table was created.

If you want to see more empty command lines just press Page Down or move the mouse scroll down. Now Type:

SELECT * FROM CLIENTS

This way you can see that the table was created and it has no information in it. So let's insert some new rows.

Press F3 to leave the table view and type the following commands.

INSERT INTO CLIENTS (ID_CLI, NAME_CLI, BIRTH_CLI, PHONE_CLI) VALUES (1, 'Mary', '12/09/1967', '999999999')
INSERT INTO CLIENTS (ID_CLI, NAME_CLI, BIRTH_CLI, PHONE_CLI) VALUES (2, 'Tom', '09/01/1979', '123456789')

Do the SELECT command again to check if the rows were correctly inserted.

Inserting data into a table
Inserting data into a table

Press F3 to leave the SQL command line. A set of options will appear. On the field you should write the option number. Try typing a number outside the option range (for instance 5) and then press Enter. As one would expect, an error message appears on the bottom of the screen. Try pressing F3 or inserting another value. It won't work because the screen is blocked. You will probably find some blocking errors and when you do press Esc the screen will go back to normal.

Now select the option 1 to exit the application, saving the session.

SQL Built-In Functions

There are some very useful SQL built-in functions on the AS/400 system.

Basic Functions

If you know SQL you may already be familiar with these functions.

Function Description
MAX Returns the maximum value from a set of pre-defined values.
MIN Returns the minimum value from a set of pre-defined values.
AVG Returns the average value of a set of pre-defined values.
SUM Returns the sum of a set of pre-defined values.
COUNT Returns the number of elements in a set of pre-defined values.

Example (returns the maximum ID_CLI value from all the rows in the table CLIENTS):

SELECT MAX(ID_CLI) FROM CLIENTS

Numeric Functions

Function Description
ABS(N) Returns the absolute value of N.
COS(A) / ACOS(A) Basic trigonometric functions.
SIN(A) / ASIN(A) Basic trigonometric functions.
TAN(A) / ATAN(A) Basic trigonometric functions.
CEILING(N) Returns the rounding of N to the unit above.
FLOOR(N) Returns the rounding of N to the unit below.
DEGREES(R) Converts a value in radians to degrees.
LN(N) Returns the natural logarithm of N.
LOG10(N) Returns the base 10 logarithm of N.

String Functions

Function Description
CHAR(N) Returns the the string representation of the number N.
CHAR_LENGTH(S) Returns the length of a string.
CONCAT(S1, S2) Concatenates S1 with S2.
SUBSTR(S, I, L) Returns a substring of S, starting at index I of lenght L.
LOWER(S) Returns the lowercase representation of S.
UPPER(S) Returns the uppercase representation of S.
TRIM(S) Removes spaces from the beggining and and of S.
RTRIM(S) Removes spaces at the begging (right) of S.
LTRIM(S) Removes spaces at the end (left) of S.

Date and Time Functions

Function Description

CURDATE()

CURTIME()

Returns the system's current date/time.

DATE(D)

DATE(T)

Converts a string representation of a date/time into into a date/time value.
DAY(D) Returns the day(1-31) from the date D.
WEEK(D) Returns the week (1-54) from the date D.
MONTH(D) Returns the month (1-12) from the date D.
YEAR(D) Returns the year from the date D.
DAYOFWEEK(D) Returns the week day (1-7) from the date D where 1 is Sunday.
DAYOFWEEK_ISO(D) Returns the week day (1-7) from the date D where 1 is Monday.
DAYOFYEAR(D) Returns the number of the day, in a year (1-366).
HOUR(T) Returns the hour (0-24) from the time T.
MINUTE(T) Returns the minute from the time T.
SECOND(T) Returns the second from the time T.
MICROSECOND(T) Returns the microsecond from the time
Dércia Silva
Posted by Dércia Silva on October 26, 2007

Related articles

哆哆女性网世纪特工的营销策略推广翘嘴鱼叫什么名字包姓宝宝起名大全男孩起名博文商丘高铁北出站口接人北京旅游网站制作韩姓起名男儿子北北电影天堂最新电影浙江建设网站两个字的宾馆起名大全周公解梦中梦见蛇是什么意思最权威的周易版本网站设计优秀朱周易姓高的男孩起名字啊戴尔外星人官网养生四宝粉使命召唤9中文版下载兆起名汽车网站制作模板网站关键字如何优化项目管理公司起名三个字幻想三国志2续缘篇周易姓名八字评分测算女装店起名时尚简单求职招聘网站设计衣服牌子起名qq特殊网名淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化